-
Notifications
You must be signed in to change notification settings - Fork 2
/
core.js
242 lines (225 loc) · 7.79 KB
/
core.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
/*
@name : 锅巴汉化 - Web汉化插件
@author : 麦子、JAR、小蓝、好阳光的小锅巴
@version : V0.6.1 - 2019-07-09
@website : http://www.g8hh.com
*/
var CNITEM_DEBUG = 0;
function cnItemByTag(text, itemgroup, node, textori){
for (let i in itemgroup){
if (i[0] == '.') { //匹配节点及其父节点的class
let current_node = node;
while (current_node){
if ( current_node.classList && current_node.classList.contains(i.substr(1)) ){
return itemgroup[i];
}
else if( current_node.parentElement && current_node.parentElement != document.documentElement ) {
current_node = current_node.parentElement;
}
else {
break;
}
}
}
else if (i[0] == '#'){ //匹配节点及其父节点的id
let current_node = node;
while (current_node){
if ( current_node.id == i.substr(1) ){
return itemgroup[i];
}
else if( current_node.parentElement && current_node.parentElement != document.documentElement ) {
current_node = current_node.parentElement;
}
else {
break;
}
}
}
else if (i[0] == '$'){ //执行document.querySelector
if (document.querySelector(i.substr(1)) != null){
return itemgroup[i];
}
}
else if (i[0] == '*'){ //搜索原始文本
if ( textori.includes(i.substr(1)) ){
return itemgroup[i];
}
}
// and more ...
else{
CNITEM_DEBUG && console.log({text, itemgroup, dsc:"不识别的标签" + i})
}
}
return null;
}
//2.采集新词
//20190320@JAR rewrite by 麦子
var cnItem = function (text, node) {
if (typeof (text) != "string")
return text;
let textori = text;
//处理前缀
let text_prefix = "";
for (let prefix in cnPrefix) {
if (text.substr(0, prefix.length) === prefix) {
text_prefix += cnPrefix[prefix];
text = text.substr(prefix.length);
}
}
//处理后缀
let text_postfix = "";
for (let postfix in cnPostfix) {
if (text.substr(-postfix.length) === postfix) {
text_postfix = cnPostfix[postfix] + text_postfix;
text = text.substr(0, text.length - postfix.length);
}
}
//处理正则后缀
let text_reg_exclude_postfix = "";
for (let reg of cnExcludePostfix) {
let result = text.match(reg);
if (result) {
text_reg_exclude_postfix = result[0] + text_reg_exclude_postfix;
text = text.substr(0, text.length - result[0].length);
}
}
//检验字典是否可存
if (!cnItems._OTHER_) cnItems._OTHER_ = [];
//检查是否排除
for (let reg of cnExcludeWhole) {
if (reg.test(text)) {
return text_prefix + text + text_reg_exclude_postfix + text_postfix;;
}
}
//尝试正则替换
for (let [key, value] of cnRegReplace.entries()) {
if (key.test(text)) {
return text_prefix + text.replace(key, value) + text_reg_exclude_postfix + text_postfix;
}
}
//遍历尝试匹配
for (let i in cnItems) {
//字典已有词汇或译文、且译文不为空,则返回译文
if (typeof(cnItems[i]) == "string" && (text == i || text == cnItems[i])){
return text_prefix + cnItems[i] + text_reg_exclude_postfix + text_postfix;
} else if ( typeof(cnItems[i]) == "object" && text == i ){
let result = cnItemByTag(i, cnItems[i], node, textori);
if (result != null){
return text_prefix + result + text_reg_exclude_postfix + text_postfix;
} else {
CNITEM_DEBUG && console.log({text:i, cnitem:cnItems[i], node});
}
} else {
// continue;
}
}
//调整收录的词条,0=收录原文,1=收录去除前后缀的文本
let save_cfg = 1;
let save_text = save_cfg ? text : textori;
//遍历生词表是否收录
for (
let i = 0; i < cnItems._OTHER_.length; i++
) {
//已收录则直接返回
if (save_text == cnItems._OTHER_[i])
return text_prefix + text + text_reg_exclude_postfix + text_postfix;
}
if (cnItems._OTHER_.length < 1000) {
//未收录则保存
cnItems._OTHER_.push(save_text);
cnItems._OTHER_.sort(
function (a, b) {
return a.localeCompare(b)
}
);
}
//开启生词打印
CNITEM_DEBUG && console.log(
'有需要汉化的英文:', text
);
//返回生词字串
return text_prefix + text + text_reg_exclude_postfix + text_postfix;
};
transTaskMgr = {
tasks: [],
addTask: function (node, attr, text) {
this.tasks.push({
node,
attr,
text
})
},
doTask: function () {
let task = null;
while (task = this.tasks.pop())
task.node[task.attr] = task.text;
},
}
function TransSubTextNode(node) {
if (node.childNodes.length > 0) {
for (let subnode of node.childNodes) {
if (subnode.nodeName === "#text") {
let text = subnode.textContent;
let cnText = cnItem(text, subnode);
cnText !== text && transTaskMgr.addTask(subnode, 'textContent', cnText);
//console.log(subnode);
} else if (subnode.nodeName !== "SCRIPT" && subnode.nodeName !== "STYLE" && subnode.nodeName !== "TEXTAREA") {
if (!subnode.childNodes || subnode.childNodes.length == 0) {
let text = subnode.innerText;
let cnText = cnItem(text, subnode);
cnText !== text && transTaskMgr.addTask(subnode, 'innerText', cnText);
//console.log(subnode);
} else {
TransSubTextNode(subnode);
}
} else {
// do nothing;
}
}
}
}
! function () {
console.log("加载汉化模块");
let observer_config = {
attributes: false,
characterData: true,
childList: true,
subtree: true
};
let targetNode = document.body;
//汉化静态页面内容
TransSubTextNode(targetNode);
transTaskMgr.doTask();
//监听页面变化并汉化动态内容
let observer = new MutationObserver(function (e) {
//window.beforeTransTime = performance.now();
observer.disconnect();
for (let mutation of e) {
if (mutation.target.nodeName === "SCRIPT"|| mutation.target.nodeName === "STYLE" || mutation.target.nodeName === "TEXTAREA") continue;
if (mutation.target.nodeName === "#text") {
mutation.target.textContent = cnItem(mutation.target.textContent, mutation.target);
} else if (!mutation.target.childNodes || mutation.target.childNodes.length == 0) {
mutation.target.innerText = cnItem(mutation.target.innerText, mutation.target);
} else if (mutation.addedNodes.length > 0) {
for (let node of mutation.addedNodes) {
if (node.nodeName === "#text") {
node.textContent = cnItem(node.textContent, node);
//console.log(node);
} else if (node.nodeName !== "SCRIPT" && node.nodeName !== "STYLE" && node.nodeName !== "TEXTAREA") {
if (!node.childNodes || node.childNodes.length == 0) {
if (node.innerText)
node.innerText = cnItem(node.innerText, node);
} else {
TransSubTextNode(node);
transTaskMgr.doTask();
}
}
}
}
}
observer.observe(targetNode, observer_config);
//window.afterTransTime = performance.now();
//console.log("捕获到页面变化并执行汉化,耗时" + (afterTransTime - beforeTransTime) + "毫秒");
});
observer.observe(targetNode, observer_config);
}();