From 91488e88f5600b56dd824b867b561b940eb6e265 Mon Sep 17 00:00:00 2001 From: wearrrrr Date: Fri, 19 Apr 2024 10:06:51 -0500 Subject: [PATCH] Tried to make progress on dynamic fetch events, currently does NOT work. --- public/marketplace/adblock/adblocker.sw.js | 58 +++++++++++----------- public/sw.js | 34 +++++++++---- src/components/ProxyRegistrar.astro | 2 +- src/pages/[lang]/marketplace.astro | 3 ++ 4 files changed, 56 insertions(+), 41 deletions(-) diff --git a/public/marketplace/adblock/adblocker.sw.js b/public/marketplace/adblock/adblocker.sw.js index 98f3b31..262d38c 100644 --- a/public/marketplace/adblock/adblocker.sw.js +++ b/public/marketplace/adblock/adblocker.sw.js @@ -1,36 +1,34 @@ -importScripts("/libcurl/index.js"); -importScripts("/epoxy/index.js"); -importScripts("/bare_transport.js"); -importScripts("/uv/uv.bundle.js"); -importScripts("/uv.config.js"); -importScripts(__uv$config.sw); - -const uv = new UVServiceWorker(); - let lowes = []; -let loadLowes = async () => { - await fetch ("/marketplace/adblock/peterlowes.json") - .then(response => response.json()).then(data => { - data.rules.forEach((rule) => { - lowes.push(rule["remote-domains"]); - }) - }) + +async function loadLowes() { + await fetch("/marketplace/adblock/peterlowes.json") + .then(response => response.json()) + .then(data => { + data.rules.forEach((rule) => { + lowes.push(rule["remote-domains"]); + }); + }); console.log("Loaded Peter Lowes' List!"); - console.log(lowes) + console.log(lowes); } + loadLowes(); +function registerFetchListener() { + self.addEventListener("fetch", (event) => { + event.respondWith( + (async () => { + console.log(event.request.url); + if (lowes.some((lowe) => __uv$config.decodeUrl(event.request.url).includes(lowe))) { + return new Response("Blocked by Peter Lowes", {status: 403}); + } + if (event.request.url.startsWith(location.origin + __uv$config.prefix)) { + return await uv.fetch(event); + } + return await fetch(event.request); + })() + ); + }); +} -self.addEventListener("fetch", (event) => { - event.respondWith( - (async () => { - if (lowes.some((lowe) => __uv$config.decodeUrl(event.request.url).includes(lowe))){ - return new Response("Blocked by Peter Lowes", {status: 403}); - } - if (event.request.url.startsWith(location.origin + __uv$config.prefix)) { - return await uv.fetch(event); - } - return await fetch(event.request); - })() - ); -}); +registerFetchListener(); diff --git a/public/sw.js b/public/sw.js index 31315ee..ea7876f 100644 --- a/public/sw.js +++ b/public/sw.js @@ -7,13 +7,27 @@ importScripts(__uv$config.sw); const uv = new UVServiceWorker(); -self.addEventListener("fetch", (event) => { - event.respondWith( - (async () => { - if (event.request.url.startsWith(location.origin + __uv$config.prefix)) { - return await uv.fetch(event); - } - return await fetch(event.request); - })() - ); -}); \ No newline at end of file +async function loadHooks() { + const db = await new Promise((resolve, reject) => { + const request = indexedDB.open("AluDB", 1); + request.onsuccess = () => resolve(request.result); + request.onerror = reject; + }); + + const transaction = db.transaction("InstalledExtensions", "readwrite"); + const objectStore = transaction.objectStore("InstalledExtensions"); + const extensions = await new Promise((resolve, reject) => { + const request = objectStore.getAll(); + request.onsuccess = () => resolve(request.result); + request.onerror = reject; + }); + + extensions.forEach((extension) => { + if (extension.serviceWorkerExtension) { + // Load the base64 encoded script contents; + importScripts(`data:text/plain;base64,${extension.script}`); + } + }); +} + +loadHooks(); diff --git a/src/components/ProxyRegistrar.astro b/src/components/ProxyRegistrar.astro index 76d76f4..7e724ac 100644 --- a/src/components/ProxyRegistrar.astro +++ b/src/components/ProxyRegistrar.astro @@ -138,9 +138,9 @@ let request = store.getAll(); request.onsuccess = () => { let extensions = request.result; - console.log(extensions); extensions.forEach((extension: any) => { // Eval the extension script inside of the iframe + if (extension.serviceWorkerExtension) return; if (iframe.contentWindow) { console.log(`Evaluating extension "${extension.title}" inside of the iframe.`); (iframe.contentWindow as any).eval(atob(extension.script)); diff --git a/src/pages/[lang]/marketplace.astro b/src/pages/[lang]/marketplace.astro index 8130e92..246e1d5 100644 --- a/src/pages/[lang]/marketplace.astro +++ b/src/pages/[lang]/marketplace.astro @@ -99,6 +99,7 @@ type MarketplaceItem = { image: string; script: string; scriptBtoa: string | null; + serviceWorkerExtension?: boolean; }; async function getMarketplaceObj(slug: string): Promise { @@ -112,6 +113,7 @@ type MarketplaceItem = { version: string | number; script: string; scriptBtoa: string | null; + serviceWorkerExtension?: boolean; } enum EXT_RETURN { @@ -138,6 +140,7 @@ type MarketplaceItem = { title: ext.title, version: ext.version, script: ext.scriptBtoa, + serviceWorkerExtension: ext.serviceWorkerExtension, }; // Check if the key already exists in the IDB let slugCheck = store.get(slug);