forked from moarsel/chrome-extension-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.html
executable file
·135 lines (127 loc) · 3.32 KB
/
background.html
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!doctype html>
<html>
<head>
<title>Extension Automation Background Page</title>
</head>
<body>
<script>
function resetStates() {
for (key in localStorage) {
if (key == "undefined" || typeof (key) == undefined|| key == "firstRun") {
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") {
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() {
//go through and disable/enable extensions marked for activation
chrome.browserAction.setBadgeBackgroundColor({
color: [0, 0, 0, 0]
});
chrome.browserAction.setBadgeText({
text: ""
});
for (key in localStorage) {
if (key == "undefined" || typeof (key) == undefined || key == "firstRun") {
continue;
}
var storedEntry = JSON.parse(localStorage.getItem(key));
chrome.management.get(key, function (ext) {
var storedEntry = JSON.parse(localStorage.getItem(ext.id));
if (storedEntry.bEnable == true) {
if (storedEntry.bActivated == true) {
chrome.management.setEnabled(storedEntry.id, true);
chrome.browserAction.setBadgeBackgroundColor({
color: [100, 250, 100, 120]
});
chrome.browserAction.setBadgeText({
text: "ON"
});
} else {
chrome.management.setEnabled(storedEntry.id, false);
}
} else {
if (storedEntry.bActivated == true) {
chrome.management.setEnabled(storedEntry.id, false);
chrome.browserAction.setBadgeBackgroundColor({
color: [250, 100, 100, 120]
});
chrome.browserAction.setBadgeText({
text: "ON"
});
} else {
chrome.management.setEnabled(storedEntry.id, true);
}
}
localStorage.setItem(ext.id, JSON.stringify(storedEntry));
//console.log("Enabled?", ext.enabled, " Set to Enable?", storedEntry.bEnable, "Set to Activate (enable/disable)?", storedEntry.bActivated);
});
}
}
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();
});
}
});
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();
});
});
//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();
});
</script>
</body>
</html>