-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
94 lines (72 loc) · 2 KB
/
script.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
chrome.contextMenus.create({
"title": "Jisho Lookup",
"id": "Menu",
"contexts": ["selection"],
});
chrome.contextMenus.create({
"title": "Search Word",
"id": "searchWord",
"parentId": "Menu",
"contexts": ["selection"],
"enabled": false
});
chrome.contextMenus.create({
"title": "Search Kanji",
"id": "searchKanji",
"parentId": "Menu",
"contexts": ["selection"],
"enabled": false
});
chrome.runtime.onMessage.addListener((msg) => {
let kanjiOnly = new RegExp(/[\u3400-\u9FBF]/)
let kanjiHiraKata = new RegExp(/[\u3040-\u309F]|[\u30A0-\u30FF]|[\u3400-\u9FBF]/)
function buildQuery(info, tab, regex) {
let selectedText = msg.selection;
let query = "";
for (let i = 0; i < selectedText.length; i++) {
if (selectedText[i].match(regex)) {
query = query + selectedText[i];
}
}
return query;
}
function searchWord(info, tab) {
let query = buildQuery(info, tab, kanjiHiraKata)
chrome.tabs.create({ url: `https://jisho.org/search/${query}` })
}
function searchKanji(info, tab) {
let query = buildQuery(info, tab, kanjiOnly)
for (let i = 0; i < query.length; i++) {
if (i === 0) {
chrome.tabs.create({ url: `http://jisho.org/kanji/details/${query[i]}`, selected: true })
} else {
chrome.tabs.create({ url: `http://jisho.org/kanji/details/${query[i]}`, selected: false })
}
}
}
if (msg.request === "yesKanji") {
chrome.contextMenus.update("searchWord", {
"enabled": true,
"onclick": searchWord
})
chrome.contextMenus.update("searchKanji", {
"enabled": true,
"onclick": searchKanji
})
} else if (msg.request === "noKanji") {
chrome.contextMenus.update("searchWord", {
"enabled": true,
"onclick": searchWord
})
chrome.contextMenus.update("searchKanji", {
"enabled": false,
})
} else {
chrome.contextMenus.update("searchWord", {
"enabled": false,
})
chrome.contextMenus.update("searchKanji", {
"enabled": false,
})
}
})