this repo has no description
1/* eslint-disable no-console */
2/* eslint-disable no-undef */
3
4const starterUrls = ["web.", "sentry."];
5let blockLoading = true;
6let doing = false;
7let collectedUrls = new Set();
8
9chrome.webNavigation.onBeforeNavigate.addListener(async (details) => {
10 const url = new URL(details.url);
11 if (!blockLoading && url.hostname.endsWith("discord.com")) {
12 console.log("Blocking", details.url);
13 blockLoading = true;
14 collectedUrls.clear();
15 }
16});
17
18async function doTheThing(urls, tabId) {
19 console.log("Doing", urls, tabId);
20
21 blockLoading = false;
22
23 try {
24 await chrome.scripting.executeScript({
25 target: { tabId },
26 world: "MAIN",
27 args: [urls],
28 func: async (urls) => {
29 try {
30 await window._moonlightBrowserInit();
31 } catch (e) {
32 console.log(e);
33 }
34
35 const scripts = [...document.querySelectorAll("script")].filter(
36 (script) => script.src && urls.some((url) => url.includes(script.src))
37 );
38
39 // backwards
40 urls.reverse();
41 for (const url of urls) {
42 const script = scripts.find((script) => url.includes(script.src));
43 console.log("adding new script", script);
44
45 const newScript = document.createElement("script");
46 for (const { name, value } of script.attributes) {
47 newScript.setAttribute(name, value);
48 }
49
50 script.remove();
51 document.documentElement.appendChild(newScript);
52 }
53 }
54 });
55 } catch (e) {
56 console.log(e);
57 }
58
59 doing = false;
60 collectedUrls.clear();
61}
62
63chrome.webRequest.onBeforeRequest.addListener(
64 async (details) => {
65 if (starterUrls.some((url) => details.url.includes(url))) {
66 console.log("Adding", details.url);
67 collectedUrls.add(details.url);
68 }
69
70 if (collectedUrls.size === starterUrls.length) {
71 if (doing) return;
72 if (!blockLoading) return;
73 doing = true;
74 const urls = [...collectedUrls];
75 const tabId = details.tabId;
76
77 // yes this is a load-bearing sleep
78 setTimeout(() => doTheThing(urls, tabId), 0);
79 }
80
81 if (blockLoading) return { cancel: true };
82 },
83 {
84 urls: ["https://*.discord.com/assets/*.js"]
85 },
86 ["blocking"]
87);
88
89chrome.webRequest.onHeadersReceived.addListener(
90 (details) => {
91 return {
92 responseHeaders: details.responseHeaders.filter(
93 (header) => header.name.toLowerCase() !== "content-security-policy"
94 )
95 };
96 },
97 { urls: ["https://*.discord.com/*"] },
98 ["blocking", "responseHeaders"]
99);