-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
76 lines (64 loc) · 2.22 KB
/
script.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
import config from "./config.js";
import { parse } from "./parser.js";
import { editor, languages } from "./monaco.js";
languages.register({ id: "language" });
languages.setMonarchTokensProvider("language", {
tokenizer: {
root: [...config.highlightedTokens.map(token => {
return [token.toUpperCase(), "highlight"]
}), [/^#.*/, "comment"]]
}
});
editor.defineTheme("theme", {
rules: [
{ token: "highlight", fontStyle: "bold" },
{ token: "comment", foreground: "#9e9e9e" }
],
colors: { "editor.foreground": "#000000" },
base: "vs"
});
const dedent = input => input.trim().replace(/^ +/gm, "");
const defaultValue = dedent(config.defaultValue);
const input = editor.create(document.getElementById("editor"), {
value: localStorage.value || defaultValue,
...config.editorSettings
});
const unregisterServiceWorkers = input => {
const text = input.split("\n").filter(line => /^\w+/.test(line)).join("\n");
const origins = Object.entries(config.serviceWorkers).map(([domain, origin]) => {
const includesEscapedDomain = text.includes(domain.replace(".", "\\."));
return text.includes(domain) || includesEscapedDomain ? origin : false;
}).filter(Boolean);
return origins.length && chrome.browsingData.remove({ origins }, {
"serviceWorkers": true
});
};
const button = document.getElementById("save");
const save = async value => {
try {
const rules = await chrome.declarativeNetRequest.getDynamicRules();
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: rules.map(rule => rule.id),
addRules: parse(value)
});
await unregisterServiceWorkers(value);
localStorage.value = value;
button.disabled = true;
} catch (error) {
alert(error);
}
};
button.addEventListener("click", () => {
save(input.getValue());
});
document.addEventListener("keydown", e => {
if ((e.ctrlKey || e.metaKey) && e.key === "s") {
e.preventDefault();
const value = input.getValue();
!button.disabled && save(value);
}
});
!localStorage.value && save(defaultValue);
input.onDidChangeModelContent(() => {
button.disabled = false;
});