Script is now fetched and btoa'd inside of indexeddb.

This commit is contained in:
wearrrrr 2024-04-17 09:47:10 -05:00
parent 0b87af884e
commit 8b3abae5f5
2 changed files with 74 additions and 69 deletions

View file

@ -0,0 +1 @@
console.log("Hello World!");

View file

@ -58,10 +58,13 @@ type MarketplaceItem = {
ripple: true, ripple: true,
}); });
let installNotif = notification.success(`Installing ${title}...`); let installNotif = notification.success(`Installing ${title}...`);
if (ele.dataset.slug) installExtension(getMarketplaceObj(ele.dataset.slug), ele.dataset.slug).then((code) => { if (ele.dataset.slug) {
let obj = await getMarketplaceObj(ele.dataset.slug);
installExtension(obj, ele.dataset.slug)
.then((code) => {
let notifMessage = ""; let notifMessage = "";
let timeout = 2000; let timeout = 2000;
console.log(code) console.log(code);
switch (code) { switch (code) {
case 0: case 0:
notifMessage = `Installed ${title} Successfully!`; notifMessage = `Installed ${title} Successfully!`;
@ -78,17 +81,17 @@ type MarketplaceItem = {
setTimeout(() => { setTimeout(() => {
notification.dismiss(installNotif); notification.dismiss(installNotif);
notification.options.duration = 2000; notification.options.duration = 2000;
notification.success(notifMessage) notification.success(notifMessage);
notification.options.duration = 999999; notification.options.duration = 999999;
}, timeout) }, timeout);
})
}).catch(() => { .catch(() => {
notification.dismiss(installNotif); notification.dismiss(installNotif);
notification.options.duration = 2000; notification.options.duration = 2000;
notification.error(`Failed to install ${title}!`); notification.error(`Failed to install ${title}!`);
notification.options.duration = 999999; notification.options.duration = 999999;
}); });
else console.error("No slug found!"); }
}); });
}); });
@ -99,8 +102,10 @@ type MarketplaceItem = {
script: string; script: string;
}; };
function getMarketplaceObj(slug: string): ExtensionMetadata { async function getMarketplaceObj(slug: string): Promise<ExtensionMetadata> {
return (marketplaceManifest as { [key: string]: MarketplaceItem })[slug]; const manifest = (marketplaceManifest as { [key: string]: MarketplaceItem })[slug];
manifest.script = btoa(await fetch(manifest.script).then((res) => res.text()));
return manifest;
} }
interface ExtensionMetadata { interface ExtensionMetadata {
@ -116,7 +121,7 @@ type MarketplaceItem = {
} }
async function installExtension(ext: ExtensionMetadata, slug: string) { async function installExtension(ext: ExtensionMetadata, slug: string) {
return new Promise((resolve, reject) => { return new Promise(async (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;
@ -136,14 +141,14 @@ type MarketplaceItem = {
}; };
// 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);
slugCheck.onsuccess = () => { slugCheck.onsuccess = async () => {
console.log(slugCheck.result) console.log(slugCheck.result);
if (slugCheck.result != undefined) { if (slugCheck.result != undefined) {
resolve(EXT_RETURN.ALREADY_INSTALLED); resolve(EXT_RETURN.ALREADY_INSTALLED);
} else { } else {
const addRequest = store.add(extensionObject); const addRequest = store.add(extensionObject);
addRequest.onerror = () => { addRequest.onerror = () => {
console.error(`Error installing ${slug}!`) console.error(`Error installing ${slug}!`);
reject(EXT_RETURN.INSTALL_FAILED); reject(EXT_RETURN.INSTALL_FAILED);
}; };
addRequest.onsuccess = () => { addRequest.onsuccess = () => {
@ -156,8 +161,7 @@ type MarketplaceItem = {
reject(EXT_RETURN.INSTALL_FAILED); reject(EXT_RETURN.INSTALL_FAILED);
}; };
}; };
}) });
} }
function InitIDB() { function InitIDB() {