-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
48 lines (41 loc) · 1.32 KB
/
Copy pathcontentScript.js
File metadata and controls
48 lines (41 loc) · 1.32 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
const DEFAULT = {
speed: 1.0,
cbSetIntervalChecked: true,
cbSetTimeoutChecked: true,
cbPerformanceNowChecked: true,
cbDateNowChecked: true,
cbRequestAnimationFrameChecked: false,
};
let speedConfig = { ...DEFAULT };
function normalize(c) {
const out = { ...DEFAULT, ...(c || {}) };
out.speed = Math.max(0.05, parseFloat(out.speed) || 1);
return out;
}
function broadcast(config) {
speedConfig = normalize(config);
document.dispatchEvent(new CustomEvent("hush-config", { detail: speedConfig }));
window.postMessage({ command: "setSpeedConfig", config: speedConfig });
}
function injectTag() {
if (document.getElementById("__hush_tag")) return;
const s = document.createElement("script");
s.id = "__hush_tag";
s.src = chrome.runtime.getURL("injected.js");
(document.documentElement || document.head).appendChild(s);
}
injectTag();
chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
if (req.command === "setSpeedConfig") {
broadcast(req.config);
sendResponse({ ok: true, speed: speedConfig.speed });
} else if (req.command === "getSpeedConfig") {
sendResponse(speedConfig);
}
return true;
});
window.addEventListener("message", (e) => {
if (e.data && e.data.command === "getSpeedConfig") {
window.postMessage({ command: "setSpeedConfig", config: speedConfig });
}
});