forked from leezu/zhongwen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
230 lines (193 loc) · 7.1 KB
/
main.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
/*
Zhongwen - A Chinese-English Popup Dictionary
Original Work Copyright (C) 2011 Christian Schiller
https://chrome.google.com/extensions/detail/kkmlkkjojmombglmlpbpapmhcaljjkde
Modified work Copyright (C) 2017 Leonard Lausen
https://github.com/leezu/zhongwen
---
Originally based on Rikaikun 0.8
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.
*/
function reportError(error) {
console.error(`Error: ${error}`);
}
function ignoreError(error) {}
var zhongwenMain = {
altView: 0,
tabIDs: {},
loadDictionary: async function() {
let dictData = await loadDictData();
return new ZhongwenDictionary(...dictData);
},
// The callback for onActivated.
// Just sends a message to the tab to enable itself if it hasn't already.
onTabActivated: function (activeInfo) {
zhongwenMain._checkEnableTab(activeInfo.tabId)
},
onTabUpdated: function (tabId, changeInfo, tabInfo) {
zhongwenMain._checkEnableTab(tabId)
},
_checkEnableTab: function (tabId) {
let enabledPromise = browser.storage.local.get({enabled: 0});
enabledPromise.then((storage) => {
if (storage.enabled === 1) {
let optionsPromise = browser.storage.sync.get({
options: {
'popupcolor': "yellow",
'tonecolors': "yes",
'fontSize': "small",
'skritterTLD': "com",
'translit': "pinyin",
'grammar': "yes"
}
});
optionsPromise.then((storage) => {
browser.tabs.sendMessage(tabId, {
type: "enable",
config: storage.options
}).catch(reportError);
});
}
});
},
enable: function(tab) {
let optionsPromise = browser.storage.sync.get({
options: {
'popupcolor': 'yellow',
'tonecolors': 'yes',
'fontSize': 'small',
'skritterTLD': 'com',
'translit': 'pinyin',
'grammar': 'yes'
}
})
let dictionaryPromise = zhongwenMain.loadDictionary()
let enabled = 1
let enablePromise = browser.storage.local.set({enabled})
Promise.all([optionsPromise, dictionaryPromise, enablePromise]).then(
([storage, dictionary, enabled]) => {
this.dict = dictionary
// Send message to current tab to add listeners and create stuff
if (tab !== undefined) {
browser.tabs.sendMessage(tab.id, {
type: 'enable',
config: storage.options
}).catch(reportError)
browser.tabs.sendMessage(tab.id, {
type: 'showPopup',
isHelp: true
}).catch(reportError)
}
browser.browserAction.setBadgeBackgroundColor({
'color': [255, 0, 0, 255]
})
browser.browserAction.setBadgeText({
'text': 'On'
})
browser.contextMenus.create({
title: 'Open word list',
id: 'wordlist-page',
onclick: zhongwenMain.wordlistTab,
contexts: ['page']
})
})
},
disable: function(tab) {
let enabled = 0;
let enablePromise = browser.storage.local.set({enabled});
Promise.all([enablePromise]).then(([storage]) => {
// Delete dictionary object after we implement it
delete this.dict;
browser.browserAction.setBadgeBackgroundColor({
"color": [0, 0, 0, 0]
});
browser.browserAction.setBadgeText({
"text": ""
});
// Send a disable message to all browsers.
browser.windows.getAll({
"populate": true
}).then((windowInfoArray) => {
for (let windowInfo of windowInfoArray) {
for (let tabInfo of windowInfo.tabs) {
browser.tabs.sendMessage(tabInfo.id, {
type: "disable"
}).catch(ignoreError);
// Some tabs may not have a listener as they were never activated
}
}
});
});
browser.contextMenus.remove("wordlist-page");
},
enableToggle: function(tab) {
let enabledPromise = browser.storage.local.get({enabled: 0});
enabledPromise.then((storage) => {
if (storage.enabled == 1) {
zhongwenMain.disable(tab);
} else {
zhongwenMain.enable(tab);
}
});
},
search: function(text) {
var entry = this.dict.wordSearch(text);
if (entry != null) {
for (var i = 0; i < entry.data.length; i++) {
var word = entry.data[i][1];
if (this.dict.hasKeyword(word) && (entry.matchLen == word.length)) {
// the final index should be the last one with the maximum length
entry.grammar = { keyword: word, index: i };
}
}
}
return entry;
},
wordlistTab: function() {
var url = browser.extension.getURL("/wordlist.html");
var tabID = zhongwenMain.tabIDs['wordlist'];
if (tabID) {
browser.tabs.get(tabID, function(tab) {
if (tab && (tab.url.substr(-13) == 'wordlist.html')) {
browser.tabs.reload(tabID);
browser.tabs.update(tabID, {active: true});
} else {
browser.tabs.create({
url: url
}, function(tab) {
zhongwenMain.tabIDs['wordlist'] = tab.id;
browser.tabs.reload(tab.id);
});
}
});
} else {
browser.tabs.create({ url: url }, function(tab) {
zhongwenMain.tabIDs['wordlist'] = tab.id;
browser.tabs.reload(tab.id); });
}
}
};