-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathimmersive_translation.js
143 lines (123 loc) · 5.18 KB
/
immersive_translation.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
(function() {
function getTextNodes(node, nodes = []) {
if (isHeaderOrFooter(node) || isHideNode(node)) {
} else if (node.nodeType === Node.TEXT_NODE && node.textContent.trim()) {
nodes.push(node);
} else if (node.tagName === 'P' ||
node.tagName == "PRE" ||
node.tagName == "A") {
nodes.push(node);
} else {
for (const child of node.childNodes) {
getTextNodes(child, nodes);
}
}
return nodes;
}
function isHeaderOrFooter(node) {
if (!node || !node.classList) {
return false;
}
return node.tagName === "HEADER" || node.tagName === "FOOTER" || node.classList.contains('header') || node.classList.contains('footer');
}
function isHideNode(node) {
if (!node || !node.classList) {
return false;
}
var pageUrl = window.location.href;
if (pageUrl.startsWith("https://www.reddit.com")) {
if ((node.hasAttribute("data-adclicklocation") && node.getAttribute("data-adclicklocation") === "top_bar") ||
(node.hasAttribute("data-testid") && node.getAttribute("data-testid") === "post-comment-header") ||
node.id === 'CommentSort--SortPicker'
) {
return true;
}
} else if (pageUrl.startsWith("https://world.hey.com/dhh/")) {
if (node.classList.contains('bio') ||
node.classList.contains('push_double--top') ||
node.classList.contains('push_half--bottom')
) {
return true;
}
}
return false;
}
function isNumeric(str) {
return /^\d+$/.test(str);
}
function checkString(input) {
const regex = /^[\d.,!?;:-\s]+$/
return regex.test(input);
}
function getPageNodes() {
var pageUrl = window.location.href;
if (pageUrl.startsWith("https://github.com")) {
if (document.querySelector("readme-toc")) {
return getTextNodes(document.querySelector("readme-toc"));
} else if (document.querySelector(".application-main")) {
return getTextNodes(document.querySelector(".application-main"));
} else {
return getTextNodes(document.body);
}
} else if (pageUrl.startsWith("https://www.reddit.com")) {
if (document.querySelector('[data-testid="post-container"]')) {
var containerNode = document.querySelector('[data-testid="post-container"]');
return getTextNodes(containerNode.parentNode);
} else {
return getTextNodes(document.body);
}
} else if (pageUrl.startsWith("https://gitlab.com")) {
if (document.querySelector("article")) {
return getTextNodes(document.querySelector("article"));
}
} else {
return getTextNodes(document.body);
}
}
function addTranslations() {
const textNodes = getPageNodes();
var pageUrl = window.location.href;
let index = 0;
let nodeTexts = [];
for (const textNode of textNodes) {
const textContent = textNode.textContent;
const text = textContent.trim();
if (text.length === 0 ||
text.length === 1 ||
checkString(textContent) ||
(["nil"].includes(text)) ||
textNode.parentNode.tagName === 'CODE' ||
textNode.parentNode.tagName === 'PRE' ||
textNode.parentNode.tagName === 'BUTTON') {
continue;
}
if (pageUrl.startsWith("https://www.reddit.com") &&
(isNumeric(textContent) ||
textContent.startsWith("/r/") ||
textContent.startsWith("/u/") ||
textContent.startsWith("r/") ||
textContent.startsWith("u/") ||
textContent.startsWith("level ") ||
textContent.endsWith(" ago") ||
(["give award", "award", "share", "reply", "cc", "comment as", "posted by", "op",
"report", "save", "follow", "markdown mode", "continue this thread"].includes(text.toLowerCase())) ||
(textNode.className && textNode.className.includes("button")) ||
(textNode.className && textNode.className.includes("icon-comment"))
)) {
continue;
}
const translatedText = "eaf-translated-node-" + index;
const translatedTextNode = document.createTextNode("");
const translatedNode = document.createElement("div");
translatedNode.appendChild(translatedTextNode);
translatedNode.classList.add("eaf-translated");
translatedNode.classList.add(translatedText);
textNode.after(translatedNode);
nodeTexts.push(textContent);
index++;
}
console.log("##### ", nodeTexts);
return nodeTexts;
}
return addTranslations();
})();