-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyinglish.js
317 lines (300 loc) · 11.7 KB
/
yinglish.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
'use strict';
let yinglish_node = [];
let yinglish_node_to_translate = [];
let yinglish_node_translated = [];
function yinglish_generate(text, type, yin_number) {
let reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
if(!reg.test(text)){
return text;
}
if (Math.random() > yin_number) {
return text;
}
if ([",", "。", ",", "."].includes(text)) {
return '……' + (Math.random() < 0.5 ? '' : '❤');
}
if (["!", "!"].includes(text)) {
return '❤';
}
if (text.length > 1 && Math.random() < 0.5) {
return text[0] + '……' + text;
} else if ((type & 0x00100000) != 0){
let text_yin = '';
for (let i = 0; i < text.length; i++) {
text_yin += '〇';
}
return '……' + text_yin;
} else if((type & 0x00080000) != 0){
let text_yin = '';
if(text.length <= 1){
return '揪~';
}
for(let i = 0; i < text.length; i += 2){
text_yin += '噗叽';
}
text_yin += '~~~';
return text_yin;
} else if((type & 0x40000000) != 0){
let number = parseInt(Math.random() * 10, 10) % 3;
switch (number) {
case 1:
return '好大❤~~~';
case 2:
return '好硬❤~~~';
default:
return '坏掉了❤~~~';
}
} else if((type & 0x02000000) != 0){
if(Math.random() < 0.5){
return '在里面❤~~~';
} else {
return '在外面❤';
}
}else if((type & 0x00001000) != 0){
if(Math.random() < 0.5){
return '要去了~~~';
} else {
return '一起做❤';
}
}else if((type & 0x00000000) != 0){
return '啊~什么要来了~~~';
}else if((type & 0x00000080) != 0 || (type & 0x00000040) != 0 || (type & 0x00000001) != 0 || (type & 0x00010000) != 0){
return text;
}else {
let text_yin = '哈';
if(text.length <= 2){
return '嗯~';
}
for(let i = 0; i < text.length; i += 2){
text_yin += '啊';
}
text_yin += '~~~';
return text_yin;
}
}
async function yinglish_real_translate() {
const segmentit = Segmentit.useDefault(new Segmentit.Segment());
let total = 0;
for (let key in yinglish_node) {
let value = yinglish_node[key];
walkTheDOM(value, function (node) {
if (node.nodeType === 3) { // Is it a Text node?
var text = node.data.trim();
var parent = node.parentNode;
var parent_parent = parent.parentNode;
if (parent.nodeName != "SCRIPT"
&& parent.nodeName != "NOSCRIPT"
&& parent.nodeName != "STYLE"
&& parent.nodeName != 'TEXTAREA'
&& parent.nodeName != 'PRE'
&& parent.nodeName != 'CODE'
&& (parent_parent != null && parent_parent.nodeName != 'CODE')) {
if (text.length > 0) {
total++;
}
}
}
});
}
let current = 0;
for (let key in yinglish_node) {
let value = yinglish_node[key];
walkTheDOM(value, function (node) {
if (node.nodeType === 3) { // Is it a Text node?
var text = node.data.trim();
var parent = node.parentNode;
var parent_parent = parent.parentNode;
if (parent.nodeName != "SCRIPT"
&& parent.nodeName != "NOSCRIPT"
&& parent.nodeName != "STYLE"
&& parent.nodeName != 'TEXTAREA'
&& parent.nodeName != 'PRE'
&& parent.nodeName != 'CODE'
&& (parent_parent != null && parent_parent.nodeName != 'CODE')) {
if (text.length > 0) {
const result = segmentit.doSegment(text);
let output = '';
for (let i = 0; i < result.length; i++) {
output += yinglish_generate(result[i]["w"], result[i]["p"], 0.4);
}
node.data = output;
current++;
}
}
}
})
yinglish_show_progress(current / total);
yinglish_node_translated[value.id] = value.cloneNode(true);
await sleep(500);
}
yinglish_show_complete(true);
}
function yinglish_show_origin(){
for(let key in yinglish_node){
let value = yinglish_node[key];
value.replaceWith(yinglish_node_to_translate[key]);
yinglish_node[key] = yinglish_node_to_translate[key];
}
yinglish_show_complete(false);
}
function yinglish_show_translated(){
for(let key in yinglish_node){
let value = yinglish_node[key];
value.replaceWith(yinglish_node_translated[key]);
yinglish_node[key] = yinglish_node_translated[key];
}
yinglish_show_complete(true);
}
function yinglish_start_translate(){
if(document.getElementById("segmentit")){
yinglish_real_translate();
return;
}
let new_element = document.createElement("script");
new_element.id = "segmentit";
new_element.setAttribute("type", "text/javascript");
new_element.setAttribute("src", "https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/segmentit.js");
new_element.onload = yinglish_real_translate;
document.body.appendChild(new_element);
}
function yinglish_show_window(){
let yinglish_style = document.createElement("style");
yinglish_style.innerHTML = 'div.translate_language {\n' +
' font-weight: bold;\n' +
' display: inline-block;\n' +
' text-decoration: underline;\n' +
' }\n' +
'\n' +
' div.translate_banner {\n' +
' top: 0px;' +
' left: 0px;' +
' right: 0px;' +
' padding: 1% 0;\n' +
' background-color: #8866FF;\n' +
' color: #FFFFFF;\n' +
' width: 100%;\n' +
' min-height: 30px;\n' +
' text-align: center;\n' +
' z-index: auto;\n' +
' font-family: \'Noto Sans SC\', sans-serif;\n' +
' font-size: large;\n' +
' position: absolute;\n' +
' position: fixed;\n' +
' animation: yinglish_fly 1s ease-out 0s;\n' +
' }\n' +
'\n' +
' @keyframes yinglish_fly {\n' +
' from {\n' +
' opacity: 0;\n' +
' transform: translateY(-50px)\n' +
' }\n' +
' to {\n' +
' opacity: 1;\n' +
' transform: translateY(0px)\n' +
' }\n' +
' }\n' +
'\n' +
' @keyframes yinglish_fly_back {\n' +
' from {\n' +
' opacity: 1;\n' +
' transform: translateY(0px)\n' +
' }\n' +
' to {\n' +
' opacity: 0;\n' +
' transform: translateY(-50px)\n' +
' }\n' +
' }\n' +
'\n' +
' .yinglish_button_positive {\n' +
' margin: 0 0.5% 0 2%;\n' +
' background-color: #FFFFFF;\n' +
' border-width: 0;\n' +
' outline: none;\n' +
' border-radius: 5%;\n' +
' font-family: \'Noto Sans SC\', sans-serif;\n' +
' font-size: medium;\n' +
' }\n' +
'\n' +
' .yinglish_button_negative {\n' +
' background: transparent; /*按钮背景透明*/\n' +
' border-width: 0; /*边框透明*/\n' +
' outline: none; /*点击后没边框*/\n' +
' text-decoration: underline;\n' +
' color: #FFFFFF;\n' +
' padding: 0 5px 0 5px;\n' +
' }';
document.head.appendChild(yinglish_style);
let yinglish_container = document.createElement("div");
yinglish_container.className = "translate_banner";
yinglish_container.id = "yinglish_container";
yinglish_container.innerHTML = '网页翻译 将 <div class="translate_language">简体中文</div>\n' +
' 翻译成\n' +
' <div class="translate_language">淫语</div>\n' +
' <button id="yinglish_button_yes" class="yinglish_button_positive">翻译</button>\n' +
' <button id="yinglish_button_no" class="yinglish_button_negative">不用了,谢谢</button>\n' ;
yinglish_container.onanimationstart = function () {
console.log(yinglish_container.offsetHeight);
document.documentElement.style.marginTop = yinglish_container.offsetHeight + 1 +'px';
}
document.body.insertBefore(yinglish_container, document.body.firstChild);
document.getElementById("yinglish_button_yes").addEventListener("click", function () {
yinglish_start_translate();
});
document.getElementById("yinglish_button_no").addEventListener("click", function () {
yinglish_container.onanimationstart = null;
yinglish_container.style.animation = 'yinglish_fly_back 1s ease-out 0s';
yinglish_container.onanimationend = function () {
yinglish_container.style.display = 'none';
}
document.documentElement.style.marginTop = 0 + 'px';
});
}
function yinglish_show_progress(progress){
document.getElementById("yinglish_container").innerHTML = '网页翻译 已完成:' + parseInt(progress * 99, 10) + '%......';
}
function yinglish_show_complete(showing_translated){
document.getElementById("yinglish_container").innerHTML = '网页翻译 已完成 <div class="translate_language">简体中文</div>\n' +
' 到\n' +
' <div class="translate_language">淫语</div>\n';
let yinglish_switch_action = (showing_translated ? yinglish_show_origin : yinglish_show_translated);
let yinglish_switch_button = document.createElement("button");
yinglish_switch_button.id = "yinglish_button_switch";
yinglish_switch_button.className = "yinglish_button_positive";
yinglish_switch_button.innerText = (showing_translated ? '显示原文' : '显示译文');
yinglish_switch_button.addEventListener("click", function () {
yinglish_switch_action();
});
document.getElementById("yinglish_container").appendChild(yinglish_switch_button);
}
function yinglish_setup(){
document.head.innerHTML = document.head.innerHTML + '<link rel="preconnect" href="https://fonts.googleapis.com">\n' +
' <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>\n' +
' <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@500&display=swap" rel="stylesheet">';
let index = 0;
var el = document.body.firstChild;
while(el!==null){
if(!el.id){
el.id = "yinglish_" + index;
}
yinglish_node[el.id] = el;
yinglish_node_to_translate[el.id] = el.cloneNode(true);
index++;
el=el.nextSibling;
}
yinglish_show_window();
}
/**
* DOM遍历
*/
function walkTheDOM(node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay))
setTimeout(function(){
yinglish_setup();
}, 1000);