-
Notifications
You must be signed in to change notification settings - Fork 13
/
background.js
124 lines (110 loc) · 3.5 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
//author= Mücahit Sahin
//github=https://github.com/mucahit-sahin
async function openBilingual() {
let tracks = document.getElementsByTagName("track");
let en;
let tr;
if (tracks.length) {
for (let i = 0; i < tracks.length; i++) {
if (tracks[i].srclang === "en") {
en = tracks[i];
}
}
if (en) {
en.track.mode = "showing";
await sleep(500);
let cues = en.track.cues;
//İngilizce yazıdaki cümle bitiş anlarının tespiti.
// .....cümle bitti. Yeni cümle
//Burada sadece nokta karakterinden sonra boşluk olan durumlarda cümlenin bittiği varsayılmıştır.
// 75.3 , model.fit gibi özel ifade belirten durumlarda noktayı cümle bitişi olarak algılamaması için.
var endSentence = [];
for (let i = 0; i < cues.length; i++) {
for (let j = 0; j < cues[i].text.length; j++) {
if (cues[i].text[j] == "." && cues[i].text[j + 1] == undefined) {
endSentence.push(i);
}
}
}
///////////////////////
var cuesTextList = getTexts(cues);
getTranslation(cuesTextList, (translatedText) => {
var translatedList = translatedText.split(" z~~~z");
translatedList.splice(-1, 1);
for (let i = 0; i < endSentence.length; i++) {
if (i != 0) {
for (let j = endSentence[i - 1] + 1; j <= endSentence[i]; j++) {
cues[j].text = translatedList[i];
//console.log(translatedList[i])
}
} else {
for (let j = 0; j <= endSentence[i]; j++) {
cues[j].text = translatedList[i];
//console.log(translatedList[i])
}
}
}
});
}
}
}
String.prototype.replaceAt = function (index, replacement) {
return (
this.substr(0, index) +
replacement +
this.substr(index + replacement.length)
);
};
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function getTexts(cues) {
let cuesTextList = "";
for (let i = 0; i < cues.length; i++) {
/*for(let j=0;j<cues[i].text.length;j++)
{
if(cues[i].text[j] == '.' && cues[i].text[j+1] == ' ')
{
cues[i].text = cues[i].text.replaceAt(j, ",")
}
}*/
if (cues[i].text[cues[i].text.length - 1] == ".") {
cues[i].text = cues[i].text.replaceAt(
cues[i].text.length - 1,
". z~~~z "
);
//console.log(cues[i].text.replaceAt(cues[i].text.length-1, ". z~~~z "))
}
cuesTextList += cues[i].text.replace(/\n/g, " ") + " ";
}
return cuesTextList;
}
function getTranslation(words, callback) {
chrome.storage.sync.get(["lang"], function (result) {
var lang = result.lang;
const xhr = new XMLHttpRequest();
let url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=${lang}&dt=t&q=${encodeURI(
words
)}`;
xhr.open("GET", url, true);
xhr.responseType = "text";
xhr.onload = function () {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200 || xhr.status === 304) {
const translatedList = JSON.parse(xhr.responseText)[0];
let translatedText = "";
for (let i = 0; i < translatedList.length; i++) {
translatedText += translatedList[i][0];
}
callback(translatedText);
}
}
};
xhr.send();
});
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.method == "translate") {
openBilingual();
}
});