This repository has been archived by the owner on Sep 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
InPageTranslation.js
238 lines (211 loc) · 8.23 KB
/
InPageTranslation.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
// eslint-disable-next-line no-unused-vars
class InPageTranslation {
constructor(mediator) {
this.translationsCounter = 0;
this.mediator = mediator;
this.started = false;
this.loadTagsSet()
this.viewportNodeMap = new Map();
this.hiddenNodeMap = new Map();
this.nonviewportNodeMap = new Map();
this.updateMap = new Map();
this.updateTimeout = null;
this.UI_UPDATE_INTERVAL = 500;
this.messagesSent = new Set();
}
loadTagsSet() {
// set of element types we want to translate
this.tagsSet = new Set();
this.tagsSet.add("div");
this.tagsSet.add("p");
this.tagsSet.add("span");
this.tagsSet.add("#text");
this.tagsSet.add("i");
this.tagsSet.add("a");
this.tagsSet.add("b");
this.tagsSet.add("h3");
this.tagsSet.add("h2");
this.tagsSet.add("h1");
this.tagsSet.add("h4");
this.tagsSet.add("label");
this.tagsSet.add("body");
this.tagsSet.add("li");
this.tagsSet.add("ul");
this.tagsSet.add("td");
}
start() {
/*
* start the dom parser, the DOM mutation observer and request the
* title to be translated
*/
this.started = true;
const pageTitle = document.getElementsByTagName("title")[0];
if (pageTitle) {
this.queueTranslation(pageTitle);
}
this.startTreeWalker(document.body);
this.startMutationObserver();
}
startTreeWalker(root) {
const acceptNode = node => {
return this.validateNode(node);
}
const nodeIterator = document.createNodeIterator(
root,
// eslint-disable-next-line no-bitwise
NodeFilter.SHOW_TEXT,
acceptNode
);
let currentNode;
// eslint-disable-next-line no-cond-assign
while (currentNode = nodeIterator.nextNode()) {
// console.log('startTreeWalker - root:', root, 'currentnode', currentNode, 'nodehidden:', this.isElementHidden(currentNode.parentNode), 'nodeinViewPort:', this.isElementInViewport(currentNode.parentNode), 'nodeType:', currentNode.nodeType, 'tagName:', currentNode.tagName, 'content:', currentNode.innerHTML, 'wholeText:', currentNode.wholeText.trim());
this.queueTranslation(currentNode);
}
this.dispatchTranslations();
}
isElementInViewport(element) {
const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
isElementHidden(element) {
return element.style.display === "none" || element.style.visibility === "hidden" || element.offsetParent === null;
}
validateNode(node) {
if (node.nodeType === 3) {
if (this.tagsSet.has(node.parentNode.nodeName.toLowerCase()) &&
node.textContent.trim().length > 0) {
return NodeFilter.FILTER_ACCEPT;
}
return NodeFilter.FILTER_REJECT;
}
return this.tagsSet.has(node.nodeName.toLowerCase())
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_REJECT;
}
queueTranslation(node) {
/*
* let's store the node to keep its reference
* and send it to the translation worker
*/
this.translationsCounter += 1;
// let's categorize the elements on their respective hashmaps
if (this.isElementHidden(node.parentNode)) {
// if the element is entirely hidden
this.hiddenNodeMap.set(this.translationsCounter, node);
} else if (this.isElementInViewport(node.parentNode)) {
// if the element is present in the viewport
this.viewportNodeMap.set(this.translationsCounter, node);
} else {
// if the element is visible but not present in the viewport
this.nonviewportNodeMap.set(this.translationsCounter, node);
}
}
dispatchTranslations() {
// we then submit for translation the elements in order of priority
this.processingNodeMap = "viewportNodeMap";
this.viewportNodeMap.forEach(this.submitTranslation, this);
this.processingNodeMap = "nonviewportNodeMap";
this.nonviewportNodeMap.forEach(this.submitTranslation, this);
this.processingNodeMap = "hiddenNodeMap";
this.hiddenNodeMap.forEach(this.submitTranslation, this);
}
submitTranslation(node, key) {
if (this.messagesSent.has(key)) {
// if we already sent this message, we just skip it
return;
}
const text = node.textContent;
if (text.trim().length) {
/*
* send the content back to mediator in order to have the translation
* requested by it
*/
const payload = {
text,
type: "inpage",
attrId: [
this.processingNodeMap,
key
],
};
this.notifyMediator("translate", payload);
this.messagesSent.add(key);
}
}
notifyMediator(command, payload) {
this.mediator.contentScriptsMessageListener(this, { command, payload });
}
startMutationObserver() {
// select the node that will be observed for mutations
const targetNode = document;
// options for the observer (which mutations to observe)
const config = { attributes: true, childList: true, subtree: true };
// callback function to execute when mutations are observed
const callback = function(mutationsList) {
for (const mutation of mutationsList) {
if (mutation.type === "childList") {
// console.log(mutation);
mutation.addedNodes.forEach(node => this.startTreeWalker(node));
}
}
}.bind(this);
// create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// start observing the target node for configured mutations
observer.observe(targetNode, config);
}
mediatorNotification(translationMessage) {
/*
* notification received from the mediator with our request.
* the only possible notification can be a translation response,
* so let's schedule the update of the original node with its new content
*/
this.enqueueElement(translationMessage);
}
updateElements() {
const updateElement = (translatedText, node) => {
// console.log("translate from", node.textContent, " to ", translatedText);
node.textContent = translatedText;
}
this.updateMap.forEach(updateElement);
this.updateMap.clear();
this.updateTimeout = null;
}
enqueueElement(translationMessage) {
const [
hashMapName,
idCounter
] = translationMessage.attrId;
const translatedText = translationMessage.translatedParagraph;
console.log("no enqueue", translatedText);
let targetNode = null;
switch (hashMapName) {
case "hiddenNodeMap":
targetNode = this.hiddenNodeMap.get(idCounter);
this.hiddenNodeMap.delete(idCounter);
break;
case "viewportNodeMap":
targetNode = this.viewportNodeMap.get(idCounter);
this.viewportNodeMap.delete(idCounter);
break;
case "nonviewportNodeMap":
targetNode = this.nonviewportNodeMap.get(idCounter);
this.nonviewportNodeMap.delete(idCounter);
break;
default:
break;
}
this.messagesSent.delete(idCounter);
this.updateMap.set(targetNode, translatedText);
// we finally schedule the UI update
if (!this.updateTimeout) {
this.updateTimeout = setTimeout(this.updateElements.bind(this),this.UI_UPDATE_INTERVAL);
}
}
}