-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
116 lines (105 loc) · 3.16 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
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
// list with all the links as objects
let linksList = [];
async function addLink(url, status) {
let index = linksList.findIndex(link => link.url === url);
if (index > -1) {
if (linksList[index].status != status) {
linksList[index].status = status;
chrome.storage.local.set({ linksList: JSON.stringify(linksList) });
}
} else {
linksList.push({ url: url, status: status });
chrome.storage.local.set({ linksList: JSON.stringify(linksList) });
}
}
/**
* Removes a link by its URL
*
* @param {String} url Absolute URL to remove
*/
async function removeLink(url) {
var deleted = [];
for (var i = 0; i < linksList.length; i++) {
if (linksList[i].url == url) {
deleted = deleted.concat(linksList.splice(i, 1));
}
}
if (deleted.length > 0) {
chrome.storage.local.set({ linksList: JSON.stringify(linksList) });
}
}
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({
"id": "markLink",
"title": "Mark link",
"contexts": [ "link" ]
});
chrome.contextMenus.create({
id: "markLinkPending",
parentId: "markLink",
title: "📙 Pending",
contexts: [ "link" ]
});
chrome.contextMenus.create({
id: "markLinkInProgress",
parentId: "markLink",
title: "📘 In progress",
contexts: [ "link" ]
});
chrome.contextMenus.create({
id: "markLinkDone",
parentId: "markLink",
title: "📗 Done",
contexts: [ "link" ]
});
chrome.contextMenus.create({
id: "markLinkClear",
parentId: "markLink",
title: "📕 Clear",
contexts: [ "link" ]
});
});
chrome.storage.local.get(["linksList"]).then((result) => {
if (typeof result.linksList !== 'undefined' && result.linksList.length > 0) {
linksList = JSON.parse(result.linksList);
}
});
chrome.contextMenus.onClicked.addListener(function(clickData) {
switch (clickData.menuItemId) {
case "markLinkPending":
addLink(clickData.linkUrl, 'pending');
break;
case "markLinkInProgress":
addLink(clickData.linkUrl, 'progress');
break;
case "markLinkDone":
addLink(clickData.linkUrl, 'done');
break;
case "markLinkClear":
removeLink(clickData.linkUrl);
break;
}
chrome.tabs.query({ active: true, lastFocusedWindow: true }, function(tabs) {
tabs.forEach(tab => {
if (typeof tab.url !== 'undefined') {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: [ "content/script.js" ],
});
}
})
});
});
chrome.tabs.onActivated.addListener(function(activeInfo) {
chrome.tabs.get(
activeInfo.tabId,
(tab) => {
if (typeof tab.url !== 'undefined') {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: [ "content/script.js" ],
});
}
}
)
});