-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathgithub_number.js
56 lines (48 loc) · 1.7 KB
/
github_number.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// ==UserScript==
// @name Github显示具体Star数字
// @namespace http://tampermonkey.net/
// @homeurl https://github.com/xiandanin/LardMonkeyScripts
// @homeurl https://greasyfork.org/zh-CN/scripts/391285
// @version 0.8
// @description 让Star/Fork等显示完整的数字
// @author xiandan
// @match https://github.com/*
// @grant none
// ==/UserScript==
(function () {
'use strict'
function extractNumber (str) {
let reg = /\d+/
let match = reg.exec(str)
return parseInt(match ? match[0] : str)
}
function formatNumber (num) {
return num.toString().replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, "$1,")
}
function applyNodeNumber () {
// 过滤出需要设置 并且有详细数字的节点
const headerNode = document.querySelector('.pagehead-actions') || document.querySelector('.Layout');
if (headerNode) {
const nodes = headerNode.querySelectorAll('.Counter')
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i]
if (/^\d+$/.test(node.innerText)) {
// 如果已经是纯数字
} else {
const countStr = node.getAttribute("title").replace(/,/g, '')
node.innerText = formatNumber(extractNumber(countStr))
}
}
}
}
const main = document.querySelector('main')
if (main != null) {
const observer = new MutationObserver(function (mutations, observer) {
applyNodeNumber()
})
observer.observe(main, {
childList: true
})
applyNodeNumber()
}
})()