-
Notifications
You must be signed in to change notification settings - Fork 9
/
content.js
234 lines (195 loc) · 7.82 KB
/
content.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
var gLastUrl = '';
function getPOSInformation(){
var selector = $('.gt-cd.gt-cd-mmd > .gt-cd-c > .gt-cd-pos');
var posIndexs = [];
var posList = [];
var posIndex = 0;
for (var i = 0; i < selector.length; i++) {
var currentSelector = selector.eq(i);
posIndex += currentSelector.next('.gt-def-list').children().length;
var pos = currentSelector.text();
posIndexs.push(posIndex);
posList.push(pos);
}
return {posIndexs: posIndexs, posList: posList};
}
function getDefinitionInformation(){
var selector = $('.gt-def-info');
var text = [];
var examples = [];
var synonyms = [];
for (var i = 0; i < selector.length; i++) {
var currentSelector = selector.eq(i);
text.push(currentSelector.children('.gt-def-row').text());
var example = currentSelector.children('.gt-def-example').text();
if (example !== '') {
example = '"' + example + '"';
}
examples.push(example);
synonym = currentSelector.children('.gt-def-synonym').text();
synonyms.push(synonym.substr(synonym.indexOf(':')+1, synonym.length-1));
}
return {text: text, examples: examples, synonyms: synonyms};
}
function getPreparedInformation(sourceText){
var posInformation = getPOSInformation();
var definitionInformation = getDefinitionInformation();
var synonymTitle = $('.gt-def-synonym-title').eq(0).text();
var toShowTitle = $('.gt-cd-mmd > .gt-cd-t > .gt-cd-tl > div').text()
.replace(sourceText, '<span id=gtp_bart ' +
'class="notranslate">' +
sourceText + '</span>');
// line feed by '; '
var toTranslateText = definitionInformation.text.join('; |; ');
toTranslateText += '; |; ' + posInformation.posList.join('; |; ') +
'; |; ' + synonymTitle + '; |; ' + toShowTitle;
return {
toTranslateText: toTranslateText,
notToTranslateInformation: {
posIndexs: posInformation.posIndexs,
examples: definitionInformation.examples,
synonyms: definitionInformation.synonyms
}
};
}
function showResult(data, notToTranslateInformation, sourceText, history){
console.log("@GTP, start to show result");
var translatedText = '';
var i;
for (i = 0; i < data[0].length; i++) {
translatedText += data[0][i][0];
}
var translatedTextList = translatedText.split('|');
var translatedResultList = [];
for (i = 0; i < translatedTextList.length; i++) {
var translatedResult = translatedTextList[i].trim();
if (translatedResult.indexOf(';') === 0) {
translatedResult = translatedResult.substr(1, translatedResult.length);
}
if (translatedResult.lastIndexOf(';') === translatedResult.length - 1) {
translatedResult = translatedResult.substr(0, translatedResult.length-1);
}
translatedResultList.push(translatedResult);
}
var toShowResult = '<div class="gtp-extension-info">' +
'<p class="gtp-extension-name"><b>Google Translate Plus</b></p>' +
'<a target="_blank" href="https://www.bart.com.hk?from=t-p">' +
'<img class="gtp-bart-logo" ' +
'title="Powered By Bart Solutions" ' +
'src="http://gtranslateplus.com/bart-logo.24.png"/>' +
'</a></div><div class="gtp-result-header">' +
'<p class="gtp-result-title">' +
translatedResultList.pop().replace('</ span>', '</span>') +
'</p></div>';
var posIndexs = notToTranslateInformation.posIndexs;
var posCount = posIndexs.length;
var definitionCount = translatedResultList.length - posCount - 1;
var index = 0;
for (i = 0; i < definitionCount; i++) {
if (index < posCount && (i === 0 || posIndexs.indexOf(i) > -1)){
toShowResult += '<p class="gtp-definition-pos">' +
translatedResultList[definitionCount + index] +
'</p>';
index ++;
}
var synonyms = notToTranslateInformation.synonyms;
var translatedSynonyms = '';
if (synonyms[i]) {
translatedSynonyms = translatedResultList.slice(-1).pop();
}
toShowResult += '<div class="gtp-translated-text">' +
'<p class="gtp-translated-definition">' +
translatedResultList[i] +
'</p><p class="gtp-definition-example">' +
notToTranslateInformation.examples[i] +
'</p><p class="gtp-definition-synonyms">' +
translatedSynonyms + synonyms[i] + '</div>';
}
for (i = 0; i < history.length; i++) {
if (i === 0) {
toShowResult += '<div class="sgn-history">';
}
var historyItem = history[i];
toShowResult += '<div class="sgn-history-item"><a target="_blank" href="' +
historyItem.url + '">' + historyItem.text + '</a></div>';
if (i === history.length - 1) {
toShowResult += '</div>';
}
}
var showDom = $(toShowResult);
//change translated source text to original source text
showDom.find('#gtp_bart').text(sourceText);
$('#gtp-definition-translated-result').append(showDom);
$( ".cd-expand-button" ).click();
}
function translateDefinition(){
var currentUrl = window.location.href;
if (currentUrl === gLastUrl){
return;
}
gLastUrl = currentUrl;
$('#gtp-definition-translated-result').remove();
if ($('.gt-cd-mmd:visible').length){
var parameters = currentUrl.split('#')[1].split('&');
var parameterDict = {};
for (i = 0; i < parameters.length; i++) {
var key = parameters[i].split('=')[0];
var value = parameters[i].split('=')[1];
parameterDict[key] = value;
}
var fromLanguage = parameterDict['sl'];
var toLanguage = parameterDict['tl'];
$('.gt-cc-l-i').before(
'<div id="gtp-definition-translated-result" class="gt-cc-l-i"></div>');
var sourceText = $('.gt-cd-mmd > .gt-cd-t > .gt-cd-tl > div > span').text();
var preparedInformation = getPreparedInformation(sourceText);
var toTranslateText = preparedInformation.toTranslateText;
var translateUrl = 'https://translate.googleapis.com' +
'/translate_a/single?client=gtx' +
'&sl=' + fromLanguage + '&tl=' + toLanguage +
'&dt=t&q=' + encodeURI(toTranslateText);
console.log("@GTP: start query");
$.get(translateUrl, function(data, status){
if (status === 'success'){
console.log("@GTP: translation collected");
getBrowser().storage.local.get(['max_history', 'GTPHistory'], function(result) {
var max_history = 10;
if(result.max_history){
max_history = parseInt(result.max_history);
}
var history = [];
if (max_history && result.GTPHistory) {
history = JSON.parse(result.GTPHistory);
}
showResult(data,
preparedInformation.notToTranslateInformation,
sourceText,
history);
if(max_history){
var historyFound = false;
for(var i=0; i<history.length; i++){
if(history[i].text === sourceText)
historyFound = true;
}
// do not repeat
if(!historyFound){
history.unshift({'text': sourceText, 'url': currentUrl});
history = JSON.stringify(history.slice(0, max_history));
getBrowser().storage.local.set({'GTPHistory': history});
}
}
});
} else {
console.log('translate failed');
}
});
}
}
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function(mutation) {
translateDefinition();
});
});
//observe whether the definition change
observer.observe($('.gt-cd-mmd')[0], {attributes: true, characterData: true});
setTimeout(translateDefinition, 200);