-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
146 lines (131 loc) · 3.97 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const STORE = 'coupons',
DB_VERSION = 1,
NONE = 0,
WWW_PREFIX = 'www.',
ONE_DAY_IN_MINUTES = 1440,
ONE = 1,
ZERO = 0,
LISTENER_OPTS = {
passive: true,
once: true,
};
function waitForRequest(requestInstance) {
return new Promise((resolve, reject) => {
requestInstance.addEventListener("success", resolve, LISTENER_OPTS);
requestInstance.addEventListener("error", reject, LISTENER_OPTS);
});
}
async function getDatabase() {
const request = globalThis.indexedDB.open(STORE, DB_VERSION);
request.addEventListener("upgradeneeded", (event) => {
const coupons = event.target.result.createObjectStore(STORE, {
keyPath: 'id',
autoIncrement: true,
});
coupons.createIndex('pagecoupon', [
'coupon',
'host',
], { unique: true });
coupons.createIndex('page', 'host', { unique: false });
coupons.createIndex('expires', 'expires', { unique: false });
}, {
once: true,
passive: true,
});
const event = await waitForRequest(request);
return event.target.result;
}
function ignoreWWW(host) {
if(host.startsWith(WWW_PREFIX)) {
return host.slice(WWW_PREFIX.length);
}
return host;
}
function getHost(url) {
return ignoreWWW((new URL(url)).hostname);
}
async function getCount(url, database) {
if(!database) {
database = await getDatabase();
}
const host = getHost(url),
transaction = database.transaction(STORE),
store = transaction.objectStore(STORE),
index = store.index('page'),
cursorRequest = index.openCursor();
return new Promise((resolve, reject) => {
let count = 0;
cursorRequest.addEventListener("success", (event) => {
const cursor = event.target.result;
if(cursor) {
if(ignoreWWW(cursor.value.host) == host) {
++count;
}
cursor.continue();
}
else {
resolve(count);
}
});
cursorRequest.addEventListener("error", reject);
});
}
async function updateTabCount(tab, database) {
const count = await getCount(tab.url, database);
browser.browserAction.setBadgeText({
text: count > NONE ? count.toString() : '',
tabId: tab.id,
});
}
async function updateActiveTabs(database) {
if(!database) {
// Optimize database opening when iterating over tabs.
database = await getDatabase(); // eslint-disable-line require-atomic-updates
}
const tabs = await browser.tabs.query({
active: true,
});
return Promise.all(tabs.map((tab) => updateTabCount(tab, database)));
}
function runExpunge(database) {
const expunger = new Worker(browser.runtime.getURL("worker.js"));
expunger.addEventListener("message", () => {
updateActiveTabs(database).catch(console.error);
}, { passive: true });
}
async function init() {
const now = new Date(),
midnight = new Date(now.getFullYear(), now.getMonth(), now.getDate() + ONE, ZERO, ZERO, ZERO, ZERO);
browser.alarms.create("expunge", {
when: midnight.getTime(),
periodInMinutes: ONE_DAY_IN_MINUTES,
});
const database = await getDatabase();
runExpunge(database);
await updateActiveTabs(database);
}
browser.tabs.onActivated.addListener(async ({ tabId }) => {
const tab = await browser.tabs.get(tabId);
updateTabCount(tab);
});
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if(changeInfo.url && tab.active) {
updateTabCount(tab);
}
}, {
urls: [ '<all_urls>' ],
});
browser.runtime.onInstalled.addListener((details) => {
if(details.reason !== "browser_update") {
init();
}
});
browser.runtime.onStartup.addListener(() => {
init();
});
browser.alarms.onAlarm.addListener((alarm) => {
if(alarm.name !== "expunge") {
return;
}
runExpunge();
});