-
Notifications
You must be signed in to change notification settings - Fork 1
/
contextmenu.js
41 lines (38 loc) · 1.4 KB
/
contextmenu.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
refreshContextMenu();
chrome.runtime.onMessage.addListener(function (request, sender) {
if (request.type === "collections edited") {
refreshContextMenu();
}
});
function refreshContextMenu() {
chrome.contextMenus.removeAll(function () {
chrome.storage.local.get("Collections", function (collections) {
collections = collections["Collections"];
chrome.contextMenus.create({
"title": "Add to collection",
"id": "rootMenu",
"contexts": ["selection", "link"]
});
for (let collectionName in collections) {
chrome.contextMenus.create({
"title": collectionName,
"parentId": "rootMenu",
"id": collectionName,
"contexts": ["selection", "link"]
});
}
})
});
}
chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (info.parentMenuItemId === "rootMenu") {
console.log(info);
chrome.storage.local.get("Collections", function (collections) {
collections = collections["Collections"];
if (!collections[info.menuItemId].hasOwnProperty(info.selectionText)) {
collections[info.menuItemId][info.selectionText] = 1;
}
chrome.storage.local.set({"Collections": collections});
})
}
});