-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
70 lines (61 loc) · 1.74 KB
/
background.js
File metadata and controls
70 lines (61 loc) · 1.74 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
68
69
70
/**
* deepblur — background
*
* Modes (browser.storage.local "imageMode"):
* "block" — cancel image-class requests via webRequest
* "blur" — allow requests; content script applies CSS blur
* "allow" — full images
*
* Default: "block"
*/
const IMAGE_TYPES = ["image", "imageset", "media"];
const STORAGE_KEY = "imageMode";
const DEFAULT_MODE = "block";
let currentMode = DEFAULT_MODE;
browser.storage.local.get(STORAGE_KEY).then((result) => {
currentMode = result[STORAGE_KEY] || DEFAULT_MODE;
applyWebRequestRule();
});
browser.storage.onChanged.addListener((changes, area) => {
if (area === "local" && changes[STORAGE_KEY]) {
currentMode = changes[STORAGE_KEY].newValue;
applyWebRequestRule();
notifyAllTabs();
}
});
function blockListener(details) {
return { cancel: true };
}
function applyWebRequestRule() {
if (browser.webRequest.onBeforeRequest.hasListener(blockListener)) {
browser.webRequest.onBeforeRequest.removeListener(blockListener);
}
if (currentMode === "block") {
browser.webRequest.onBeforeRequest.addListener(
blockListener,
{ urls: ["<all_urls>"], types: IMAGE_TYPES },
["blocking"]
);
}
}
browser.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.type === "GET_MODE") {
sendResponse({ mode: currentMode });
return true;
}
if (msg.type === "SET_MODE") {
const newMode = msg.mode;
browser.storage.local.set({ [STORAGE_KEY]: newMode });
sendResponse({ ok: true });
return true;
}
});
function notifyAllTabs() {
browser.tabs.query({}).then((tabs) => {
for (const tab of tabs) {
browser.tabs
.sendMessage(tab.id, { type: "MODE_CHANGED", mode: currentMode })
.catch(() => {});
}
});
}