-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbackground.js
330 lines (281 loc) · 10.3 KB
/
background.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
318
319
320
321
322
323
324
325
326
327
328
329
330
(function () {
var mutationIdx = 0;
const MUTATION_UPDATE_STEP = 2;
const cache = new Map();
const ORIGINAL_TRANSLATIONS = [
"original", // English
"оригинал", // Russian
"オリジナル", // Japanese
"原始", // Chinese
"원본", // Korean
"origineel", // Dutch
"original", // Spanish/Portuguese
"originale", // Italian/French
"original", // German
"oryginał", // Polish
"původní", // Czech
"αρχικό", // Greek
"orijinal", // Turkish
"原創", // Traditional Chinese
"gốc", // Vietnamese
"asli", // Indonesian
"מקורי", // Hebrew
"أصلي", // Arabic
];
function makeHttpObject() {
try {
return new XMLHttpRequest();
} catch (error) {}
try {
return new ActiveXObject("Msxml2.XMLHTTP");
} catch (error) {}
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch (error) {}
throw new Error("Could not create HTTP request object.");
}
function makeLinksClickable(html) {
return html.replace(
/(https?:\/\/[^\s]+)/g,
"<a rel='nofollow' target='_blank' dir='auto' class='yt-simple-endpoint style-scope yt-formatted-string' href='$1'>$1</a>"
);
}
function get(url, callback) {
if (cache.has(url)) {
callback(cache.get(url));
return;
}
var request = makeHttpObject();
request.open("GET", url, true);
request.send(null);
request.onreadystatechange = function () {
if (request.readyState == 4) {
cache.set(url, request);
callback(request);
}
};
}
function trimYoutube(title) {
return title.replace(/ - YouTube$/, "");
}
function setTitleNode(text, afterNode) {
if (document.getElementById("yt-anti-translate-fake-node")) {
const node = document.getElementById("yt-anti-translate-fake-node");
node.textContent = text;
return;
}
const node = document.createElement("span");
node.className = "style-scope ytd-video-primary-info-renderer";
node.id = "yt-anti-translate-fake-node";
node.textContent = text;
afterNode.after(node);
}
function untranslateCurrentVideo() {
let translatedTitleElement = document.querySelector(
"#title > h1 > yt-formatted-string"
);
if (!translatedTitleElement || !translatedTitleElement.textContent) {
translatedTitleElement = document.querySelector(
"h1 > yt-formatted-string:not(.cbCustomTitle)"
);
}
// that means we cannot find youtube title, so there is nothing we can do
if (!translatedTitleElement || !translatedTitleElement.textContent) {
return;
}
get(
"https://www.youtube.com/oembed?url=" + document.location.href,
function (response) {
if (response.status !== 200) {
return;
}
const realTitle = JSON.parse(response.responseText).title;
if (!realTitle || !translatedTitleElement) {
// Do nothing if video is not loaded yet
return;
}
document.title = document.title.replace(
translatedTitleElement.textContent,
realTitle
);
if (realTitle === translatedTitleElement.textContent) {
// Do not revert already original videos
return;
}
// untranslate video by creating its copy
translatedTitleElement.style.visibility = "hidden";
translatedTitleElement.style.display = "none";
console.log(
`[YoutubeAntiTranslate] translated title to "${realTitle}"`
);
setTitleNode(realTitle, translatedTitleElement);
// translatedTitleElement.textContent = realTitle;
// translatedTitleElement.removeAttribute('is-empty');
// translatedTitleElement.untranslatedByExtension = true;
}
);
// disabled bugged description untranslation
// const translatedDescriptions = [document.querySelector("#description .ytd-video-secondary-info-renderer"), document.getElementById('description-inline-expander')];
// let realDescription = null;
// // For description, try ytInitialPlayerResponse object Youtube creates whenever you open video page, if it is for this video
// if (!window.ytInitialPlayerResponse) {
// return;
// }
// if (window.ytInitialPlayerResponse.videoDetails && window.ytInitialPlayerResponse.videoDetails.title === realTitle) {
// realDescription = window.ytInitialPlayerResponse.videoDetails.shortDescription;
// } else {
// for (const translatedDescription of translatedDescriptions) {
// if (translatedDescription.firstChild.id === FIRST_CHILD_DESC_ID) {
// translatedDescription.removeChild(translatedDescription.firstChild);
// }
// }
// }
// console.log(realDescription, translatedDescriptions);
// if (realDescription) {
// // for (const translatedDescription of translatedDescriptions) {
// // const div = document.createElement('div');
// // div.innerHTML = makeLinksClickable(realDescription) + "\n\n<b>TRANSLATED (added by <a class='yt-simple-endpoint style-scope yt-formatted-string' href='https://chrome.google.com/webstore/detail/youtube-anti-translate/ndpmhjnlfkgfalaieeneneenijondgag?hl=ru'>Youtube Anti Translate</a>):</b>\n";
// // div.id = FIRST_CHILD_DESC_ID;
// // translatedDescription.insertBefore(div, translatedDescription.firstChild);
// // }
// }
}
function untranslateOtherVideos() {
function untranslateArray(otherVideos) {
for (let i = 0; i < otherVideos.length; i++) {
let video = otherVideos[i];
// video was deleted
if (!video) {
return;
}
let videoThumbnail = video.querySelector("a#thumbnail");
// false positive result detected
if (!videoThumbnail) {
continue;
}
let videoId = videoThumbnail.href;
if (
!video.untranslatedByExtension ||
video.untranslatedKey !== videoId
) {
// do not request same video multiply times
let href = video.querySelector("a");
video.untranslatedByExtension = true;
video.untranslatedKey = videoId;
get(
"https://www.youtube.com/oembed?url=" + href.href,
function (response) {
if (response.status !== 200) {
return;
}
const title = JSON.parse(response.responseText).title;
const titleElement = video.querySelector(
"#video-title:not(.cbCustomTitle)"
);
if (title !== titleElement.innerText) {
console.log(
`[YoutubeAntiTranslate] translated from "${titleElement.innerText}" to "${title}"`
);
if (titleElement) {
video.querySelector(
"#video-title:not(.cbCustomTitle)"
).innerText = title;
video.querySelector(
"#video-title:not(.cbCustomTitle)"
).title = title;
if (
video.querySelector(
"a#video-title-link:not(.cbCustomTitle)"
)
) {
// home page
video.querySelector(
"a#video-title-link:not(.cbCustomTitle)"
).title = title;
}
}
}
}
);
}
}
}
untranslateArray(document.querySelectorAll("ytd-video-renderer"));
untranslateArray(document.querySelectorAll("ytd-rich-item-renderer"));
untranslateArray(document.querySelectorAll("ytd-compact-video-renderer"));
untranslateArray(document.querySelectorAll("ytd-grid-video-renderer"));
untranslateArray(document.querySelectorAll("ytd-playlist-video-renderer"));
untranslateArray(
document.querySelectorAll("ytd-playlist-panel-video-renderer")
);
// let compactVideos = document.getElementsByTagName('ytd-compact-video-renderer'); // related videos
// let normalVideos = document.getElementsByTagName('ytd-video-renderer'); // channel page videos
// let gridVideos = document.getElementsByTagName('ytd-grid-video-renderer'); // grid page videos
// untranslateArray(compactVideos);
// untranslateArray(normalVideos);
// untranslateArray(gridVideos);
}
function getOriginalTrack(tracks) {
if (!tracks || !Array.isArray(tracks)) {
return null;
}
// First find which field contains the name property
let languageFieldName = null;
for (const track of tracks) {
if (!track || typeof track !== "object") continue;
for (const [fieldName, field] of Object.entries(track)) {
if (field && typeof field === "object" && field.name) {
languageFieldName = fieldName;
break;
}
}
if (languageFieldName) break;
}
if (!languageFieldName) {
return;
}
// Now find track with "original" in any language
for (const track of tracks) {
if (!track || !track[languageFieldName] || !track[languageFieldName].name)
continue;
const trackName = track[languageFieldName].name.toLowerCase();
for (const originalWord of ORIGINAL_TRANSLATIONS) {
if (trackName.includes(originalWord.toLowerCase())) {
return track;
}
}
}
}
function untranslateAudioTrack() {
const player = document.querySelector("#movie_player");
if (
!player ||
!player.getAvailableAudioTracks ||
player.audioUntranslated
) {
return;
}
const tracks = player.getAvailableAudioTracks();
const originalTrack = getOriginalTrack(tracks);
if (originalTrack) {
player.setAudioTrack(originalTrack);
player.audioUntranslated = true;
}
}
function untranslate() {
if (mutationIdx % MUTATION_UPDATE_STEP == 0) {
untranslateCurrentVideo();
untranslateOtherVideos();
}
mutationIdx++;
}
function run() {
// Change current video title and description
// Using MutationObserver as we can't exactly know the moment when YT js will load video title
const target = document.body;
const config = { childList: true, subtree: true };
const observer = new MutationObserver(untranslate);
observer.observe(target, config);
}
run();
})();