Page plugin handling (uv.config.inject)

Next up, workerwear for SW plugin handling
This commit is contained in:
MotorTruck1221 2024-10-21 23:58:52 -06:00
parent fff4d0b286
commit 952337be6e
No known key found for this signature in database
GPG key ID: 08F417E2B8B61EA4
4 changed files with 27 additions and 20 deletions

View file

@ -41,7 +41,6 @@ async function setupDB(db: ModelStatic<CatalogModel>) {
tags: ["Theme", "Simple"], tags: ["Theme", "Simple"],
payload: "gruvbox.css", payload: "gruvbox.css",
type: "theme", type: "theme",
entryFunc: null
}, },
{ {
package_name: "com.nebula.oled", package_name: "com.nebula.oled",
@ -53,7 +52,6 @@ async function setupDB(db: ModelStatic<CatalogModel>) {
tags: ["Theme", "Simple", "Sleek"], tags: ["Theme", "Simple", "Sleek"],
payload: "oled.css", payload: "oled.css",
type: "theme", type: "theme",
entryFunc: null
}, },
{ {
package_name: "com.nebula.lightTheme", package_name: "com.nebula.lightTheme",
@ -65,7 +63,6 @@ async function setupDB(db: ModelStatic<CatalogModel>) {
tags: ["Theme", "Simple", "Light"], tags: ["Theme", "Simple", "Light"],
payload: "light.css", payload: "light.css",
type: "theme", type: "theme",
entryFunc: null
}, },
{ {
package_name: "com.nebula.retro", package_name: "com.nebula.retro",
@ -77,7 +74,6 @@ async function setupDB(db: ModelStatic<CatalogModel>) {
tags: ["Theme", "Simple", "Dark", "Retro"], tags: ["Theme", "Simple", "Dark", "Retro"],
payload: "retro.css", payload: "retro.css",
type: "theme", type: "theme",
entryFunc: null
}, },
{ {
package_name: "com.nebula.darkMode", package_name: "com.nebula.darkMode",
@ -89,7 +85,6 @@ async function setupDB(db: ModelStatic<CatalogModel>) {
tags: ["Plugin", "Dark Mode", "Noctura"], tags: ["Plugin", "Dark Mode", "Noctura"],
payload: "index.js", payload: "index.js",
type: "plugin", type: "plugin",
entryFunc: "entryFunc"
} }
]; ];
const dbItems = await db.findAll(); const dbItems = await db.findAll();

View file

@ -28,7 +28,6 @@ interface Catalog {
background_video: string; background_video: string;
payload: string; payload: string;
type: CatalogType; type: CatalogType;
entryFunc: string;
} }
interface CatalogModel interface CatalogModel
@ -47,7 +46,6 @@ const catalogAssets = db.define<CatalogModel>("catalog_assets", {
background_video: { type: DataTypes.TEXT, allowNull: true }, background_video: { type: DataTypes.TEXT, allowNull: true },
payload: { type: DataTypes.TEXT }, payload: { type: DataTypes.TEXT },
type: { type: DataTypes.TEXT }, type: { type: DataTypes.TEXT },
entryFunc: { type: DataTypes.STRING }
}); });
function marketplaceAPI(app: FastifyInstance) { function marketplaceAPI(app: FastifyInstance) {
@ -81,7 +79,6 @@ function marketplaceAPI(app: FastifyInstance) {
background_video: asset.background_video, background_video: asset.background_video,
payload: asset.payload, payload: asset.payload,
type: asset.type, type: asset.type,
entryFunc: asset.entryFunc
}; };
return acc; return acc;
}, {}); }, {});
@ -109,7 +106,6 @@ function marketplaceAPI(app: FastifyInstance) {
background_video: packageRow.get("background_video"), background_video: packageRow.get("background_video"),
payload: packageRow.get("payload"), payload: packageRow.get("payload"),
type: packageRow.get("type"), type: packageRow.get("type"),
entryFunc: packageRow.get("entryFunc")
}; };
reply.send(details); reply.send(details);
} catch (error) { } catch (error) {
@ -132,7 +128,6 @@ function marketplaceAPI(app: FastifyInstance) {
background_video: string; background_video: string;
background_image: string; background_image: string;
type: CatalogType; type: CatalogType;
entryFunc: string;
}; };
}>; }>;
interface VerifyStatus { interface VerifyStatus {
@ -199,7 +194,6 @@ function marketplaceAPI(app: FastifyInstance) {
background_video: request.body.background_video, background_video: request.body.background_video,
background_image: request.body.background_image, background_image: request.body.background_image,
type: request.body.type as CatalogType, type: request.body.type as CatalogType,
entryFunc: request.body.entryFunc
}; };
await catalogAssets.create({ await catalogAssets.create({
package_name: body.package_name, package_name: body.package_name,
@ -213,7 +207,6 @@ function marketplaceAPI(app: FastifyInstance) {
background_video: body.background_video, background_video: body.background_video,
background_image: body.background_image, background_image: body.background_image,
type: body.type, type: body.type,
entryFunc: body.entryFunc
}); });
const assets = fileURLToPath(new URL("../database_assets", import.meta.url)); const assets = fileURLToPath(new URL("../database_assets", import.meta.url));
try { try {

View file

@ -35,10 +35,15 @@ const marketPlaceSettings = {
let plugins = localStorage.getItem(PluginSettings.plugins) as any; let plugins = localStorage.getItem(PluginSettings.plugins) as any;
plugins ? (plugins = JSON.parse(plugins)) : (plugins = []); plugins ? (plugins = JSON.parse(plugins)) : (plugins = []);
//@ts-ignore //@ts-ignore
if (!plugins.find(({ name }) => name === packageName)) { const plugin = plugins.find(({ name }) => name === packageName) as Plugin
plugins.push({name: packageName, src: p.plugin.src, type: p.plugin.type, entryFunc: p.plugin.entryFunc} as unknown as Plugin) if (!plugin) {
plugins.push({name: packageName, src: p.plugin.src, type: p.plugin.type} as unknown as Plugin)
localStorage.setItem(PluginSettings.plugins, JSON.stringify(plugins)); localStorage.setItem(PluginSettings.plugins, JSON.stringify(plugins));
} }
else if (plugin && plugin.remove) {
plugin.remove = false;
localStorage.setItem(Settings.PluginSettings.plugins, JSON.stringify(plugins));
}
resolve(); resolve();
} }
}); });
@ -60,9 +65,9 @@ const marketPlaceSettings = {
let plugins = localStorage.getItem(PluginSettings.plugins) as any; let plugins = localStorage.getItem(PluginSettings.plugins) as any;
plugins ? (plugins = JSON.parse(plugins)) : (plugins = []); plugins ? (plugins = JSON.parse(plugins)) : (plugins = []);
//@ts-ignore //@ts-ignore
if (plugins.find(({name}) => name === packageName)) { const plugin = plugins.find(({name}) => name === packageName);
const idx = plugins.indexOf(packageName); if (plugin) {
plugins.splice(idx, 1); plugin.remove = true;
localStorage.setItem(PluginSettings.plugins, JSON.stringify(plugins)); localStorage.setItem(PluginSettings.plugins, JSON.stringify(plugins));
} }
resolve(); resolve();
@ -72,15 +77,28 @@ const marketPlaceSettings = {
handlePlugins: function(worker: never | ServiceWorkerRegistration) { handlePlugins: function(worker: never | ServiceWorkerRegistration) {
return new Promise<void>((resolve) => { return new Promise<void>((resolve) => {
const plugins = JSON.parse(localStorage.getItem(Settings.PluginSettings.plugins) as string) || []; const plugins = JSON.parse(localStorage.getItem(Settings.PluginSettings.plugins) as string) || [];
if (plugins.length === 0) {
console.log('Plugin length is not greater then 0. Resolving.');
return resolve();
}
plugins.forEach(async (plugin: Plugin) => { plugins.forEach(async (plugin: Plugin) => {
if (plugin.type === "page") { if (plugin.type === "page") {
const pluginScript = await fetch(`/packages/${plugin.name}/${plugin.src}`).then((res) => res.text()); const pluginScript = await fetch(`/packages/${plugin.name}/${plugin.src}`).then((res) => res.text());
const script = eval(pluginScript); const script = eval(pluginScript);
const inject = script() as unknown as SWPlugin; const inject = script() as unknown as SWPlugin;
if (plugin.remove) {
const idx = plugins.indexOf(plugin.name);
worker.active?.postMessage([{remove: true, host: inject.host, html: inject.html, injectTo: inject.injectTo}] as SWPlugin[]);
plugins.splice(idx, 1);
localStorage.setItem(Settings.PluginSettings.plugins, JSON.stringify(plugins));
}
else {
worker.active?.postMessage([{host: inject.host, html: inject.html, injectTo: inject.injectTo}] as SWPlugin[]); worker.active?.postMessage([{host: inject.host, html: inject.html, injectTo: inject.injectTo}] as SWPlugin[]);
} }
}); //only resolve AFTER we have postMessaged to the SW.
resolve(); resolve();
}
});
}); });
}, },
changeTheme: async function ( changeTheme: async function (

View file

@ -21,12 +21,13 @@ interface Plugin {
name: string; name: string;
src: string; src: string;
type: PluginType; type: PluginType;
entryFunc: () => unknown | unknown; remove?: boolean;
} }
interface SWPlugin { interface SWPlugin {
host: string; host: string;
html: string; html: string;
injectTo: "head" | "body"; injectTo: "head" | "body";
remove?: boolean;
} }
interface Package { interface Package {
theme?: { theme?: {