-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
67 lines (57 loc) · 2.14 KB
/
Copy pathbackground.js
File metadata and controls
67 lines (57 loc) · 2.14 KB
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
/*
* NoRefer 2 — background.
* Single writer for declarativeNetRequest dynamic rules: the popup only
* writes storage; this script compiles storage -> DNR and owns the badge.
* Runs as a service worker in Chrome and an event page script in Firefox.
*/
/* global NoRefer */
if (typeof importScripts === 'function') importScripts('rules.js');
var api = typeof browser !== 'undefined' ? browser : chrome;
function loadState() {
return api.storage.local.get({ enabled: true, rules: [] });
}
function sync() {
return loadState().then(function (state) {
return NoRefer.applyState(api, state).then(
function () {
return { ok: true, at: Date.now(), active: NoRefer.compile(state).length };
},
function (err) {
return { ok: false, at: Date.now(), error: String((err && err.message) || err) };
}
);
}).then(function (status) {
return api.storage.local.set({ syncStatus: status });
}).then(refreshBadge);
}
function refreshBadge() {
return Promise.all([loadState(), api.tabs.query({ active: true })])
.then(function (results) {
var state = results[0];
results[1].forEach(function (tab) {
var count = 0;
if (state.enabled !== false && tab.url && /^https?:/.test(tab.url)) {
var host = new URL(tab.url).hostname;
count = state.rules.filter(function (r) {
return NoRefer.matchesHost(r, host);
}).length;
}
api.action.setBadgeText({ tabId: tab.id, text: count ? String(count) : '' });
});
})
.catch(function () { /* tab may be gone */ });
}
api.action.setBadgeBackgroundColor({ color: '#6d5df2' });
if (api.action.setBadgeTextColor) api.action.setBadgeTextColor({ color: '#ffffff' });
api.runtime.onInstalled.addListener(sync);
api.runtime.onStartup.addListener(sync);
api.storage.onChanged.addListener(function (changes, area) {
if (area !== 'local') return;
if (!('rules' in changes) && !('enabled' in changes)) return;
sync();
});
api.tabs.onActivated.addListener(refreshBadge);
api.tabs.onUpdated.addListener(function (tabId, info) {
if (info.url || info.status === 'complete') refreshBadge();
});
refreshBadge();