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,37 +58,40 @@ 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 notifMessage = ""; let obj = await getMarketplaceObj(ele.dataset.slug);
let timeout = 2000; installExtension(obj, ele.dataset.slug)
console.log(code) .then((code) => {
switch (code) { let notifMessage = "";
case 0: let timeout = 2000;
notifMessage = `Installed ${title} Successfully!`; console.log(code);
break; switch (code) {
case 1: case 0:
notifMessage = `${title} is already installed!`; notifMessage = `Installed ${title} Successfully!`;
timeout = 0; break;
break; case 1:
case -1: notifMessage = `${title} is already installed!`;
// We should NEVER get here, but just in case. timeout = 0;
notifMessage = `Failed to install ${title}!`; break;
break; case -1:
} // We should NEVER get here, but just in case.
setTimeout(() => { notifMessage = `Failed to install ${title}!`;
break;
}
setTimeout(() => {
notification.dismiss(installNotif);
notification.options.duration = 2000;
notification.success(notifMessage);
notification.options.duration = 999999;
}, timeout);
})
.catch(() => {
notification.dismiss(installNotif); notification.dismiss(installNotif);
notification.options.duration = 2000; notification.options.duration = 2000;
notification.success(notifMessage) notification.error(`Failed to install ${title}!`);
notification.options.duration = 999999; notification.options.duration = 999999;
}, timeout) });
}
}).catch(() => {
notification.dismiss(installNotif);
notification.options.duration = 2000;
notification.error(`Failed to install ${title}!`);
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,48 +121,47 @@ 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;
if (!db.objectStoreNames.contains("InstalledExtensions")) { if (!db.objectStoreNames.contains("InstalledExtensions")) {
db.createObjectStore("InstalledExtensions", { keyPath: "slug" }); db.createObjectStore("InstalledExtensions", { keyPath: "slug" });
}
};
request.onsuccess = async (event) => {
const db = (event.target as IDBOpenDBRequest).result;
const transaction = db.transaction("InstalledExtensions", "readwrite");
const store = transaction.objectStore("InstalledExtensions");
const extensionObject = {
slug: slug,
title: ext.title,
version: ext.version,
script: ext.script,
};
// Check if the key already exists in the IDB
let slugCheck = store.get(slug);
slugCheck.onsuccess = async () => {
console.log(slugCheck.result);
if (slugCheck.result != undefined) {
resolve(EXT_RETURN.ALREADY_INSTALLED);
} else {
const addRequest = store.add(extensionObject);
addRequest.onerror = () => {
console.error(`Error installing ${slug}!`);
reject(EXT_RETURN.INSTALL_FAILED);
};
addRequest.onsuccess = () => {
resolve(EXT_RETURN.INSTALL_SUCCESS);
};
} }
}; };
request.onsuccess = async (event) => { slugCheck.onerror = () => {
const db = (event.target as IDBOpenDBRequest).result; console.error("Error checking install status!");
const transaction = db.transaction("InstalledExtensions", "readwrite"); reject(EXT_RETURN.INSTALL_FAILED);
const store = transaction.objectStore("InstalledExtensions");
const extensionObject = {
slug: slug,
title: ext.title,
version: ext.version,
script: ext.script,
};
// Check if the key already exists in the IDB
let slugCheck = store.get(slug);
slugCheck.onsuccess = () => {
console.log(slugCheck.result)
if (slugCheck.result != undefined) {
resolve(EXT_RETURN.ALREADY_INSTALLED);
} else {
const addRequest = store.add(extensionObject);
addRequest.onerror = () => {
console.error(`Error installing ${slug}!`)
reject(EXT_RETURN.INSTALL_FAILED);
};
addRequest.onsuccess = () => {
resolve(EXT_RETURN.INSTALL_SUCCESS);
};
}
};
slugCheck.onerror = () => {
console.error("Error checking install status!");
reject(EXT_RETURN.INSTALL_FAILED);
};
}; };
}) };
});
} }
function InitIDB() { function InitIDB() {