This repository was archived by the owner on Sep 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
67 lines (63 loc) · 1.55 KB
/
background.js
File metadata and controls
67 lines (63 loc) · 1.55 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
class StorageWrapper {
constructor(storage) {
this.queue = Promise.resolve();
this.storage = storage;
}
pushTask(executor) {
let result = this.queue
.then(executor) // onFulfilled
.catch(console.error); // onRejected
this.queue = result;
return result;
}
export() {
return this.batchGet(null).then((object) => {
let text = JSON.stringify(object);
console.log(text);
return object;
});
}
clear() {
let executor = () => this.storage.clear();
return this.pushTask(executor);
}
batchGet(keys) {
let executor = () => this.storage.get(keys);
return this.pushTask(executor);
}
batchSet(keys) {
let executor = () => this.storage.set(keys);
return this.pushTask(executor);
}
batchRemove(keys) {
let executor = () => this.storage.remove(keys);
return this.pushTask(executor);
}
get(key) {
return this.batchGet([key]).then((keys) => keys[key]);
}
set(key, value) {
return this.batchSet({ [key]: value });
}
remove(key) {
return this.batchRemove([key]);
}
}
const extensionStorage = new StorageWrapper(browser.storage.local);
browser.runtime.onMessage.addListener(({ hostname, code }) => {
// get code
if (typeof code !== "string") {
return extensionStorage.get(hostname);
}
// set code
if (code !== "") extensionStorage.set(hostname, code);
else extensionStorage.remove(hostname);
// notify relative tabs about code change
browser.tabs
.query({ url: [`https://${hostname}/*`, `http://${hostname}/*`] })
.then((tabs) => {
for (let { id } of tabs) {
browser.tabs.sendMessage(id, code);
}
});
});