-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
35 lines (28 loc) · 1.11 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
function replaceMetaTags(details) {
let filter = browser.webRequest.filterResponseData(details.requestId);
let decoder = new TextDecoder("utf-8");
let encoder = new TextEncoder();
filter.ondata = event => {
let str = decoder.decode(event.data, {stream: true});
str = str.replaceAll(/<meta( |http-equiv=.?content-security-policy.?|content=".*?"){4,}\/?>/gi, '<!-- CSP REMOVED -->');
filter.write(encoder.encode(str));
filter.disconnect();
}
return {};
}
browser.browserAction.setBadgeText({text: "on"});
browser.browserAction.setBadgeBackgroundColor({color: "green"});
browser.browserAction.onClicked.addListener(() => {
const has = browser.webRequest.onBeforeRequest.hasListener(replaceMetaTags);
browser.browserAction.setBadgeText({text: has ? "on" : "off" });
browser.browserAction.setBadgeBackgroundColor({color: has ? "green" : "red" });
if (has) {
browser.webRequest.onBeforeRequest.removeListener(replaceMetaTags)
return;
}
browser.webRequest.onBeforeRequest.addListener(
replaceMetaTags,
{ urls: ["<all_urls>"], types: ["main_frame"] },
["blocking"]
);
});