-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathuserscript.js
289 lines (263 loc) · 8.38 KB
/
userscript.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// ==UserScript==
// @name GitHub Internationalization
// @name:zh-CN GitHub汉化插件
// @name:ja GitHub日本語
// @namespace https://github.com/k1995/github-i18n-plugin/
// @version 0.30
// @description Translate GitHub.com
// @description:zh GitHub汉化插件,包含人机翻译
// @description:zh-CN GitHub汉化插件,包含人机翻译
// @description:ja GitHub日本語プラグイン
// @author k1995
// @match https://github.com/*
// @match https://gist.github.com/*
// @grant GM_xmlhttpRequest
// @grant GM_getResourceText
// @resource zh-CN https://www.github-zh.com/raw-githubusercontent/k1995/github-i18n-plugin/master/locales/zh-CN.json?v=20240617
// @resource ja https://www.github-zh.com/raw-githubusercontent/k1995/github-i18n-plugin/master/locales/ja.json
// @require https://cdn.staticfile.org/timeago.js/4.0.2/timeago.min.js
// @require https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js
// @license MIT
// ==/UserScript==
(function() {
'use strict';
const SUPPORT_LANG = ["zh-CN", "ja"];
const lang = (navigator.language || navigator.userLanguage);
const locales = getLocales(lang)
translateByCssSelector();
translateTime();
traverseElement(document.body);
watchUpdate();
// 翻译描述
if(window.location.pathname.split('/').length == 3) {
translateDesc(".repository-content .f4"); //仓库简介翻译
// translateDesc(".gist-content [itemprop='about']"); // Gist 简介翻译
}
function getLocales(lang) {
if(lang.startsWith("zh")) { // zh zh-TW --> zh-CN
lang = "zh-CN";
}
if(SUPPORT_LANG.includes(lang)) {
return JSON.parse(GM_getResourceText(lang));
}
return {
css: [],
dict: {}
};
}
function translateRelativeTimeEl(el) {
const datetime = $(el).attr('datetime');
let humanTime = timeago.format(datetime, lang.replace('-', '_'));
if(el.shadowRoot) {
el.shadowRoot.textContent = humanTime;
} else {
el.textContent = humanTime;
}
}
function translateElement(el) {
// Get the text field name
let k;
if(el.tagName === "INPUT") {
if (el.type === 'button' || el.type === 'submit') {
k = 'value';
} else {
k = 'placeholder';
}
} else {
k = 'data';
}
if (isNaN(el[k])){
const txtSrc = el[k].trim();
const key = txtSrc.toLowerCase()
.replace(/\xa0/g, ' ') // replace ' '
.replace(/\s{2,}/g, ' ');
if (locales.dict[key]) {
el[k] = el[k].replace(txtSrc, locales.dict[key])
}
}
translateElementAriaLabel(el)
}
function translateElementAriaLabel(el) {
if (el.ariaLabel) {
const k = 'ariaLabel'
const txtSrc = el[k].trim();
const key = txtSrc.toLowerCase()
.replace(/\xa0/g, ' ') // replace ' '
.replace(/\s{2,}/g, ' ');
if (locales.dict[key]) {
el[k] = el[k].replace(txtSrc, locales.dict[key])
}
}
}
function shouldTranslateEl(el) {
const blockIds = [
"readme",
"file-name-editor-breadcrumb", "StickyHeader", "sticky-file-name-id", "sticky-breadcrumb" // fix repo详情页文件路径breadcrumb
];
const blockClass = [
"CodeMirror",
"js-navigation-container", // 过滤文件目录
"blob-code",
"topic-tag", // 过滤标签,
// "text-normal", // 过滤repo name, 复现:https://github.com/search?q=explore
"repo-list",//过滤搜索结果项目,解决"text-normal"导致的有些文字不翻译的问题,搜索结果以后可以考虑单独翻译
"js-path-segment","final-path", "react-tree-show-tree-items", //过滤目录,文件位置栏
"markdown-body", // 过滤wiki页面,
"search-input-container", //搜索框
"search-match", //fix搜索结果页,repo name被翻译
"cm-editor", "react-code-lines", //代码编辑框
"PRIVATE_TreeView-item", // 文件树
"repo", // 项目名称
];
const blockTags = ["CODE", "SCRIPT", "LINK", "IMG", "svg", "TABLE", "PRE"];
const blockItemprops = ["name"];
if (blockTags.includes(el.tagName)) {
return false;
}
if (el.id && blockIds.includes(el.id)) {
return false;
}
if (el.classList) {
for (let clazz of blockClass) {
if (el.classList.contains(clazz)) {
return false;
}
}
}
if (el.getAttribute) {
let itemprops = el.getAttribute("itemprop");
if (itemprops) {
itemprops = itemprops.split(" ");
for (let itemprop of itemprops) {
if (blockItemprops.includes(itemprop)) {
return false;
}
}
}
}
return true;
}
function traverseElement(el) {
translateElementAriaLabel(el)
if (!shouldTranslateEl(el)) {
return
}
if (el.childNodes.length === 0) {
if (el.nodeType === Node.TEXT_NODE) {
translateElement(el);
return;
}
else if(el.nodeType === Node.ELEMENT_NODE) {
if (el.tagName === "INPUT") {
translateElement(el);
return;
}
}
}
for (const child of el.childNodes) {
if (child.nodeType === Node.TEXT_NODE) {
translateElement(child);
}
else if(child.nodeType === Node.ELEMENT_NODE) {
if (child.tagName === "INPUT") {
translateElement(child);
} else {
traverseElement(child);
}
} else {
// pass
}
}
}
function watchUpdate() {
const m = window.MutationObserver || window.WebKitMutationObserver;
const observer = new m(function (mutations, observer) {
var reTrans = false;
for(let mutationRecord of mutations) {
if (mutationRecord.addedNodes || mutationRecord.type === 'attributes') {
reTrans = true;
// traverseElement(mutationRecord.target);
}
}
if(reTrans) {
traverseElement(document.body);
translateTime();
}
});
observer.observe(document.body, {
subtree: true,
characterData: true,
childList: true,
attributeFilter: ['value', 'placeholder', 'aria-label', 'data', 'data-confirm'], // 仅观察特定属性变化(试验测试阶段,有问题再恢复)
});
}
// translate "about"
function translateDesc(el) {
$(el).append("<br/>");
$(el).append("<a id='translate-me' href='#' style='color:rgb(27, 149, 224);font-size: small'>翻译</a>");
$("#translate-me").click(function() {
// get description text
const desc = $(el)
.clone()
.children()
.remove()
.end()
.text()
.trim();
if(!desc) {
return;
}
let lang = (navigator.userLanguage || navigator.language).toLowerCase();
let data_json = {
header: {
fn: "auto_translation"
},
type: "plain",
source: {
text_list: [
desc
]
},
target: {
lang: lang == "zh-cn" ? "zh" : lang
}
}
const repoId = $("input[name=repository_id]").val();
GM_xmlhttpRequest({
method: "GET",
url: `https://www.github-zh.com/translate?i=${repoId}&q=`+ encodeURIComponent(desc),
onload: function(rsp) {
if (rsp.status === 200) {
$("#translate-me").hide();
// render result
const text = rsp.responseText;
$(".repository-content .f4").append("<span style='font-size: small'>由 <a target='_blank' style='color:rgb(27, 149, 224);' href='https://www.githubs.cn'>GitHub中文社区</a> 翻译👇</span>");
$(".repository-content .f4").append("<br/>");
$(".repository-content .f4").append(text);
} else {
console.error("仓库描述翻译失败:", rsp)
alert("翻译失败");
}
}
});
});
}
function translateByCssSelector() {
if(locales.css) {
for(var css of locales.css) {
if($(css.selector).length > 0) {
if(css.key === '!html') {
$(css.selector).html(css.replacement);
} else {
$(css.selector).attr(css.key, css.replacement);
}
}
}
}
}
function translateTime() {
$("relative-time").each(function() {
translateRelativeTimeEl(this);
})
}
})();