Middleware can now be loaded from indexedDB, merge into main soon!!
This commit is contained in:
parent
e9167fda9a
commit
d58e31e8d9
6 changed files with 48 additions and 31 deletions
BIN
public/marketplace/adblock/adblock.png
Normal file
BIN
public/marketplace/adblock/adblock.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 110 KiB |
28
public/sw.js
28
public/sw.js
|
|
@ -5,17 +5,35 @@ importScripts("/uv/uv.bundle.js");
|
||||||
importScripts("/uv.config.js");
|
importScripts("/uv.config.js");
|
||||||
importScripts(__uv$config.sw);
|
importScripts(__uv$config.sw);
|
||||||
importScripts("./workerware/workerware.js");
|
importScripts("./workerware/workerware.js");
|
||||||
importScripts("./marketplace/adblock/index.js")
|
|
||||||
|
|
||||||
const ww = new WorkerWare({
|
const ww = new WorkerWare({
|
||||||
debug: true,
|
debug: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
ww.use({
|
function loadExtensionScripts() {
|
||||||
function: self.adblockExt.filterRequest,
|
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"],
|
events: ["fetch"],
|
||||||
name: "Adblock"
|
}); // Use extension middleware
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Failed load extension scripts: ${err}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loadExtensionScripts();
|
||||||
|
|
||||||
const uv = new UVServiceWorker();
|
const uv = new UVServiceWorker();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -295,7 +295,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateProxiedFavicon(iframe: HTMLIFrameElement, hasErrored = false) {
|
function updateProxiedFavicon(iframe: HTMLIFrameElement) {
|
||||||
if (!iframe) return;
|
if (!iframe) return;
|
||||||
let proxiedFavicon = document.getElementById("proxied-favicon") as HTMLImageElement;
|
let proxiedFavicon = document.getElementById("proxied-favicon") as HTMLImageElement;
|
||||||
if (iframe) {
|
if (iframe) {
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,8 @@
|
||||||
"title": "Adblocker",
|
"title": "Adblocker",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"image": "/marketplace/adblock/adblock.png",
|
"image": "/marketplace/adblock/adblock.png",
|
||||||
"script": "/marketplace/adblock/adblocker.js"
|
"script": "/marketplace/adblock/index.js",
|
||||||
},
|
"entryNamespace": "adblockExt",
|
||||||
"dev.wearr.adblock2": {
|
"entryFunc": "filterRequest"
|
||||||
"title": "Adblocker2",
|
|
||||||
"version": "0.0.2",
|
|
||||||
"image": "/marketplace/adblock/adblock.png",
|
|
||||||
"script": "/marketplace/adblock/adblocker.js"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ const { title, optionalPreloads } = Astro.props;
|
||||||
<script is:inline>
|
<script is:inline>
|
||||||
window.dataLayer = window.dataLayer || [];
|
window.dataLayer = window.dataLayer || [];
|
||||||
function gtag() {
|
function gtag() {
|
||||||
dataLayer.push(arguments);
|
window.dataLayer.push(arguments);
|
||||||
}
|
}
|
||||||
gtag("js", new Date());
|
gtag("js", new Date());
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ type MarketplaceItem = {
|
||||||
version: string | number;
|
version: string | number;
|
||||||
image: string;
|
image: string;
|
||||||
script: string;
|
script: string;
|
||||||
|
entryNamespace: string
|
||||||
|
entryFunc: string;
|
||||||
};
|
};
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -45,7 +47,7 @@ type MarketplaceItem = {
|
||||||
import marketplaceManifest from "../../json/marketplace.json";
|
import marketplaceManifest from "../../json/marketplace.json";
|
||||||
const installButtons = document.getElementsByClassName(
|
const installButtons = document.getElementsByClassName(
|
||||||
"marketplace-install-btn"
|
"marketplace-install-btn"
|
||||||
) as HTMLCollectionOf<HTMLButtonElement>;
|
);
|
||||||
Array.from(installButtons).forEach((btn) => {
|
Array.from(installButtons).forEach((btn) => {
|
||||||
btn.addEventListener("click", async (event) => {
|
btn.addEventListener("click", async (event) => {
|
||||||
const ele = event.target as HTMLButtonElement;
|
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 {
|
interface ExtensionMetadata {
|
||||||
title: string;
|
title: string;
|
||||||
version: string | number;
|
version: string | number;
|
||||||
script: string;
|
script: string;
|
||||||
|
entryNamespace: string;
|
||||||
|
entryFunc: string;
|
||||||
scriptBtoa: string | null;
|
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 {
|
enum EXT_RETURN {
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
INSTALL_SUCCESS = 0,
|
INSTALL_SUCCESS = 0,
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
INSTALL_FAILED = -1,
|
INSTALL_FAILED = -1,
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
ALREADY_INSTALLED = 1,
|
ALREADY_INSTALLED = 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
async function installExtension(ext: ExtensionMetadata, slug: string) {
|
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);
|
const request = window.indexedDB.open("AluDB", 1);
|
||||||
request.onupgradeneeded = (event) => {
|
request.onupgradeneeded = (event) => {
|
||||||
const db = (event.target as IDBOpenDBRequest).result;
|
const db = (event.target as IDBOpenDBRequest).result;
|
||||||
|
|
@ -138,6 +139,8 @@ type MarketplaceItem = {
|
||||||
title: ext.title,
|
title: ext.title,
|
||||||
version: ext.version,
|
version: ext.version,
|
||||||
script: ext.scriptBtoa,
|
script: ext.scriptBtoa,
|
||||||
|
entryNamespace: ext.entryNamespace,
|
||||||
|
entryFunc: ext.entryFunc,
|
||||||
};
|
};
|
||||||
// Check if the key already exists in the IDB
|
// Check if the key already exists in the IDB
|
||||||
let slugCheck = store.get(slug);
|
let slugCheck = store.get(slug);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue