-
Notifications
You must be signed in to change notification settings - Fork 0
/
rikaichan.js
433 lines (366 loc) · 10.7 KB
/
rikaichan.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
Rikaisan-universal
Copyright (C) 2018 eyeS Code
http://code.google.com/p/rikaisan-universal
---
Originally based on Rikaikun
by Erek Speed
Copyright (C) 2010 Erek Speed
http://code.google.com/p/rikaikun/
---
Originally based on Rikaichan 1.07
by Jonathan Zarate
http://www.polarcloud.com/
---
Originally based on RikaiXUL 0.4 by Todd Rudick
http://www.rikai.com/
http://rikaixul.mozdev.org/
---
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
---
Please do not change or remove any of the copyrights or links to web pages
when modifying any of the files. - Jon
*/
var rcxMain = {
haveNames: true,
// canDoNames: false,
dictCount: 3,
altView: 0,
enabled: 0,
/*
onLoad: function() { rcxMain._onLoad(); },
_onLoad: function() {
try {
//this.loadPrefs();
//this.haveNames = this.canDoNames = (typeof(rcxNamesDict) != 'undefined');
}
catch (ex) {
alert('Exception in onLoad: ' + ex);
}
},
*/
/*
//Switch this over to local storage method for chrome. Have to make a UI.
loadPrefs: function() {
try {
var pb = this.getPrefBranch();
var xm = ['cm', 'tm'];
var i;
var a, b, c;
this.cfg = {};
for (i = 0; i < rcxCfgList.length; ++i) {
b = rcxCfgList[i];
switch (b[0]) {
case 0:
this.cfg[b[1]] = pb.getIntPref(b[1]);
break;
case 1:
this.cfg[b[1]] = pb.getCharPref(b[1]);
break;
case 2:
this.cfg[b[1]] = pb.getBoolPref(b[1]);
break;
}
}
this.dictCount = 3;
this.canDoNames = this.haveNames;
if (!this.haveNames) this.cfg.dictorder = 0;
switch (this.cfg.dictorder) {
case 0:
this.canDoNames = false;
this.dictCount = 2;
case 1:
this.kanjiN = 1;
this.namesN = 2;
break;
case 2:
this.kanjiN = 2;
this.namesN = 1;
break;
}
for (i = 1; i >= 0; --i) {
c = xm[i];
try {
a = !this.cfg[c + 'toggle'];
b = !this.cfg[c + 'lbar'];
document.getElementById('rikaichan-toggle-' + c).hidden = a;
document.getElementById('rikaichan-lbar-' + c).hidden = b;
document.getElementById('rikaichan-separator-' + xm[i]).hidden = a || b;
}
catch (ex) {
// alert('unable to set menu: c=' + c + ' ex=' + ex)
}
}
switch (this.cfg.ssep) {
case 'Tab':
this.cfg.ssep = '\t';
break;
case 'Comma':
this.cfg.ssep = ',';
break;
case 'Space':
this.cfg.ssep = ' ';
break;
}
this.cfg.css = (this.cfg.css.indexOf('/') == -1) ? ('chrome://rikaichan/skin/popup-' + this.cfg.css + '.css') : this.cfg.css;
if (!this.isTB) {
for (i = 0; i < gBrowser.browsers.length; ++i) {
c = gBrowser.browsers[i].contentDocument.getElementById('rikaichan-css');
if (c) c.setAttribute('href', this.cfg.css);
}
}
c = { };
c.kdisp = [];
a = pb.getCharPref('kindex').split(',');
for (i = 0; i < a.length; ++i) {
c.kdisp[a[i]] = 1;
}
c.wmax = this.cfg.wmax;
c.wpop = this.cfg.wpop;
c.wpos = this.cfg.wpos;
c.namax = this.cfg.namax;
this.dconfig = c;
if (this.dict) this.dict.setConfig(c);
if (this.isTB) this.cfg.enmode = 0;
b = document.getElementById('rikaichan-status');
if (b) b.hidden = (this.cfg.sticon == 0);
}
catch (ex) {
alert('Exception in LoadPrefs: ' + ex);
}
},
*/
loadDictionary: function() {
this.dict = new rcxDict();
return this.dict.init(this.haveNames/* && !this.cfg.nadelay*/);
//this.dict.setConfig(this.dconfig);
},
/*
showDownload: function() {
const url = 'http://rikaichan.mozdev.org/welcome.html';
try {
var u = '';
if (this.version != null) {
u += 'rv=' + this.version + '&';
}
if ((typeof(rcxWordDict) != 'undefined') && (rcxWordDict.version != null)) {
u += 'wv=' + rcxWordDict.version + '&';
}
if ((typeof(rcxNamesDict) != 'undefined') && (rcxNamesDict.version != null)) {
u += 'nv=' + rcxNamesDict.version + '&';
}
if (u.length) u = url + '?' + u;
else u = url;
if (this.isTB) {
Components.classes['@mozilla.org/messenger;1'].createInstance()
.QueryInterface(Components.interfaces.nsIMessenger)
.launchExternalURL(u);
}
else {
var w = window.open(u, 'rcxdict');
if (w) w.focus();
}
}
catch (ex) {
if (typeof(rcxWordDict) == 'undefined') {
alert('[rikaichan] Please install a dictionary file from ' + url);
}
else {
alert('[rikaichan] There was an error while opening ' + url);
}
}
},
*/
// The callback for onSelectionChanged
// Just sends a message to the tab to enable itself if it hasn't
// already
onTabSelect: function(tabId) { rcxMain._onTabSelect(tabId); },
_onTabSelect: function(tabId) {
if ((this.enabled == 1))
chrome.tabs.sendMessage(tabId, {"type":"enable", "config":rcxMain.config});
},
savePrep: function(clip, entry) {
var me, mk;
var text;
var i;
var f;
var e;
f = entry;
if ((!f) || (f.length == 0)) return null;
if (clip) { // save to clipboard
me = rcxMain.config.maxClipCopyEntries;
//mk = this.cfg.smaxck; // something related to the number of kanji in the look-up bar
}
/*else { // save to file
me = this.cfg.smaxfe;
//mk = this.cfg.smaxfk;
}*/
if (!this.fromLB) mk = 1;
text = '';
for (i = 0; i < f.length; ++i) {
e = f[i];
if (e.kanji) {
//if (mk-- <= 0) continue
text += this.dict.makeText(e, 1);
}
else {
if (me <= 0) continue;
text += this.dict.makeText(e, me);
me -= e.data.length;
}
}
if (rcxMain.config.lineEnding == "rn") text = text.replace(/\n/g, '\r\n');
else if (rcxMain.config.lineEnding == "r") text = text.replace(/\n/g, '\r');
if (rcxMain.config.copySeparator != "tab") {
if (rcxMain.config.copySeparator == "comma")
return text.replace(/\t/g, ",");
if (rcxMain.config.copySeparator == "space")
return text.replace(/\t/g, " ");
}
return text;
},
// Needs entirely new implementation and dependent on savePrep
copyToClip: function(tab, entry) {
var text;
if ((text = this.savePrep(1, entry)) != null) {
document.oncopy = function(event) {
event.clipboardData.setData("Text", text);
event.preventDefault();
};
document.execCommand("Copy");
document.oncopy = undefined;
chrome.tabs.sendMessage(tab.id, {"type":"showPopup", "text":'Copied to clipboard.'});
}
},
miniHelp:
'<span style="font-weight:bold">Rikaikun enabled!</span><br><br>' +
'<table cellspacing=5>' +
'<tr><td>A</td><td>Alternate popup location</td></tr>' +
'<tr><td>Y</td><td>Move popup location down</td></tr>' +
'<tr><td>C</td><td>Copy to clipboard</td></tr>' +
'<tr><td>D</td><td>Hide/show definitions</td></tr>' +
'<tr><td>Shift/Enter </td><td>Switch dictionaries</td></tr>' +
'<tr><td>B</td><td>Previous character</td></tr>' +
'<tr><td>M</td><td>Next character</td></tr>' +
'<tr><td>N</td><td>Next word</td></tr>' +
'</table>',
/* '<tr><td>C</td><td>Copy to clipboard</td></tr>' +
'<tr><td>S</td><td>Save to file</td></tr>' + */
// Function which enables the inline mode of rikaikun
// Unlike rikaichan there is no lookup bar so this is the only enable.
inlineEnable: function(tab, mode) {
if (!this.dict) {
// var time = (new Date()).getTime();
this.loadDictionary().then(function () {
// Send message to current tab to add listeners and create stuff
chrome.tabs.sendMessage(tab.id, { "type": "enable", "config": rcxMain.config });
this.enabled = 1;
if (mode == 1) {
if (rcxMain.config.minihelp == 'true')
chrome.tabs.sendMessage(tab.id, { "type": "showPopup", "text": rcxMain.miniHelp });
else
chrome.tabs.sendMessage(tab.id, { "type": "showPopup", "text": 'Rikaikun enabled!' });
}
chrome.browserAction.setBadgeBackgroundColor({ "color": [255, 0, 0, 255] });
chrome.browserAction.setBadgeText({ "text": "On" });
}.bind(this)).catch(function(err){
alert('Error loading dictionary: ' + err);
});
// time = (((new Date()).getTime() - time) / 1000).toFixed(2);
}
},
// This function diables
inlineDisable: function(tab, mode) {
// Delete dictionary object after we implement it
delete this.dict;
this.enabled = 0;
chrome.browserAction.setBadgeBackgroundColor({"color":[0,0,0,0]});
chrome.browserAction.setBadgeText({"text":""});
// Send a disable message to all browsers
var windows = chrome.windows.getAll({"populate":true},
function(windows) {
for (var i =0; i < windows.length; ++i) {
var tabs = windows[i].tabs;
for ( var j = 0; j < tabs.length; ++j) {
chrome.tabs.sendMessage(tabs[j].id, {"type":"disable"});
}
}
});
},
inlineToggle: function(tab) {
if (rcxMain.enabled) rcxMain.inlineDisable(tab, 1);
else rcxMain.inlineEnable(tab, 1);
},
kanjiN: 1,
namesN: 2,
showMode: 0,
nextDict: function() {
this.showMode = (this.showMode + 1) % this.dictCount;
},
resetDict: function() {
this.showMode = 0;
},
sameDict: '0',
forceKanji: '1',
defaultDict: '2',
nextDict: '3',
search: function(text, dictOption) {
switch (dictOption) {
case this.forceKanji:
var e = this.dict.kanjiSearch(text.charAt(0));
return e;
break;
case this.defaultDict:
this.showMode = 0;
break;
case this.nextDict:
this.showMode = (this.showMode + 1) % this.dictCount;
break;
}
var m = this.showMode;
var e = null;
do {
switch (this.showMode) {
case 0:
e = this.dict.wordSearch(text, false);
break;
case this.kanjiN:
e = this.dict.kanjiSearch(text.charAt(0));
break;
case this.namesN:
e = this.dict.wordSearch(text, true);
break;
}
if (e) break;
this.showMode = (this.showMode + 1) % this.dictCount;
} while (this.showMode != m);
return e;
}
};
/*
2E80 - 2EFF CJK Radicals Supplement
2F00 - 2FDF Kangxi Radicals
2FF0 - 2FFF Ideographic Description
p 3000 - 303F CJK Symbols and Punctuation
x 3040 - 309F Hiragana
x 30A0 - 30FF Katakana
3190 - 319F Kanbun
31F0 - 31FF Katakana Phonetic Extensions
3200 - 32FF Enclosed CJK Letters and Months
3300 - 33FF CJK Compatibility
x 3400 - 4DBF CJK Unified Ideographs Extension A
x 4E00 - 9FFF CJK Unified Ideographs
x F900 - FAFF CJK Compatibility Ideographs
p FF00 - FFEF Halfwidth and Fullwidth Forms
x FF66 - FF9D Katakana half-width
*/