-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
194 lines (169 loc) · 5.04 KB
/
background.js
File metadata and controls
194 lines (169 loc) · 5.04 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
if (navigator.userAgent.indexOf("Chrome") != -1) {
browser = chrome;
}
async function loadJSON(path) {
const url = browser.runtime.getURL(path);
const res = await fetch(url);
return res.json();
}
async function findSiteForUrl(url) {
if (!url || !url.startsWith("http")) return null;
const hostname = new URL(url).hostname;
const registry = await loadJSON("sites/registry.json");
for (const [key, site] of Object.entries(registry)) {
if (hostname.endsWith(site.domain)) {
const meta = await loadJSON(site.meta);
return { siteKey: key, site, meta };
}
}
return null;
}
async function getEnabledMap() {
const data = await browser.storage.local.get("enabled");
return data.enabled || {};
}
async function setEnabledMap(map) {
await browser.storage.local.set({ enabled: map });
}
async function updateBadge(tabId, url) {
try {
if (!url || !url.startsWith("http")) {
await browser.action.setBadgeText({ tabId, text: "" });
return;
}
const found = await findSiteForUrl(url);
if (!found) {
await browser.action.setBadgeText({ tabId, text: "" });
return;
}
const { siteKey, site, meta } = found;
const enabledMap = await getEnabledMap();
const perSite = enabledMap[siteKey] || {};
let count = 0;
for (const tweak of meta.tweaks) {
const enabled = perSite.hasOwnProperty(tweak.id) ? perSite[tweak.id] : true;
if (enabled) count++;
}
await browser.action.setBadgeText({ tabId, text: count ? String(count) : "" });
await browser.action.setBadgeBackgroundColor({ tabId, color: site.badgeColor || "#444" });
} catch (e) {
console.error("updateBadge error", e);
await browser.action.setBadgeText({ tabId, text: "" });
}
}
async function insertCss(tabId, cssPath) {
try {
await browser.scripting.insertCSS({
target: { tabId },
files: [cssPath]
});
} catch (e) {
console.error("insertCss error", e);
}
}
async function removeCss(tabId, cssPath) {
try {
await browser.scripting.removeCSS({
target: { tabId },
files: [cssPath]
});
} catch (e) {
console.error("removeCss error", e);
}
}
async function injectJs(tabId, jsPath) {
try {
await browser.scripting.executeScript({
target: { tabId },
files: [jsPath]
});
} catch (e) {
console.error("injectJs error", e);
}
}
async function applyTweaksToTab(tabId, url) {
const found = await findSiteForUrl(url);
if (!found) return;
const { siteKey, meta } = found;
const enabledMap = await getEnabledMap();
const perSite = enabledMap[siteKey] || {};
for (const tweak of meta.tweaks) {
const enabled = perSite.hasOwnProperty(tweak.id) ? perSite[tweak.id] : true;
const cssPath = tweak.css || null;
const jsPath = tweak.js || null;
if (cssPath) {
if (enabled) {
await insertCss(tabId, cssPath);
} else {
await removeCss(tabId, cssPath);
}
}
if (jsPath) {
if (enabled) {
await injectJs(tabId, jsPath);
}
}
}
await updateBadge(tabId, url);
}
browser.tabs.onActivated.addListener(async ({ tabId }) => {
try {
const tab = await browser.tabs.get(tabId);
await applyTweaksToTab(tabId, tab.url);
} catch (e) {}
});
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status === "complete") {
await applyTweaksToTab(tabId, tab.url);
}
});
browser.runtime.onMessage.addListener((msg, sender, sendResponse) => {
const handleMessage = async () => {
if (msg?.action === "setEnabled") {
const { siteKey, tweakId, enabled } = msg;
const map = await getEnabledMap();
map[siteKey] = map[siteKey] || {};
map[siteKey][tweakId] = enabled;
await setEnabledMap(map);
const registry = await loadJSON("sites/registry.json");
const site = registry[siteKey];
if (!site) return { ok: true };
const tabs = await browser.tabs.query({});
for (const t of tabs) {
if (!t.url || !t.url.startsWith("http")) continue;
if (new URL(t.url).hostname.endsWith(site.domain)) {
await applyTweaksToTab(t.id, t.url);
}
}
const meta = await loadJSON(site.meta);
const tweak = meta.tweaks.find(x => x.id === tweakId);
const needReload = Boolean(tweak && tweak.js);
return { needReload };
}
if (msg?.action === "reloadTab") {
if (typeof msg.tabId === "number") {
await browser.tabs.reload(msg.tabId);
return { reloaded: true };
}
return { reloaded: false };
}
if (msg?.action === "getState") {
const registry = await loadJSON("sites/registry.json");
const enabled = await getEnabledMap();
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
const tab = tabs[0] || null;
return {
registry,
enabled,
tabUrl: tab ? tab.url : null,
tabId: tab ? tab.id : null
};
}
};
handleMessage().then((response) => {
if (response !== undefined) {
sendResponse(response);
}
});
return true;
});