-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpolyocat.js
164 lines (138 loc) · 5.62 KB
/
polyocat.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
async function main() {
const REGEXP = {
"/ year": /\/ year$/,
"/ month": /\/ month$/,
"N open": /^([\d,]+) Open$/,
"N closed": /^([\d,]+) Closed$/,
"N files": /^([\d,]+) files?$/,
"N forks": /^([\d,]+) forks?$/,
"N stars": /^([\d,]+) stars?$/,
"N labels": /^([\d,]+) labels?$/,
"N comments": /^([\d,]+) comments?$/,
"N remaining": /^([\d,]+) remaining$/,
"N languages": /^([\d,]+) languages?$/,
"N pending invitations": /^([\d,]+) pending invitations?$/,
"N applications": /^([\d,]+) applications?$/,
"N repositories": /^([\d,]+) repositor(?:y|ies)$/,
"N hours ago": /(\d+) hours? ago/,
"N minutes ago": /(\d+) minutes? ago/,
"N stars today": /^([\d,]+) stars today$/,
"N stars this week": /^([\d,]+) stars this week$/,
"N stars this month": /^([\d,]+) stars this month$/,
"N contributions": /^([\d,]+) contributions?$/,
"N contributions in the last year": /^([\d,]+) contributions in the last year$/,
"month day": /^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d+)$/,
"on month day": /^on (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d+)$/,
"on month day, year": /^on (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d+), (\d{4})$/,
"Joined on Jun 24, 2017": /^Joined on (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d+), (\d{4})$/,
"View USER on GitHub": /View ([\w-]+) on GitHub/,
"Delete PROJECT_NAME": /Delete (.*)$/,
"View USER": /View ([\w-]+)$/,
"Invite members to ORG": /^Invite members to (.*)$/,
"Turn USER into an organization": /Turn ([\w-]+) into an organization/,
"Your next charge will be on date": /Your next charge will be on (\d{4}-\d{2}-\d{2})./
}
let page = where()
let lang = language()
let dict = await translation(lang)
let observer = new MutationObserver(mutations => {
mutations
.filter(mutation =>
(mutation.type === 'childList' && mutation.addedNodes.length) ||
(mutation.type === 'attributes' && mutation.attributeName === 'placeholder')
)
.forEach(mutation => translate(mutation.target))
})
observer.observe(document.body, {
attributes: true,
childList: true,
subtree: true
})
translate(document.body)
function find(str) {
if (page && dict[page] && dict[page][str]) return ` ${dict[page][str]} `
if (dict.global[str]) return ` ${dict.global[str]} `
for (let key in REGEXP) {
let reg = REGEXP[key]
if (reg.test(str)) {
str = str
.replace(reg, dict.regexp[key])
.replace(/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/, (match, p1) => dict.global.m2n[p1])
return ` ${str} `
}
}
return ''
}
function where() {
if (location.host === 'gist.github.com') return 'gist'
let pathname = location.pathname
if (pathname === '/' || /dashboard$/.test(pathname)) return 'dashboard'
if (pathname === '/trending') return 'trending'
if (pathname === '/new' || pathname.endsWith('repositories/new')) return 'new'
if (/^\/watching/.test(pathname)) return 'watching'
if (/^\/pulls(\/.*)?/.test(pathname)) return 'pulls'
if (/^\/notifications/.test(pathname)) return 'notifications'
if (/^\/(?:organizations\/\w+\/)?settings\/.+$/.test(pathname)) return 'settings'
if (/^\/[\w-]+\/?$/.test(pathname)) return 'profile'
if (/^\/[\w-]+\/.+$/.test(pathname)) return 'repository'
}
function language() {
return localStorage.getItem('polyocat-lang') || navigator.language || navigator.languages[0]
}
async function translation(lang) {
let url = chrome.extension.getURL(`translations/${lang}.json`)
let res = await fetch(url)
return await res.json()
}
function translate(node) {
translateNode(node)
if (node.classList && node.classList[0] === 'file') {
return translate(node.firstElementChild)
}
if (
node.id === 'files' ||
node.id === 'readme' ||
/highlight/.test(node.className) ||
/octotree_sidebar/.test(node.className) ||
/personal-access-tokens-group/.test(node.className)
) { return }
if (node.className && /CodeMirror/.test(node.className)) return
node.childNodes.forEach(translate)
}
function translateNode(node) {
if (node.nodeType === Node.TEXT_NODE) return translateTextNode(node)
if (node.nodeType === Node.ELEMENT_NODE) return translateElement(node)
}
function translateTextNode(node) {
if (!node || node.nodeValue == null) return
let text = node.nodeValue.replace(/[\n\s]+/g, ' ').trim()
let tran = find(text)
if (tran) node.nodeValue = tran
}
function translateElement(elem) {
if (elem.tagName === 'INPUT' || elem.tagName === 'TEXTAREA') translateAttribute(elem, 'placeholder')
if (elem.tagName === 'INPUT' && elem.type === 'submit') translateAttribute(elem, 'value')
if (elem.tagName === 'OPTGROUP' && elem.label) translateAttribute(elem, 'label')
if (elem.tagName === 'RELATIVE-TIME' || elem.tagName === 'TIME-AGO') {
elem.innerText = elem.innerText.replace(
/(\d+|a|an) (day|days|hour|hours|minute|minutes|second|seconds|month|months|year|years) (ago)/,
(match, p1, p2, p3) => `${find(p1) || p1} ${find(p2).trim()}${find(p3).trim()}`
)
}
translateAttribute(elem, 'aria-label')
translateAttribute(elem, 'data-copied-hint')
}
function translateAttribute(elem, name) {
if (elem[name]) {
let tran = find(elem[name])
if (tran) elem[name] = tran
return
}
if (elem.hasAttribute(name)) {
let tran = find(elem.getAttribute(name))
if (tran) elem.setAttribute(name, tran)
}
}
console.log(page, lang, dict)
}
main()