-
Notifications
You must be signed in to change notification settings - Fork 5
/
background.js
112 lines (107 loc) · 3.24 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
function resetStates() {
for (key in localStorage) {
if (key == "undefined" || typeof (key) == undefined|| key == "firstRun" || localStorage.getItem(key) == null) {
continue;
}
var storedEntry = JSON.parse(localStorage.getItem(key));
storedEntry.bActivated = false;
localStorage.setItem(key, JSON.stringify(storedEntry))
}
}
/*function checkForValidUrl(tab) {
for (key in localStorage) {
if (key == "undefined" || typeof (key) == undefined|| key == "firstRun" || localStorage.getItem(key) == null) {
continue;
}
var storedEntry = JSON.parse(localStorage.getItem(key));
if (storedEntry.bActivated == false) {
for (q in storedEntry.filterWords) {
var patt = new RegExp(storedEntry.filterWords[q], 'i');
if (patt.test(tab.url)) {
//console.log(tab.url,storedEntry.name)
storedEntry.bActivated = true;
localStorage.setItem(key, JSON.stringify(storedEntry))
}
}
}
}
}*/
function setExt(url) {
// go through and disable/enable extensions marked for activation
console.log("Updating extension enablednesses...");
chrome.browserAction.setBadgeBackgroundColor({color: [0, 0, 0, 0]});
chrome.browserAction.setBadgeText({text: ""});
for (key in localStorage) {
if (key == "undefined" || typeof (key) == undefined || key == "firstRun" || localStorage.getItem(key) == null) {
continue;
}
var storedEntry = JSON.parse(localStorage.getItem(key));
chrome.management.get(key, function (ext) {
console.log("Updating for:" + JSON.stringify(storedEntry));
let entryMatchesURL = storedEntry.filterWords.find(urlPattern=>url.match(new RegExp(urlPattern, "i"))) != null;
if (entryMatchesURL) {
chrome.browserAction.setBadgeBackgroundColor({color: [100, 250, 100, 120]});
chrome.browserAction.setBadgeText({text: "ON"});
chrome.management.setEnabled(storedEntry.id, storedEntry.bEnable);
} else {
chrome.management.setEnabled(storedEntry.id, !storedEntry.bEnable);
}
});
}
}
/*chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status == 'loading') {
//Execute script when the page is fully (DOM) ready, otherwise EVENT FIRES TWICE!
resetStates();
chrome.windows.getAll({
"populate": true
}, function (window) {
for (w in window) {
for (t in window[w].tabs) {
checkForValidUrl(window[w].tabs[t]);
}
}
setExt(window[w].tabs[t].url);
});
}
});
chrome.tabs.onRemoved.addListener(function (tabId, removeInfo) {
resetStates();
chrome.windows.getAll({
"populate": true
}, function (window) {
for (w in window) {
for (t in window[w].tabs) {
checkForValidUrl(window[w].tabs[t]);
}
}
setExt(window[w].tabs[t].url);
});
});*/
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status == 'loading') {
setExt(tab.url);
}
});
chrome.tabs.onActivated.addListener(info=> {
let {tabId, windowId} = info;
chrome.tabs.getSelected(null, function(tab) {
console.log("Tab changed:" + tab.url);
setExt(tab.url);
});
});
//on init get all
chrome.browserAction.setBadgeText({
text: ""
});
/*chrome.windows.getAll({
"populate": true
}, function (window) {
resetStates();
/*for (w in window) {
for (t in window[w].tabs) {
checkForValidUrl(window[w].tabs[t]);
}
}*#/
setExt(window[w].tabs[t].url);
});*/