-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
50 lines (47 loc) · 1.11 KB
/
background.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
const dictionaries = {
kimo: {
name: '奇摩字典',
query: text => `http://tw.dictionary.yahoo.com/dictionary?p=${text}`
},
moedict: {
name: '萌典',
query: text => `http://www.moedict.tw/${text}`
},
goo: {
name: 'goo辞書',
query: text => `http://dictionary.goo.ne.jp/srch/all/${text}/m0u/`
},
google: {
name: 'Google Translate',
query: text => `https://translate.google.com/#en/zh-TW/${text}`
}
};
const createMenu = id => {
const { name } = dictionaries[id];
browser.menus.create({
id,
title: `${name}: %s`,
contexts: ['selection'],
icons: {
"16": `assets/${id}.ico`
}
});
};
createMenu('kimo'); // only enable kimo before implement options
browser.menus.onClicked.addListener(function(info, tab) {
const id = info.menuItemId;
if (dictionaries[id]) {
browser.pageAction.setIcon({
tabId: tab.id,
path: {
"16": `assets/${id}.ico`
}
});
browser.pageAction.setPopup({
tabId: tab.id,
popup: dictionaries[id].query(info.selectionText)
})
}
browser.pageAction.show(tab.id);
browser.pageAction.openPopup();
});