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,
});
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 timeout = 2000;
console.log(code)
console.log(code);
switch (code) {
case 0:
notifMessage = `Installed ${title} Successfully!`;
@ -78,17 +81,17 @@ type MarketplaceItem = {
setTimeout(() => {
notification.dismiss(installNotif);
notification.options.duration = 2000;
notification.success(notifMessage)
notification.success(notifMessage);
notification.options.duration = 999999;
}, timeout)
}).catch(() => {
}, 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;
};
function getMarketplaceObj(slug: string): ExtensionMetadata {
return (marketplaceManifest as { [key: string]: MarketplaceItem })[slug];
async function getMarketplaceObj(slug: string): Promise<ExtensionMetadata> {
const manifest = (marketplaceManifest as { [key: string]: MarketplaceItem })[slug];
manifest.script = btoa(await fetch(manifest.script).then((res) => res.text()));
return manifest;
}
interface ExtensionMetadata {
@ -116,7 +121,7 @@ type MarketplaceItem = {
}
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);
request.onupgradeneeded = (event) => {
const db = (event.target as IDBOpenDBRequest).result;
@ -136,14 +141,14 @@ type MarketplaceItem = {
};
// Check if the key already exists in the IDB
let slugCheck = store.get(slug);
slugCheck.onsuccess = () => {
console.log(slugCheck.result)
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}!`)
console.error(`Error installing ${slug}!`);
reject(EXT_RETURN.INSTALL_FAILED);
};
addRequest.onsuccess = () => {
@ -156,8 +161,7 @@ type MarketplaceItem = {
reject(EXT_RETURN.INSTALL_FAILED);
};
};
})
});
}
function InitIDB() {