Middleware can now be loaded from indexedDB, merge into main soon!!

This commit is contained in:
wearrrrr 2024-07-06 14:33:24 -05:00
parent e9167fda9a
commit d58e31e8d9
6 changed files with 48 additions and 31 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 110 KiB

View file

@ -5,17 +5,35 @@ importScripts("/uv/uv.bundle.js");
importScripts("/uv.config.js");
importScripts(__uv$config.sw);
importScripts("./workerware/workerware.js");
importScripts("./marketplace/adblock/index.js")
const ww = new WorkerWare({
debug: true,
});
ww.use({
function: self.adblockExt.filterRequest,
events: ["fetch"],
name: "Adblock"
});
function loadExtensionScripts() {
try {
let db = indexedDB.open("AluDB", 1);
db.onsuccess = () => {
let transaction = db.result.transaction("InstalledExtensions", "readonly");
let store = transaction.objectStore("InstalledExtensions");
let request = store.getAll();
request.onsuccess = () => {
let extensions = request.result;
extensions.forEach((extension) => {
eval(atob(extension.script));
ww.use({
function: self[extension.entryNamespace][extension.entryFunc],
name: extension.title,
events: ["fetch"],
}); // Use extension middleware
});
};
};
} catch (err) {
console.error(`Failed load extension scripts: ${err}`);
}
}
loadExtensionScripts();
const uv = new UVServiceWorker();

View file

@ -295,7 +295,7 @@
}
}
function updateProxiedFavicon(iframe: HTMLIFrameElement, hasErrored = false) {
function updateProxiedFavicon(iframe: HTMLIFrameElement) {
if (!iframe) return;
let proxiedFavicon = document.getElementById("proxied-favicon") as HTMLImageElement;
if (iframe) {

View file

@ -3,12 +3,8 @@
"title": "Adblocker",
"version": "0.0.1",
"image": "/marketplace/adblock/adblock.png",
"script": "/marketplace/adblock/adblocker.js"
},
"dev.wearr.adblock2": {
"title": "Adblocker2",
"version": "0.0.2",
"image": "/marketplace/adblock/adblock.png",
"script": "/marketplace/adblock/adblocker.js"
"script": "/marketplace/adblock/index.js",
"entryNamespace": "adblockExt",
"entryFunc": "filterRequest"
}
}

View file

@ -58,7 +58,7 @@ const { title, optionalPreloads } = Astro.props;
<script is:inline>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
window.dataLayer.push(arguments);
}
gtag("js", new Date());

View file

@ -12,6 +12,8 @@ type MarketplaceItem = {
version: string | number;
image: string;
script: string;
entryNamespace: string
entryFunc: string;
};
---
@ -45,7 +47,7 @@ type MarketplaceItem = {
import marketplaceManifest from "../../json/marketplace.json";
const installButtons = document.getElementsByClassName(
"marketplace-install-btn"
) as HTMLCollectionOf<HTMLButtonElement>;
);
Array.from(installButtons).forEach((btn) => {
btn.addEventListener("click", async (event) => {
const ele = event.target as HTMLButtonElement;
@ -93,35 +95,34 @@ type MarketplaceItem = {
});
});
type MarketplaceItem = {
title: string;
version: string | number;
image: string;
script: string;
scriptBtoa: string | null;
};
async function getMarketplaceObj(slug: string): Promise<ExtensionMetadata> {
const manifest = (marketplaceManifest as unknown as { [key: string]: MarketplaceItem })[slug];
manifest.scriptBtoa = btoa(await fetch(manifest.script).then((res) => res.text()));
return manifest;
}
interface ExtensionMetadata {
title: string;
version: string | number;
script: string;
entryNamespace: string;
entryFunc: string;
scriptBtoa: string | null;
}
async function getMarketplaceObj(slug: string): Promise<ExtensionMetadata> {
const manifest = (marketplaceManifest as unknown as { [key: string]: ExtensionMetadata })[slug];
manifest.scriptBtoa = btoa(await fetch(manifest.script).then((res) => res.text()));
return manifest;
}
// Stupid eslint bug.
// eslint-disable-next-line no-unused-vars
enum EXT_RETURN {
// eslint-disable-next-line no-unused-vars
INSTALL_SUCCESS = 0,
// eslint-disable-next-line no-unused-vars
INSTALL_FAILED = -1,
// eslint-disable-next-line no-unused-vars
ALREADY_INSTALLED = 1,
}
async function installExtension(ext: ExtensionMetadata, slug: string) {
return new Promise(async (resolve, reject) => {
return new Promise<EXT_RETURN>((resolve, reject) => {
const request = window.indexedDB.open("AluDB", 1);
request.onupgradeneeded = (event) => {
const db = (event.target as IDBOpenDBRequest).result;
@ -138,6 +139,8 @@ type MarketplaceItem = {
title: ext.title,
version: ext.version,
script: ext.scriptBtoa,
entryNamespace: ext.entryNamespace,
entryFunc: ext.entryFunc,
};
// Check if the key already exists in the IDB
let slugCheck = store.get(slug);