forked from quoid/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry-script-market.js
More file actions
63 lines (59 loc) · 1.59 KB
/
Copy pathentry-script-market.js
File metadata and controls
63 lines (59 loc) · 1.59 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
let url;
async function injection() {
const tabUrl = new URL(location.href);
let links;
if (tabUrl.hostname.endsWith("greasyfork.org")) {
links = document.querySelectorAll(
'#install-area a.install-link[data-install-format="js"]',
);
}
for (const link of links) {
if (link["href"]) url = link["href"];
link.addEventListener(
"click",
(e) => {
e.stopImmediatePropagation();
e.preventDefault();
browser.runtime.sendMessage({ name: "WEB_USERJS_POPUP" });
},
true,
);
}
}
async function listeners() {
/**
* handle messages from background, popup, etc...
* @type {import("webextension-polyfill").Runtime.OnMessageListener}
*/
const handleMessage = async (message) => {
if (import.meta.env.MODE === "development") {
console.debug(message, url);
}
if (message === "TAB_CLICK_USERJS") {
return url;
}
};
/** Dynamically remove listeners to avoid memory leaks */
if (document.visibilityState === "visible") {
browser.runtime.onMessage.addListener(handleMessage);
}
document.addEventListener("visibilitychange", () => {
if (document.hidden) {
browser.runtime.onMessage.removeListener(handleMessage);
} else {
browser.runtime.onMessage.addListener(handleMessage);
}
});
}
async function initialize() {
// avoid duplicate injection of content scripts
if (window["CS_ENTRY_SCRIPT_MARKET"]) return;
window["CS_ENTRY_SCRIPT_MARKET"] = 1;
// check user settings
const key = "US_AUGMENTED_USERJS_INSTALL";
if ((await browser.storage.local.get(key))[key] === false) return;
// actual execution content
injection();
listeners();
}
initialize();