-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
273 lines (235 loc) · 7.18 KB
/
index.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
var {Cc, Ci} = require("chrome");
var contextMenu = require("sdk/context-menu");
var tabs = require("sdk/tabs");
var self = require("sdk/self");
var data = self.data;
var _ = require("sdk/l10n").get;
var prefs = require("sdk/simple-prefs");
var Request = require("sdk/request").Request;
var Hotkey = require("sdk/hotkeys").Hotkey;
var {Cc, Ci} = require("chrome");
var tabs = require("sdk/tabs");
var ui = require("sdk/ui");
// Globals variables
var currentSelection = null;
var internalSource = prefs.prefs["internal.source"];
// The ActionButton need to know selection
var selection = require("sdk/selection");
selection.on('select', function() {
if (selection.isContiguous && selection.text) {
currentSelection = selection.text;
}
else {
currentSelection = null;
}
});
// Panel to show translation
var panel = require("sdk/panel").Panel({
width: prefs.prefs["panel.width"],
height: prefs.prefs["panel.height"],
contentURL: data.url("html/loading.html"),
contentScriptFile: [data.url("js/jquery.min.js"), data.url("js/content-panel.js")],
onHide: function() {
this.port.emit("hide", prefs.prefs["panel.width"]);
}
});
//menu context don't support port
//The context-menu package doesn't allow bi-directional communication with the content script. If you look at the documentation
//you can only receive messages sent by the content script but not send messages to it.
//Instead, the messages context and click are sent to the content script automatically in appropriate situations.
var menuItem = contextMenu.Item({
label: _("menu_prefix"),
image: data.url("img/menuItem.png"),
// display context menu only if selection is not null
context: contextMenu.SelectionContext(),
contentScriptFile: [data.url("js/content-context_menu.js")],
onMessage: function (msg) {
switch(msg.event) {
case "context":
var txt = msg.payload;
// Truncate text before display
if(msg.payload.length > 30) {
txt = msg.payload.substring(0, 30) +"...";
}
this.label = _("menu_prefix", txt);
break;
case "click":
//show panel before request else focus don't work
panel.show();
fulfillPanel(msg.payload);
break;
}
}
});
var button = ui.ActionButton({
id: "wrtip-button",
label: _("addon_abbr"),
badge: createTranslationLabel(),
badgeColor: "#0A21E3",
icon: {
"16": "./img/menuItem.png"
},
onClick: function(state) {
fulfillPanel(currentSelection);
panel.show({
position: this
});
}
});
var showHotKey = null;
// Activate hotkey only if user checked option
if(prefs.prefs["hotkey.active"]) {
showHotKey = initHotKey(getHotkeyCombination());
}
/**
* Forge wordreference url
* word : word to translate
* format : type of result : html or json. Default empty for html
*/
function createTranslationUrl(word, format="", sourceDest = undefined) {
var language = sourceDest || prefs.prefs["translate.source"]+prefs.prefs["translate.destination"];
return "http://api.wordreference.com/b583b/"+format+"/"+language+"/"+word;
}
function createTranslationUrlMobile(word, sourceDest = undefined) {
var language = sourceDest || prefs.prefs["translate.source"]+prefs.prefs["translate.destination"];
return "http://mini.wordreference.com/mini/index.aspx?w="+word+"&dict="+language;
}
function createTranslationUrlSite(word, sourceDest = undefined) {
var language = sourceDest || prefs.prefs["translate.source"]+prefs.prefs["translate.destination"];
return "http://www.wordreference.com/"+language+"/"+word;
}
function createTranslationLabel() {
return prefs.prefs["translate.source"]+""+prefs.prefs["translate.destination"];
}
/**
* Get html content from WordReference and fulfill panel with result.
* Asynchrone request
*
* Run request if word to translate is defined
*
* @param word : word to translate
* @param sourceDest symbolizes the source and destination languages Optional
*/
function fulfillPanel(word, sourceDest) {
determineInternalSource(function() {
var wrRequest;
if(internalSource == "API") {
wrRequest = Request({
url: createTranslationUrl(word, "", sourceDest),
onComplete: function (response) {
//Transmit information to panel
panel.port.emit("newTranslationAPI", {
"search": word,
"html": sanitize(response.text),
"msg" : {
"openTab" :_("panel.openTab")
}
});
}
});
}
else {
// Compute mobile url
wrRequest = Request({
url: createTranslationUrlMobile(word, sourceDest),
onComplete: function (response) {
//Transmit information to panel
panel.port.emit("newTranslationMOBILE", {
"search": word,
"html": sanitize(response.text),
"msg" : {
"openTab" :_("panel.openTab")
}
});
}
});
}
// Run request
wrRequest.get();
});
}
function initHotKey(key) {
return Hotkey({
combo: key,
onPress: function() {
panel.show();
fulfillPanel(currentSelection);
}
});
}
/**
* Returns combination for hotkey. If user doesn't provide comination use default
*/
function getHotkeyCombination() {
return prefs.prefs["hotkey.combination"] || "alt-shift-f";
}
/**
* Sanitize string keeping style
*/
function sanitize(html) {
var parser = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils);
return parser.sanitize(html, parser.SanitizerAllowStyle | parser.SanitizerInternalEmbedsOnly);
}
// ##### Events #####
// search for panel : form submit
panel.port.on("newSearch", function (obj) {
fulfillPanel(obj.search, obj.language);
});
// Click on open in new tab
panel.port.on("openTab", function (obj) {
tabs.open(createTranslationUrlSite(obj.search, obj.language));
panel.hide();
});
// ##### Preferences #####
prefs.on("panel.width", function() {
panel.width = prefs.prefs["panel.width"];
});
prefs.on("panel.height", function() {
panel.height = prefs.prefs["panel.height"];
});
prefs.on("translate.source", function() {
button.badge = createTranslationLabel();
});
prefs.on("translate.destination", function() {
button.badge = createTranslationLabel();
});
prefs.on("hotkey.active", function() {
//deativate hotkey if uncheked
if(!prefs.prefs["hotkey.active"]) {
showHotKey.destroy();
}
else { // User re-checked box so re-activation
showHotKey = initHotKey(getHotkeyCombination());
}
});
prefs.on("hotkey.combination", function() {
showHotKey = initHotKey(getHotkeyCombination());
});
prefs.on("internal.source", function() {
internalSource = prefs.prefs["internal.source"];
});
// ##### Other #####
/**
* Query a website to determine which provider use to get translation.
* MOBILE => use html of mobile version and customise it
* API => use html of api.
*/
function determineInternalSource(callback) {
// If user doesn't force a provider => get config over http (once)
if(internalSource == "DEFAULT") {
var r = Request({
url: "http://jerep6.fr/divers/wrtip/wrtip.json",
onComplete: function (response) {
if(response.status == 200) {
internalSource=response.json.internalSource;
}
//execute callback even if error. User wants translation. He don't care tha config doesn't respond
callback();
}
});
r.get();
}
else {
callback();
}
}