-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinline-translator.user.js
More file actions
208 lines (170 loc) · 4.12 KB
/
Copy pathinline-translator.user.js
File metadata and controls
208 lines (170 loc) · 4.12 KB
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
// ==UserScript==
// @id Inline-Translator
// @name Inline-Translator
// @version 0.5
// @namespace 12425
// @author 12425
// @description 在每个段落之后加上中文翻译
// @license MIT
// @downloadURL https://github.com/12425/Inline-Translate.user.js/raw/main/inline-translator.user.js
// @include http://*
// @include https://*
// @run-at document-idle
// @grant GM_registerMenuCommand
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
GM_registerMenuCommand("翻译成双语网页", () => {
const root = document.body;
if (root.classList.contains('_myscript_translated')) {
return;
}
root.classList.add('_myscript_translated');
translate(root);
});
const TranslationTags = new Set([
'blockquote',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'li',
'ol',
'p',
]);
const TranslationClass = new Set([
'comment',
]);
const DeleteTags = new Array([
'audio',
'iframe',
'img',
'meta',
'picture',
'script',
'svg',
'video',
]);
const SkipTags = new Array([
'code',
'pre',
]);
const EmptyTags = new Set([
'br',
'hr',
]);
function fetch(text, callback, arg1, arg2, arg3) {
GM_xmlhttpRequest({
method: 'GET',
url: `https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=zh&dt=t&q=${encodeURIComponent(text)}`,
onload: function(resp) {
let json = null;
try {
json = JSON.parse(resp.responseText)[0];
} catch {
return;
}
if (!json) {
return;
}
const data = json.map(item => {
return item[0].trim();
}).join('');
if (text !== data) {
callback(data, arg1, arg2, arg3);
}
},
});
}
function simplify(node, attrs, values) {
// return false if node is removed
node.querySelectorAll(DeleteTags.join(',')).forEach(el => {
el.remove();
});
if (!removeEmptyNode(node)) {
return false;
}
attrs.push(extractAttrs(node));
node.querySelectorAll('*').forEach(el => {
attrs.push(extractAttrs(el));
});
node.querySelectorAll(SkipTags.join(',')).forEach(el => {
values.push(el.innerHTML);
el.innerHTML = '';
});
node.innerHTML = node.innerHTML.replace(/\s*(\n\s*)+\s*/g, ' ');
return true;
}
function removeEmptyNode(node) {
// return false if node is removed
const children = node.querySelectorAll('*');
if (children.length) {
let toDelete = true;
for (const child of children) {
if (removeEmptyNode(child)) {
toDelete = false;
}
}
if (!toDelete) {
return true;
}
}
if (EmptyTags.has(node.tagName)) {
return true;
}
if (!node.innerHTML.trim().length) {
node.remove();
return false;
}
return true;
}
function extractAttrs(node) {
let attr = {};
Array.from(node.attributes).forEach(attr => {
attr[attr.name] = attr.value;
node.removeAttribute(attr.name);
});
return attr;
}
function resumeAttrs(node, attrs) {
Object.keys(attrs).forEach(key => {
node.setAttribute(key, attrs[key]);
});
}
function updateNode(data, node, attrs, values) {
let newNode = document.createElement(node.tagName);
newNode.innerHTML = data;
node.insertAdjacentElement('afterend', newNode);
resumeAttrs(newNode, attrs[0]);
node.querySelectorAll('*').forEach((el, i) => {
resumeAttrs(el, attrs[i]);
});
node.querySelectorAll(SkipTags.join(',')).forEach((el, i) => {
el.innerHTML = values[i];
});
if (node.parentNode.tagName !== 'SMALL') {
const smallNode = document.createElement('small');
node.insertAdjacentElement('afterend', smallNode);
smallNode.appendChild(node);
}
}
function translate(node) {
const tag = node.tagName.toLowerCase();
if (!tag) {
return;
}
if (!TranslationTags.has(tag) && !Array.from(node.classList).some(c => TranslationClass.has(c))) {
Array.from(node.children).forEach(translate);
return;
}
let newNode = node.cloneNode(true);
let attrs = [];
let values = [];
if (simplify(newNode, attrs, values)) {
fetch(newNode.innerHTML, updateNode, node, attrs, values);
}
}
})();