-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
63 lines (50 loc) · 1.32 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
var currentTab;
var sitesJson = null;
// update currentTab (including browser storage) if new tab is active
function updateActiveTab(tabs) {
function updateTab(tabs) {
if (tabs[0]) {
currentTab = tabs[0];
console.log(currentTab.url);
browser.storage.local.set({
ctab: currentTab.url
});
}
var getting = browser.storage.local.get("sites");
getting.then(setSitesJson, onError);
}
var gettingActiveTab = browser.tabs.query({active: true, currentWindow: true});
gettingActiveTab.then(updateTab);
}
browser.tabs.onUpdated.addListener(updateActiveTab);
browser.tabs.onActivated.addListener(updateActiveTab);
updateActiveTab();
function setSitesJson(result) {
sitesJson = result.sites || "";
siteInJson();
}
// search if current URL is included in JSON
// enable/disable icon button accordingly
function siteInJson() {
sites = JSON.parse(sitesJson);
var found = false;
for (var i = 0; i < sites.length; i++) {
if (found === true) {
break;
}
for (var j = 0; j < sites[i].length; j++) {
if (currentTab.url.indexOf(sites[i][j]) === 0) {
found = true;
break;
}
}
}
if (found === false) {
browser.action.disable();
} else {
browser.action.enable();
}
}
function onError(error) {
console.log(`Error: ${error}`);
}