-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
executable file
·196 lines (182 loc) · 6.54 KB
/
index.html
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
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="lib.js" charset="utf-8"></script>
<script type="text/javascript">
( function () {
var storage = localStorage;
var browserLang = window.navigator.language;
var homeLang = storage.getItem( 'homeLang' ) ||
( Translate.languages[ browserLang ] ? browserLang : 'en' );
var xhrManager = ( function() {
var pool = [];
return {
get: function () {
return pool.pop() || new XMLHttpRequest();
},
release: function( xhr ) {
xhr.onreadystatechange = function () {};
pool.push( xhr );
}
};
}() );
function getAnalysisUrl ( text ) {
var url =
'https://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=';
text = encodeURIComponent( text );
if ( text.length > 1300 ) {
text = text.slice( 0, text.lastIndexOf( '%', 1300 ) );
}
url += text;
return url;
}
function getTranslatePacket ( strings, from, to ) {
var packet = 'v=1.0&langpair=' + from + '%7C' + to,
i = 0,
l = strings.length;
while ( i < l ) {
for ( var segments = 0;
i < l && segments < 100; i += 1, segments += 1 ) {
var next = encodeURIComponent( strings[i] );
packet += '&q=';
packet += next;
}
}
return packet;
}
var actions = {
analyse: function ( data, source ) {
if ( data.protocol === 'https:' &&
storage.getItem( 'https' ) !== '1' ) {
source.postMessage({
action: 'analysisFailed'
});
return;
}
var xhr = xhrManager.get();
xhr.open( 'GET', getAnalysisUrl( data.text ), true );
xhr.onreadystatechange = function () {
if ( xhr.readyState < 4 ) { return; }
var result = JSON.parse( xhr.responseText ),
data = result.responseData;
if ( ( xhr.status >= 200 && xhr.status < 300 ) &&
( result.responseStatus === 200 &&
data.confidence > 0.15 ) ) {
var language = data.language;
if ( language !== homeLang ) {
var langPref = storage.getItem( language ) || 'ask';
if ( langPref === 'never' ) {
// no translate
source.postMessage({
action: 'inNativeLanguage'
});
} else {
// Request or auto translate
source.postMessage({
action: 'showMessage',
data: {
homeLang: homeLang,
language: Translate.languages[ language ],
langCode: language,
preference: langPref
}
});
}
} else {
source.postMessage({
action: 'inNativeLanguage'
});
}
} else {
source.postMessage({
action: 'analysisFailed'
});
}
xhrManager.release( xhr );
};
xhr.send();
},
translate: function ( data, source ) {
var transactionId = data.id,
strings = data.strings,
from = data.fromLang,
packet = getTranslatePacket( strings, from, homeLang ),
translatedStrings = [],
xhr = xhrManager.get();
xhr.onreadystatechange = function () {
if ( xhr.readyState < 4 ) { return; }
var result = JSON.parse( xhr.responseText ),
xhrData = result.responseData;
if ( ( xhr.status >= 200 && xhr.status < 300 ) &&
result.responseStatus === 200 ) {
if ( !( xhrData instanceof Array ) ) {
xhrData = [ xhrData ];
}
Array.prototype.push.apply( translatedStrings, xhrData.map(
function ( item ) {
return item.responseStatus === 200 ?
item.responseData.translatedText : null;
}
) );
source.postMessage({
action: 'translate',
data: {
id: transactionId,
translatedStrings: translatedStrings
}
});
} else {
source.postMessage({
action: 'fail',
data: {
id: transactionId
}
});
}
xhrManager.release( xhr );
};
xhr.open( 'POST',
'https://ajax.googleapis.com/ajax/services/language/translate',
true );
xhr.setRequestHeader( 'Content-type',
'application/x-www-form-urlencoded' );
xhr.send( packet );
},
openPreferences: function ( data, source ) {
opera.extension.tabs.create( { url: 'options.html', focused: true } );
},
setPreference: function ( data, source ) {
if ( data.selection ) {
storage.setItem( data.fromLang, data.selection );
}
},
resetPreferences: function ( data, source ) {
storage.clear();
source.postMessage({
action: 'preferencesReset'
});
homeLang = Translate.languages[ browserLang ] ? browserLang : 'en';
},
blanketPreferences: function ( data, source ) {
for ( var lang in Translate.languages ) {
storage.setItem( lang, data.value );
}
source.postMessage({
action: 'preferencesReset'
});
},
setHomeLang: function ( data, source ) {
homeLang = data;
storage.setItem( 'homeLang', data );
}
};
window.addEventListener( 'load', function () {
opera.extension.addEventListener( 'message', function ( msg ) {
actions[ msg.data.action ]( msg.data.data, msg.source );
}, false );
}, false );
}() );
</script>
</head>
<body></body>
</html>