more robust theme selector

This commit is contained in:
SethBurkart123
2023-11-02 17:11:02 +11:00
parent 46e86b9643
commit 8508169cd7
7 changed files with 246 additions and 109 deletions
+5 -2
View File
@@ -66,16 +66,19 @@ const applyTheme = async (themeName) => {
}
};
export const listThemes = async () => {
const themes = await localforage.keys();
return themes.filter((key) => key.startsWith("css_")).map((key) => key.replace("css_", ""));
};
export const downloadTheme = async (themeName, themeUrl) => {
console.log(`Fetching theme ${themeName} from ${themeUrl}...`);
const themeData = await fetchThemeJSON(themeUrl);
await saveToIndexedDB(themeData, themeName);
console.log(`Theme ${themeName} saved to IndexedDB`);
return;
};
export const setTheme = async (themeName, themeUrl) => {
await downloadTheme(themeName, themeUrl);
if (!(await themeExistsInDB(themeName))) {
await downloadTheme(themeName, themeUrl);
}
+15 -6
View File
@@ -1,14 +1,14 @@
/* global chrome */
import { MenuOptionsOpen, OpenMenuOptions, closeSettings } from "../../SEQTA.js";
import { downloadTheme, setTheme } from "../ui/Themes.js";
import { downloadTheme, listThemes, setTheme } from "../ui/Themes.js";
export class MessageHandler {
constructor() {
chrome.runtime.onMessage.addListener(this.routeMessage.bind(this));
}
routeMessage(request) {
routeMessage(request, sender, sendResponse) {
switch (request.info) {
case "EditSidebar":
@@ -16,11 +16,20 @@ export class MessageHandler {
break;
case "SetTheme":
console.log(request);
setTheme(request.body.themeName, request.body.themeURL);
break;
setTheme(request.body.themeName, request.body.themeURL).then(() => {
sendResponse({ status: "success" });
});
return true;
case "DownloadTheme":
downloadTheme(request.body.themeURL, request.body.themeName);
break;
downloadTheme(request.body.themeName, request.body.themeURL).then(() => {
sendResponse({ status: "success" });
});
return true;
case "ListThemes":
listThemes().then((themes) => {
sendResponse({ themes });
});
return true;
default:
console.log("Unknown request info:", request.info);