-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
87 lines (73 loc) · 2.6 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
async function hasPermissions() {
return await browser.permissions.contains({
origins: [
"*://minecraft.fandom.com/*",
"*://*.gamepedia.com/*",
"*://*.minecraftwiki.net/*",
]
});
}
async function setupPermissions() {
let createData = {
url: "settings.html",
};
browser.tabs.create(createData);
}
browser.runtime.onInstalled.addListener(async (event) => {
// disable the blurb in the preferences page if the user updated from an older version that did not have it
if (event.reason === "update" && event.previousVersion === "1.0.0" || event.previousVersion === "1.1.0")
await browser.storage.local.set({projectPromos: false});
if (!await hasPermissions())
await setupPermissions();
});
async function getAllTabsWithHostPermissions() {
const tabs = await browser.tabs.query({});
const selectedTabs = [];
for (const tab of tabs) {
try {
if (tab.url && await browser.permissions.contains({ origins: [tab.url] })) {
selectedTabs.push(tab);
}
} catch {
// ignore special tabs like about:blank
}
}
return selectedTabs;
}
async function configureDisabledStatus(disabled) {
browser.action.setBadgeBackgroundColor({color: [255, 0, 0, 255]});
browser.action.setBadgeText({text: disabled ? 'X' : ''});
browser.declarativeNetRequest.updateEnabledRulesets(disabled ? {
disableRulesetIds: ["minecraft-wiki-redirect"]
} : {
enableRulesetIds: ["minecraft-wiki-redirect"]
});
browser.storage.local.set({disabled: disabled});
// if the user enables it, refresh all of their Fandom Minecraft Wiki tabs
if (!disabled) {
const tabs = await getAllTabsWithHostPermissions();
tabs.forEach(tab => browser.tabs.reload(tab.id));
}
}
browser.runtime.onMessage.addListener(async (message) => {
if (message.action === "change-redirect-state") {
await configureDisabledStatus(message.state);
}
});
browser.action.onClicked.addListener(async () => {
const oldState = (await browser.storage.local.get(["disabled"])).disabled;
let newState = !oldState;
if (!await hasPermissions()) {
newState = false;
await setupPermissions();
}
await configureDisabledStatus(newState);
try {
await browser.runtime.sendMessage({ action: "change-redirect-state", state: !newState });
} catch (err) {
// preference page is not open
}
});
(async () => {
configureDisabledStatus((await browser.storage.local.get(["disabled"])).disabled || false);
})();