this repo has no description
at main 2.7 kB view raw
1/* eslint-disable no-console */ 2/* eslint-disable no-undef */ 3 4const scriptUrls = ["web.", "sentry."]; 5let blockedScripts = new Set(); 6 7chrome.webRequest.onBeforeRequest.addListener( 8 async (details) => { 9 if (details.tabId === -1) return; 10 11 const url = new URL(details.url); 12 const hasUrl = scriptUrls.some((scriptUrl) => { 13 return ( 14 details.url.includes(scriptUrl) && 15 !url.searchParams.has("inj") && 16 (url.host.endsWith("discord.com") || url.host.endsWith("discordapp.com")) 17 ); 18 }); 19 if (hasUrl) blockedScripts.add(details.url); 20 21 if (blockedScripts.size === scriptUrls.length) { 22 const blockedScriptsCopy = Array.from(blockedScripts); 23 blockedScripts.clear(); 24 25 setTimeout(async () => { 26 console.log("Starting moonlight"); 27 await chrome.scripting.executeScript({ 28 target: { tabId: details.tabId }, 29 world: "MAIN", 30 args: [blockedScriptsCopy], 31 func: async (blockedScripts) => { 32 console.log("Initializing moonlight"); 33 try { 34 await window._moonlightBrowserInit(); 35 } catch (e) { 36 console.error(e); 37 } 38 39 console.log("Readding scripts"); 40 try { 41 const scripts = [...document.querySelectorAll("script")].filter( 42 (script) => script.src && blockedScripts.some((url) => url.includes(script.src)) 43 ); 44 45 blockedScripts.reverse(); 46 for (const url of blockedScripts) { 47 if (url.includes("/sentry.")) continue; 48 49 const script = scripts.find((script) => url.includes(script.src)); 50 const newScript = document.createElement("script"); 51 for (const attr of script.attributes) { 52 if (attr.name === "src") attr.value += "?inj"; 53 newScript.setAttribute(attr.name, attr.value); 54 } 55 script.remove(); 56 document.documentElement.appendChild(newScript); 57 } 58 } catch (e) { 59 console.error(e); 60 } 61 } 62 }); 63 }, 0); 64 } 65 66 if (hasUrl) return { cancel: true }; 67 }, 68 { 69 urls: ["https://*.discord.com/assets/*.js", "https://*.discordapp.com/assets/*.js"] 70 }, 71 ["blocking"] 72); 73 74chrome.webRequest.onHeadersReceived.addListener( 75 (details) => { 76 return { 77 responseHeaders: details.responseHeaders.filter( 78 (header) => header.name.toLowerCase() !== "content-security-policy" 79 ) 80 }; 81 }, 82 { urls: ["https://*.discord.com/*", "https://*.discordapp.com/*"] }, 83 ["blocking", "responseHeaders"] 84);