-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.js
418 lines (365 loc) · 13.3 KB
/
bootstrap.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
Copyright 2014 ispedals
This file is part of Rikaichan for Android.
Rikaichan for Android is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Rikaichan for Android is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Rikaichan for Android. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/Services.jsm");
function isNativeUI() {
return Services.appinfo.ID === "{aa3c5121-dab2-40e2-81ca-7ea25febc110}";
}
//sandbox for Rikaisama. It's global because it makes it easier to debug
var context;
var menuIcon;
function loadIntoWindow(window) {
if (!window){
return;
}
var currentWindow = window;
var gBrowser;
//gBrowser polyfill for Firefox for Android
if(isNativeUI()){
gBrowser = {
get mCurrentBrowser() {
return currentWindow.BrowserApp.selectedBrowser;
},
get mTabContainer() {
return currentWindow.BrowserApp.deck;
},
get selectedTab(){
return currentWindow.BrowserApp.selectedTab;
},
get addTab() {
return currentWindow.BrowserApp.addTab.bind(currentWindow.BrowserApp);
},
get browsers(){
return currentWindow.BrowserApp.tabs.map(
tab => currentWindow.BrowserApp.getBrowserForWindow(tab.window)
);
}
};
}
else {
gBrowser = currentWindow.gBrowser;
}
/*
Rikaisama expects to be running in a chrome window with a loaded document.
Instead of letting Rikaisama have unfettered access to "window", we only
expose the properties it expects, so that if things change, things will fail
faster. Doing it this way also causes Rikaisama to fail silently when it
attempts to modify the XUL overlay, which we want to happen.
To do this, we load Rikaisama in a sandbox with "context" being the global
environment.
*/
context = {
'window': {
'addEventListener': function () {
},
get content() {
return gBrowser.mCurrentBrowser.contentWindow;
},
get document() {
return gBrowser.mCurrentBrowser.contentDocument;
},
get setTimeout() {
return currentWindow.setTimeout.bind(currentWindow);
},
get clearTimeout() {
return currentWindow.clearTimeout.bind(currentWindow);
}
},
get setTimeout() {
return currentWindow.setTimeout.bind(currentWindow);
},
get clearTimeout() {
return currentWindow.clearTimeout.bind(currentWindow);
},
get content() {
return gBrowser.mCurrentBrowser.contentWindow;
},
'document': {
'documentElement': {
},
'getElementById': function () {
}
},
'gBrowser': gBrowser,
//stores the default preferences
'defaultPreferences': {
},
/*
Default preferences are a file with the preferences wrapped in a function
like:
pref(key, val)
We exploit this fact and define a global function that assigns the
preference values to "defaultPreferences"
*/
'pref': function (key, val) {
this.defaultPreferences[key] = val;
},
//Rikaisama expects these two properties to be available globally
'Node': Ci.nsIDOMNode,
'XPathResult': Ci.nsIDOMXPathResult
};
context.pref.bind(context);
//set context.defaultPreferences with the default preferences
Services.scriptloader.loadSubScript('chrome://alts/content/Rikaisama/defaults/preferences/rikaichan.js', context, 'UTF-8');
//load rcxConfig
Services.scriptloader.loadSubScript('chrome://rikaichan/content/config.js', context, 'UTF-8');
/*
These methods expect that if a user has not set the preferences, Firefox
will just return the default preference.
We wrap these methods to first try to read from the preferences and if
that fails, return the value stored as the default preference
*/
['getString', 'getInt', 'getBool'].forEach(function (func) {
var oldFunc = context.rcxPrefs.prototype[func];
context.rcxPrefs.prototype[func] = function (key) {
try {
return oldFunc.call(this, key);
}
catch (e){
if (context.defaultPreferences[this.branch.root + key] != null) {
return context.defaultPreferences[this.branch.root + key];
}
}
};
});
//load rcxMain
Services.scriptloader.loadSubScript('chrome://rikaichan/content/rikaichan.js', context, 'UTF-8');
//load rcxData
Services.scriptloader.loadSubScript('chrome://rikaichan/content/data.js', context, 'UTF-8');
/*
Rikaisama expects that dictionaries will be installed as addons and that
they will add themselves to a global variable called rcxDicList. Every
dictionary must emulate this
*/
context.rcxDicList = {
"[email protected]" : {
'name': "Japanese-English",
'version': "2.01.160401",
'id': "[email protected]",
'hasType': true,
'isName': false
}
};
//resolve chrome url to dictionary to file path
const chromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIChromeRegistry);
const ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
var url = ioService.newURI( 'chrome://alts/content/[email protected]/', null, null);
var path = chromeRegistry.convertChromeURL(url).QueryInterface(Components.interfaces.nsIFileURL).file.path;
/*
Rikaisama expects dictionaries to be installed as addons and to be access
files inside the addon's directory. However, Rikaisama happily loads the
dictionary database if its provided with a file path, so we do that.
*/
context.rcxData.dicPath = {
'ready': true,
"[email protected]" : path
};
//some default preferences for us
context.defaultPreferences['extensions.rikaisama.checkversion'] = false;
context.defaultPreferences['extensions.rikaisama.firsticon'] = false;
context.defaultPreferences['extensions.rikaisama.minihelp'] = false;
// rikaichan is active for the entire browser window
context.defaultPreferences['extensions.rikaisama.enmode'] = 1;
// sticky means that the popup is dismissed on only on dblclick
context.defaultPreferences['extensions.rikaisama.startsticky'] = true;
/*
even though we set extensions.rikaichan.checkversion to false
this function is still called, so we make it a no-op
*/
context.rcxMain.checkVersion = function rikaisamaCustomCheckVersion(){};
//wrap rcxMain.showPopup to add our modifications to the popup window
var old = context.rcxMain.showPopup;
context.rcxMain.showPopup = function rikaisamaCustomShowPopup(){
old.apply(context.rcxMain, arguments);
var htmlDocument = context.window.content.document;
var popup = htmlDocument.getElementById('rikaichan-window');
if(!popup){
return;
}
/*
By having extensions.rikaichan.startsticky set, and therefore having Rikaisama be in
super-sticky mode, the popup is dismissed on double clicks. Touch devices
can't cause double clicks, so we make clicks act as double clicks
*/
if(!popup.hasListener){
popup.addEventListener("click", function rikaisamaPopupClick(ev) {
context.rcxMain.hidePopup();
ev.stopPropagation();
}, false);
popup.hasListener = true;
}
/*
If this is true, these are the text-only messages Rikaisama emits,
so don't add our tools to it
*/
if(!popup.firstElementChild){
return;
}
popup.style.opacity = "0.9";
//add our tools
var tools = htmlDocument.createElement('div');
/*
because our tools correspond to keyboard-bound functions, we use the
existing key bindings to trigger the functions
*/
var makeKeyboardEvent = function(code){
return function rikaisamaPopupTool(ev){
var fakeKeyEvent = { //emulate a key event
'altKey': false,
'metaKey': false,
'ctrlKey': false,
'shiftKey': false,
'keyCode': parseInt(code, 10),
'currentTarget': {
'rikaichan': context.rcxMain.getBrowser().rikaichan
},
'stopPropagation': function(){},
'preventDefault': function(){}
};
ev.stopPropagation();
//Rikaisama expects keydown to be followed by keyup
context.rcxMain.onKeyDown(fakeKeyEvent);
context.rcxMain.onKeyUp(fakeKeyEvent);
};
};
//previous character
var previous = htmlDocument.createElement('a');
previous.innerHTML = '\u21E6';
previous.id = 'rikai-previous';
previous.href = 'javascript:void(0)';
previous.style.cssFloat = 'left';
previous.style.fontSize="xx-large";
previous.addEventListener("click", makeKeyboardEvent(context.rcxConfig.kbpreviouscharacter), false);
tools.appendChild(previous);
//next word
var next = htmlDocument.createElement('a');
next.innerHTML = '\u21E8';
next.id = 'rikai-next';
next.href = 'javascript:void(0)';
next.style.cssFloat = 'left';
next.style.fontSize="xx-large";
next.addEventListener("click", makeKeyboardEvent(context.rcxConfig.kbnextword), false);
tools.appendChild(next);
//save
var save = htmlDocument.createElement('a');
save.innerHTML = '\uD83D\uDCBE';
save.id = 'rikai-save';
save.href = 'javascript:void(0)';
save.style.cssFloat = 'right';
save.style.fontSize="xx-large";
save.addEventListener("click", makeKeyboardEvent(context.rcxConfig.kbsavetofile), false);
tools.appendChild(save);
popup.insertBefore(tools, popup.firstChild);
var br = htmlDocument.createElement('br');
br.style.clear='both';
popup.insertBefore(br, tools.nextSibling);
//copy to clipboard
var copy = htmlDocument.createElement('a');
copy.innerHTML = '\u2702';
copy.id = 'rikai-copy';
copy.href = 'javascript:void(0)';
copy.style.cssFloat = 'right';
copy.style.fontSize="xx-large";
copy.addEventListener("click", makeKeyboardEvent(context.rcxConfig.kbcopytoclipboard), false);
popup.appendChild(copy);
}.bind(context.rcxMain);
/*
Rikaisama expects to be able to set the window onload handler to run
rcxMain._init. We run this code after the window has loaded, so we call rcxMain._init ourselves
*/
context.rcxMain._init();
if(isNativeUI()){
menuIcon = currentWindow.NativeWindow.menu.add({
'name': "Rikaichan",
'checkable': true,
'callback': function rikaisamaMenuCallback(){
context.rcxMain.toggle();
currentWindow.NativeWindow.menu.update(menuIcon, {
'checked': !!context.rcxMain.enabled
});
}
});
currentWindow.NativeWindow.menu.update(menuIcon, {'checked': false});
}
}
function unloadFromWindow(window) {
if (!window){
return;
}
context.rcxMain.disable(context.gBrowser.mCurrentBrowser, 1);
if (isNativeUI()) {
window.NativeWindow.menu.remove(menuIcon);
}
}
var windowListener = {
'onOpenWindow': function(window) {
var domWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindow);
function onWindowLoad(){
domWindow.removeEventListener("load",onWindowLoad);
if (domWindow.document.documentElement.getAttribute("windowtype") == "navigator:browser"){
if (isNativeUI() && domWindow && !domWindow.BrowserApp.deck){
domWindow.addEventListener("UIReady", function onUIReady(){
domWindow.removeEventListener("UIReady", onUIReady, false);
loadIntoWindow(domWindow);
}, false);
}
else {
loadIntoWindow(domWindow);
}
}
}
domWindow.addEventListener("load",onWindowLoad);
},
'onCloseWindow': function(window) {
},
'onWindowTitleChange': function(window, title) {
}
};
function startup(data, reason) {
var windows = Services.wm.getEnumerator("navigator:browser");
var domWindow;
while (windows.hasMoreElements()) {
domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
if (isNativeUI() && domWindow && !domWindow.BrowserApp.deck){
domWindow.addEventListener("UIReady", function onUIReady(){
domWindow.removeEventListener("UIReady", onUIReady, false);
loadIntoWindow(domWindow);
}, false);
}
else {
loadIntoWindow(domWindow);
}
}
Services.wm.addListener(windowListener);
}
function shutdown(data, reason) {
if (reason == APP_SHUTDOWN){
return;
}
Services.wm.removeListener(windowListener);
var windows = Services.wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
var domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
unloadFromWindow(domWindow);
}
// HACK WARNING: The Addon Manager does not properly clear all addon related caches on update;
// in order to fully update images and locales, their caches need clearing here
Services.obs.notifyObservers(null, "chrome-flush-caches", null);
}
function install(data, reason) {
}
function uninstall(data, reason) {
}