Tried to make progress on dynamic fetch events, currently does NOT work.
This commit is contained in:
parent
fdae47c9b3
commit
91488e88f5
4 changed files with 56 additions and 41 deletions
|
|
@ -1,29 +1,24 @@
|
|||
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 () => {
|
||||
|
||||
async function loadLowes() {
|
||||
await fetch("/marketplace/adblock/peterlowes.json")
|
||||
.then(response => response.json()).then(data => {
|
||||
.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});
|
||||
}
|
||||
|
|
@ -34,3 +29,6 @@ self.addEventListener("fetch", (event) => {
|
|||
})()
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
registerFetchListener();
|
||||
|
|
|
|||
32
public/sw.js
32
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);
|
||||
})()
|
||||
);
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -99,6 +99,7 @@ type MarketplaceItem = {
|
|||
image: string;
|
||||
script: string;
|
||||
scriptBtoa: string | null;
|
||||
serviceWorkerExtension?: boolean;
|
||||
};
|
||||
|
||||
async function getMarketplaceObj(slug: string): Promise<ExtensionMetadata> {
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue