File size: 10,296 Bytes
89a8d13 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
//marketplace code & handlers
import { Settings } from "../index";
import {
type Package,
type PackageType,
type Plugin,
type PluginType,
type SWPagePlugin,
type SWPlugin
} from "../types";
const AppearanceSettings = {
themes: "nebula||themes",
themeName: "nebula||themeName",
stylePayload: "nebula||stylepayload",
video: "nebula||video",
image: "nebula||image"
};
const PluginSettings = {
plugins: "nebula||plugins"
};
const MarketPlaceExtras = {
proxy: "nebula||marketplaceProxy",
hostname: "nebula||marketplaceHostname"
};
const marketPlaceSettings = {
install: function (p: Package, packageName: string, payload?: any) {
return new Promise<void>((resolve) => {
if (p.theme) {
let themes = localStorage.getItem(AppearanceSettings.themes) as any;
themes ? (themes = JSON.parse(themes)) : (themes = []);
if (!themes.find((theme: any) => theme === packageName)) {
themes.push(packageName);
localStorage.setItem(AppearanceSettings.themes, JSON.stringify(themes));
this.changeTheme(false, payload, p.theme.video, p.theme.bgImage, packageName);
}
resolve();
}
if (p.plugin) {
let plugins = localStorage.getItem(PluginSettings.plugins) as any;
plugins ? (plugins = JSON.parse(plugins)) : (plugins = []);
//@ts-ignore
const plugin = plugins.find(({ name }) => name === packageName) 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));
} else if (plugin && plugin.remove) {
plugin.remove = false;
localStorage.setItem(Settings.PluginSettings.plugins, JSON.stringify(plugins));
}
resolve();
}
});
},
uninstall: function (p: PackageType, packageName: string) {
console.log(p);
return new Promise<void>((resolve) => {
if (p === "theme") {
let items = localStorage.getItem(AppearanceSettings.themes) as any;
items ? (items = JSON.parse(items)) : (items = []);
if (items.find((theme: any) => theme === packageName.toLowerCase())) {
const idx = items.indexOf(packageName.toLowerCase());
items.splice(idx, 1);
localStorage.setItem(AppearanceSettings.themes, JSON.stringify(items));
this.changeTheme(true);
}
resolve();
}
if (p === "plugin-page" || p === "plugin-sw") {
let plugins = localStorage.getItem(PluginSettings.plugins) as any;
plugins ? (plugins = JSON.parse(plugins)) : (plugins = []);
//@ts-ignore
const plugin = plugins.find(({ name }) => name === packageName.toLowerCase());
if (plugin) {
plugin.remove = true;
localStorage.setItem(PluginSettings.plugins, JSON.stringify(plugins));
}
resolve();
}
});
},
handlePlugins: function (worker: never | ServiceWorkerRegistration) {
return new Promise<void>((resolve) => {
let plugins =
JSON.parse(localStorage.getItem(Settings.PluginSettings.plugins) as string) || [];
const swPagePlugins: SWPagePlugin[] = [];
const swPlugins: SWPlugin[] = [];
if (plugins.length === 0) {
console.log("Plugin length is not greater then 0. Resolving.");
return resolve();
}
plugins.forEach(async (plugin: Plugin) => {
if (plugin.type === "page") {
const pluginScript = await fetch(
`/packages/${plugin.name.toLowerCase()}/${plugin.src}`
).then((res) => res.text());
const script = eval(pluginScript);
const inject = (await script()) as unknown as SWPagePlugin;
if (plugin.remove) {
plugins = plugins.filter(
//@ts-ignore freaking types BRO
({ name }) => name !== plugin.name.toLowerCase()
);
swPagePlugins.push({
remove: true,
host: inject.host,
html: inject.html,
injectTo: inject.injectTo,
type: "page"
});
} else {
swPagePlugins.push({
host: inject.host,
html: inject.html,
injectTo: inject.injectTo,
type: "page"
});
}
//only resolve AFTER we have postMessaged to the SW.
worker.active?.postMessage(swPagePlugins);
} else if (plugin.type === "serviceWorker") {
const pluginScript = await fetch(
`/packages/${plugin.name.toLowerCase()}/${plugin.src}`
).then((res) => res.text());
const script = eval(pluginScript);
const inject = (await script()) as unknown as SWPlugin;
if (plugin.remove) {
plugins = plugins.filter(
//@ts-ignore
({ name }) => name !== plugin.name.toLowerCase()
);
swPlugins.push({
remove: true,
function: inject.function.toString(),
name: plugin.name,
events: inject.events,
type: "serviceWorker"
});
} else {
swPlugins.push({
function: inject.function.toString(),
name: plugin.name,
events: inject.events,
type: "serviceWorker"
});
}
worker.active?.postMessage(swPlugins);
}
localStorage.setItem(Settings.PluginSettings.plugins, JSON.stringify(plugins));
resolve();
});
});
},
changeTheme: async function (
reset: Boolean,
payload?: any,
videoSource?: string,
bgSource?: string,
name?: string
) {
async function resetCSS() {
const stylesheet = document.getElementById("stylesheet")! as HTMLLinkElement;
localStorage.removeItem(AppearanceSettings.stylePayload);
localStorage.removeItem(AppearanceSettings.themeName);
stylesheet.href = "/nebula.css";
}
function resetVideo() {
localStorage.removeItem(AppearanceSettings.video);
const source = document.getElementById("nebulaVideo")! as HTMLVideoElement;
source.src = "";
}
function resetBGImage() {
localStorage.removeItem(AppearanceSettings.image);
const image = document.getElementById("nebulaImage")! as HTMLImageElement;
image.style.display = "none";
image.src = "";
}
if (reset === true) {
await resetCSS();
resetBGImage();
resetVideo();
}
if (videoSource || localStorage.getItem(AppearanceSettings.video)) {
resetBGImage();
resetVideo();
const source = document.getElementById("nebulaVideo")! as HTMLVideoElement;
if (!localStorage.getItem(AppearanceSettings.video)) {
localStorage.setItem(AppearanceSettings.video, videoSource as string);
}
source.src = `/packages/${name}/${videoSource ? videoSource : localStorage.getItem(AppearanceSettings.video)}`;
}
if (bgSource || localStorage.getItem(AppearanceSettings.image)) {
resetVideo();
resetBGImage();
const image = document.getElementById("nebulaImage")! as HTMLImageElement;
if (!localStorage.getItem(AppearanceSettings.image)) {
localStorage.setItem(AppearanceSettings.image, bgSource as string);
}
image.style.display = "block";
image.src = `/packages/${name}/${bgSource ? bgSource : localStorage.getItem(AppearanceSettings.image)}`;
}
if (payload) {
const stylesheet = document.getElementById("stylesheet")! as HTMLLinkElement;
if (localStorage.getItem(AppearanceSettings.stylePayload) !== payload) {
localStorage.setItem(AppearanceSettings.stylePayload, payload);
localStorage.setItem(AppearanceSettings.themeName, name as string);
}
stylesheet.href = `/packages/${name}/${localStorage.getItem(AppearanceSettings.stylePayload)}`;
} else {
if (localStorage.getItem(AppearanceSettings.stylePayload)) {
const stylesheet = document.getElementById("stylesheet")! as HTMLLinkElement;
stylesheet.href = `/packages/${localStorage.getItem(AppearanceSettings.themeName)}/${localStorage.getItem(AppearanceSettings.stylePayload)}`;
}
}
}
};
export { AppearanceSettings, PluginSettings, MarketPlaceExtras, marketPlaceSettings };
|