From 9302ed4bdfceebc911d0928d7220eb78af2314f4 Mon Sep 17 00:00:00 2001 From: Zim <23222943+ZimCodes@users.noreply.github.com> Date: Sun, 10 Oct 2021 15:33:39 +0000 Subject: [PATCH] Firefox: Mixed Mode Themed Favicon Issue (#72) * initial commit * This is the base functionality of Doki Theme. * Search functionality is now operable in new custom tab page; Removes settings and logo icons from new custom tab page; Decreases the size of doki logo in popup; Updates Dark Ishtar theme; Updates manifest; Live updating when selecting a new waifu; Adds documentation; Removes debugs; * New custom tab pages are now called 'Waifu Tab'; Waifus are now loaded to a Waifu Tab using JS instead of CSS. This is more convenient than making a CSS for each waifu; Adds a new background script, resources.js. This script points to theme resources & provides utility functions for the other background script. It also provides an easier way to activate a theme without using a switch statement; All of Firefox's New Tabs are replaced with a Waifu Tab. Refactors code for more readability; Adds more documentation comments for new classes & functions; * Completes Aqua & Ishtar themes * Adds more waifu themes to complete; 10 waifus can now be set as background * Adds 5 more Waifu themes * Adds 13 more waifu themes;Adds a permissions file, which explains the purpose of each permission needed to use the extension * Change location of files * Update Readme with Firefox section * relocate files * update readme * New permission needed: Storage; Storage is used to add all waifu choices in the popup menu dynamically with less boilerplate; Refactor code;Update permissions * Merges firefox branch; Updates CHANGELOG * Update manifest * Update manifest * Can now choose a theme at random; Increase tab text brightness for megumin theme; * Refactors code making great use of the local storage api. Instead of making a custom new tab HTML page for each theme, doki theme now reuses one HTML page to load all themes; It is also easier to add more doki themes to the current collection; Permissions file is updated with more clarification on the Storage section; Updates manifest; * Kanna's sidebar now has a theme applied to it * Update permission of firefox branch with master * Update manifest * Removes XML declaration; * Removes merge files outside of firefox folder * The popup menu now remembers the last waifu that was set. * Makes popup menu smaller; Doki Theme now takes advantage of chrome_url_overrides manifest key to land on the custom New Tab. Initially, Doki Theme had its own method for transitioning which involved forcing the browser to show the new custom tab page if any of these tabs are present: about:newtab, about:privatebrowsing, or about:home; Adds chrome_url_overrides & chrome_settings_overrides keys to manifest; Removes unnecessary background script; Adds two more themes for Monika: light/dark Monika joy variant; Update names & file paths for Natsuki & Monika images; Enhance tab_text & tab_background_text colors for some themes; Remembers the last waifu theme chosen; Updates PERMISSIONS.md file; * Removes extra themes;Removes image key from all theme json * Makes branch on par with master * Adds other Monika & Natsuki images; New feature 'mixed' loads a new theme per new tab created; Adds some Documentation; Updates DokiThemeDefinitions for Firefox; * merge with doki-theme-chrome repo * Adds asynchronous programming to reduce load times; * Adds high res icons * Update manifest to proper version formatting * merge from master * Removes unneeded async keywords * update firefox build template by removing unnecessary theme_frame key * Removes theme_frame key from new themes; * When mix mode is activated all new tabs except the last are closed before initialization;Adds comments * Removes empty spaces from themes; In mix mode, tab theme info is now storage in local storage instead of temp variable; Fix Popup menu not changing colors with the theme; * Actually fix popup menu changing to the correct background color * Fixes the popup menu only changing colors on New Tab pages * Fixes popup menu color change for non-new tab pages * Update versioning; Update Changelog * Adds bounce animation to doki icon * update Changelogs;Adds back .editorconfig file * actually adds .editorconfig file * Adds hover animation to popup menu;Fixes popup menu color change when chosen a theme normally without any modes; * Cleans up mix mode * Styling popup header font color. * - Removes extra *Rias* folder. - Changes order of switches - Reduces size of switches - Adds border-radius to dropdown menu - Changes animation from bouncing to heartbeat * yarn.lock * Brighten switch titles * Update Changelog * Update manifest version to 9.0.1 * Themed switch labels * Update Changelog * Add option to activate/disable scrollbar & text selection themes; Update package.json version for firefox; Removes deprecated colors.toolbar_field_separator theme key; Fix 'Random' mode; Add shortcut to options page; Add filters: hue rotation & drop shadow to doki image in popup menu; Refactor some code; * Make doki theme's scrollbar & text selection styles have high priority; Simplify option labels; * Change drop shadow of doki heartbeat to light gray; Fix switches properly disabling for both secondary and dark (functionality); Blurs disabled switches (appearance); Improve performance when switching between theme variants; Adds glow effect to unchecked enabled switches; Code refactoring; * Clean up CONTRIBUTING file * Fixes URL match pattern * Corrects issue number in CHANGELOG; * Fixes Popup menu not changing to appropriate theme color when selected; Reset selection when choosing Random mode; * Update CHANGELOG * Fixes text selection and scrollbar not changing themes when selecting a new theme while on a webpage; * Fixes Random mode not enabling switches properly * Instead of dynamically adding style tag to the , this uses CSS variables to manipulate the styles * Removes toolbar_field_separator from firefox build template; Adds custom checkbox for options page; Tightens search for chosen theme by theme name && group when initializing random mode; Use accentColor instead of caretRow attribute for the caret color; * Update CHANGELOG; Options page now matches the current doki theme; Change checkmark to a heart; Refresh pages based on current activity; * Possible solution to checkmark being out-of-bounds; Remove colons from label representing each checkbox * Appropriate theme(favicon,json,action button,options page & image) for each tab loads correctly when opening tabs really quickly in mixed mode; Fix Tabs not being randomized when opening tabs really quickly in mixed mode; Fix text selection & scrollbar not loading appropriately with their individual tab in mixed mode; Scripts loading the page theme is now more modular; Fix initialization browser if the last option chosen was mix mode; * Update Changlog and versionings * Update changelog * Adds missing `loadTheme` import; Update CHANGELOG.md; * Fix initialization, when user reopens browser; * Error handling for undefined list during mix mode; Removes tab creation listener for mix mode; clean up local mix list when disabled; rename `mixList to `mixedList`; Replace glitchy tab algorithm to utilize the message system approach; `getCurrentTheme` method is now synchronous; * Rename `mixList` to `mixedList`; clean up local mix list; * Fixes search bar widget toggling in *Mixed Mode* reverting all tabs back to using a single theme. Co-authored-by: Alex Simons --- CHANGELOG.md | 8 ++ firefoxThemes/manifest.json | 2 +- firefoxThemes/modules/modes/mix.js | 97 +++++++++++-------- firefoxThemes/modules/utils/themes/browser.js | 10 +- firefoxThemes/options/options.js | 20 ++-- firefoxThemes/package.json | 2 +- firefoxThemes/popup/popup.js | 9 +- firefoxThemes/resources.js | 57 ++++++----- firefoxThemes/waifus/faviconLoader.js | 25 ----- firefoxThemes/waifus/index.html | 6 +- firefoxThemes/waifus/tab.js | 41 ++++---- firefoxThemes/waifus/themeLoader.js | 36 +++++++ firefoxThemes/waifus/waifu.js | 17 ++-- 13 files changed, 185 insertions(+), 145 deletions(-) delete mode 100644 firefoxThemes/waifus/faviconLoader.js create mode 100644 firefoxThemes/waifus/themeLoader.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 8df2f267..663dbd7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ Changelog --- # Unreleased +# 15.0.1 [Firefox: Theming Bug Fixes] +- Fix tabs not being randomized when creating tabs rapidly in mixed mode. +- Fixes text selection & scrollbar not loading appropriately with their individual tab in mixed mode. +- Fixes text selection & scrollbar not loading appropriately with their individual tab in mixed mode. +- Appropriate theme for *options page* for each tab loads correctly in mixed mode. +- Fixes initialization of a theme, when closing and reopening a browser. +- Mitigates favicon & other theming components to stay themed in mixed mode. +- Fixes search bar widget toggling in *Mixed Mode* reverting all tabs back to using a single theme. # 15.0.0 [Only for Onii-chan] diff --git a/firefoxThemes/manifest.json b/firefoxThemes/manifest.json index e58f3304..30fc2ad0 100644 --- a/firefoxThemes/manifest.json +++ b/firefoxThemes/manifest.json @@ -1,7 +1,7 @@ { "name": "Doki Theme for Firefox", "short_name": "Doki Theme", - "version": "15.0.0", + "version": "15.0.1", "description": "A theme collection of girls from various anime, manga, and visual novels series.", "manifest_version": 2, "icons": { diff --git a/firefoxThemes/modules/modes/mix.js b/firefoxThemes/modules/modes/mix.js index 042feaac..273bbb4d 100644 --- a/firefoxThemes/modules/modes/mix.js +++ b/firefoxThemes/modules/modes/mix.js @@ -1,6 +1,7 @@ import {getRandomThemeId} from "../utils/random.js"; import {loadTheme} from "../utils/themes/browser.js"; - +/*Global Variables*/ +let mixedList = undefined;//Maintain a local list of all mixed tabs /*Separate the New Tab pages from the other types of pages*/ function separateTabs(tabs) { let newTabs = []; @@ -15,7 +16,7 @@ function separateTabs(tabs) { return [newTabs, otherTabs]; } -/*Add all tabs that are not a new tab into the mix collection*/ +/*Add all tabs that are not a new tab into the mix mixedQueue*/ function addOtherTabsToMix(tabs, mixList, themes) { if (tabs.length > 0) { for (const tab of tabs) { @@ -37,51 +38,34 @@ function keepLastTab(tabs) { return lastTabs; } -/*EVENT: When a new tab is created add a random theme to it*/ -function MixTabCreated(tab) { - if (tab.title === "New Tab") { - browser.storage.local.get("waifuThemes") - .then((storage) => { - const chosenThemeId = getRandomThemeId(storage.waifuThemes.themes); - MixedUpdate(tab, chosenThemeId, storage.waifuThemes.themes); - }); - } -} - /*EVENT: Set the theme for the current active tab*/ function MixTabActivated(activeInfo) { - browser.storage.local.get(["waifuThemes", "mixedTabs"]) + browser.storage.local.get(["waifuThemes"]) .then((storage) => { - const currentThemeId = storage.mixedTabs.get(activeInfo.tabId); + //Check if mixed mode is still active. + //This check is when searchbar is toggled during mix mode which disables it + if (!mixedList) { + mixTabCleanup(); + return; + } + const currentThemeId = mixedList.get(activeInfo.tabId); if (currentThemeId) { loadTheme(storage.waifuThemes.themes, currentThemeId); + browser.storage.local.set({currentThemeId}); } }); } /*EVENT: When a tab is closed delete the saved data for it*/ function MixTabClosed(tabId) { - browser.storage.local.get() - .then((storage) => { - storage.mixedTabs.delete(tabId); - browser.storage.local.set({mixedTabs: storage.mixedTabs}); - }); -} - -/*Updates the new tab with a new waifu theme*/ -function MixedUpdate(tab, themeId, themes) { - browser.storage.local.get(["mixedTabs"]) - .then((storage) => { - storage.mixedTabs.set(tab.id, themeId);//Adds the created tab to the list of mixed tabs - browser.storage.local.set({currentThemeId: themeId, mixedTabs: storage.mixedTabs}); - //Update the tab with theme - browser.tabs.update(tab.id, { - loadReplace: true, - url: themes[themeId].page - }); - //Load browser theme - loadTheme(themes, themeId); - }); + //Check if mixed mode is still active. + //This check is when searchbar is toggled during mix mode which disables it + if (!mixedList) { + mixTabCleanup(); + return; + } + mixedList.delete(tabId); + browser.storage.local.set({mixedTabs: mixedList}); } /*Cleans up all things relating to the Mixed tab option*/ @@ -89,13 +73,13 @@ function mixTabCleanup() { browser.storage.local.get("mixedTabs") .then((storage) => { //Removes all listeners - if (browser.tabs.onCreated.hasListener(MixTabCreated)) { - browser.tabs.onCreated.removeListener(MixTabCreated); + if (browser.tabs.onCreated.hasListener(MixTabActivated)) { browser.tabs.onActivated.removeListener(MixTabActivated); browser.tabs.onRemoved.removeListener(MixTabClosed); } if (storage.mixedTabs) { browser.storage.local.set({mixedTabs: undefined}); + mixedList = undefined; } }); } @@ -108,9 +92,9 @@ function setupMixedUpdate(msg) { .then((storage) => { if (!storage.mixedTabs) { storage.mixedTabs = new Map();//Create a new mixed tab list + mixedList = new Map(); } //Activate event listeners - browser.tabs.onCreated.addListener(MixTabCreated);//When a tab is created browser.tabs.onActivated.addListener(MixTabActivated);//When the active tab has been switched browser.tabs.onRemoved.addListener(MixTabClosed);//When a tab has been closed if (tabs.length > 0) { @@ -130,6 +114,7 @@ function setupMixedUpdate(msg) { } else { browser.storage.local.set({currentThemeId, mixedTabs: storage.mixedTabs}); } + mixedList = storage.mixedTabs; //Initialize first (and only) new tab with the default theme loadTheme(themes, currentThemeId); } @@ -137,4 +122,36 @@ function setupMixedUpdate(msg) { }); } -export {setupMixedUpdate, mixTabCleanup}; \ No newline at end of file +/*MESSAGE: Send a message to the page to apply theme*/ +function pageResponse(msg) { + if (!msg.mixMSG) return; + browser.storage.local.get(["currentThemeId", "waifuThemes", "backgroundType", "showWidget"]).then(storage => { + if (!mixedList) { + browser.tabs.sendMessage(msg.pageTab.id, { + pageMSG: true, + waifuThemes: storage.waifuThemes, + currentThemeId: storage.currentThemeId, + backgroundType: storage.backgroundType, + showWidget: storage.showWidget, + }); + } else { + if (!mixedList.has(msg.pageTab.id)) { + mixedList.set(msg.pageTab.id, getRandomThemeId(storage.waifuThemes.themes)); + browser.storage.local.set({mixedTabs: mixedList}); + } + browser.tabs.sendMessage(msg.pageTab.id, { + pageMSG: true, + waifuThemes: storage.waifuThemes, + currentThemeId: storage.currentThemeId, + backgroundType: storage.backgroundType, + showWidget: storage.showWidget, + mixedTabs: mixedList, + pageTab: msg.pageTab + }); + loadTheme(storage.waifuThemes.themes, mixedList.get(msg.pageTab.id)); + } + }); +} + +export {setupMixedUpdate, mixTabCleanup}; +browser.runtime.onMessage.addListener(pageResponse); \ No newline at end of file diff --git a/firefoxThemes/modules/utils/themes/browser.js b/firefoxThemes/modules/utils/themes/browser.js index e60549fa..deef4934 100644 --- a/firefoxThemes/modules/utils/themes/browser.js +++ b/firefoxThemes/modules/utils/themes/browser.js @@ -10,5 +10,13 @@ async function loadTheme(themes, themeId) { const themeJSON = await res.json(); browser.theme.update(themeJSON); } +/*Retrieve the appropriate theme*/ +function getCurrentTheme(themes,themeId,mixedTabs,tab){ + if(mixedTabs){ + themeId = mixedTabs.get(tab.id); + browser.storage.local.set({currentThemeId:themeId}); + } + return themes[themeId]; +} -export {loadTheme}; \ No newline at end of file +export {loadTheme,getCurrentTheme}; \ No newline at end of file diff --git a/firefoxThemes/options/options.js b/firefoxThemes/options/options.js index 42df2fb9..a6916162 100644 --- a/firefoxThemes/options/options.js +++ b/firefoxThemes/options/options.js @@ -14,19 +14,23 @@ function setCss(chosenTheme) { root.style.setProperty('--checkmark-color',colors.classNameColor); } -function initContent() { - browser.storage.local.get(['loadOnStart', 'textSelection', 'scrollbar', 'waifuThemes', 'currentThemeId']) - .then((storage) => { - initCheckbox(loadOnStartCheckbox,!!storage.loadOnStart); - initCheckbox(textSelectionCheckbox,!!storage.textSelection); - initCheckbox(scrollbarCheckbox,!!storage.scrollbar); - setCss(storage.waifuThemes.themes[storage.currentThemeId]) - }); +async function initContent() { + const storage = await browser.storage.local.get(['loadOnStart', 'textSelection', 'scrollbar', 'waifuThemes', 'currentThemeId','mixedTabs']) + initCheckbox(loadOnStartCheckbox,!!storage.loadOnStart); + initCheckbox(textSelectionCheckbox,!!storage.textSelection); + initCheckbox(scrollbarCheckbox,!!storage.scrollbar); + let themeId = storage.currentThemeId; + if(storage.mixedTabs){ + const tab = await browser.tabs.getCurrent(); + themeId = storage.mixedTabs.get(tab.id); + } + setCss(storage.waifuThemes.themes[themeId]); } const onChangeCheckEvents = async (e) => { changeCheckboxState(e); await browser.runtime.sendMessage({ + resourceMSG:true, optionName: e.target.id, optionValue: e.target.className }); diff --git a/firefoxThemes/package.json b/firefoxThemes/package.json index 4351c9a8..a16d18d8 100644 --- a/firefoxThemes/package.json +++ b/firefoxThemes/package.json @@ -1,6 +1,6 @@ { "name": "doki-theme-firefox", - "version": "15.0.0", + "version": "15.0.1", "description": "A theme collection of girls from various anime, manga, and visual novels series.", "main": "index.js", "repository": "https://github.com/doki-theme/doki-theme-web", diff --git a/firefoxThemes/popup/popup.js b/firefoxThemes/popup/popup.js index 03cead25..a021feb5 100644 --- a/firefoxThemes/popup/popup.js +++ b/firefoxThemes/popup/popup.js @@ -36,7 +36,7 @@ const setBackground = async () => { }); const {currentThemeId} = await browser.storage.local.get(["currentThemeId"]) - browser.runtime.sendMessage({currentThemeId}); + browser.runtime.sendMessage({resourceMSG:true,currentThemeId}); } const setHideWidget = async () => { @@ -44,8 +44,7 @@ const setHideWidget = async () => { showWidget: showSearchSwitch.checked }); - const {currentThemeId} = await browser.storage.local.get(["currentThemeId"]) - browser.runtime.sendMessage({currentThemeId}); + browser.runtime.sendMessage({resourceMSG:true,applyWidget:true}); } /*Stores info to set dark mode switch accordingly*/ @@ -81,7 +80,7 @@ const setOpposingTheme = async () => { const newThemeId = newTheme.id; setCSS(newTheme); await browser.storage.local.set({currentThemeId: newThemeId}); - browser.runtime.sendMessage({currentThemeId: newThemeId}); + browser.runtime.sendMessage({resourceMSG:true,currentThemeId: newThemeId}); } } @@ -151,7 +150,7 @@ function setTheme(e) { setCSS(themes[chosenThemeId]); selectTag.value = 'none';// Reset option back to 'choose a waifu' } - browser.runtime.sendMessage({currentThemeId: chosenThemeId, mixState: currentMix}); + browser.runtime.sendMessage({resourceMSG:true,currentThemeId: chosenThemeId, mixState: currentMix}); }); } diff --git a/firefoxThemes/resources.js b/firefoxThemes/resources.js index 0cd998cb..899404e8 100644 --- a/firefoxThemes/resources.js +++ b/firefoxThemes/resources.js @@ -3,7 +3,8 @@ import {mixedStates} from "./modules/utils/states.js"; import {setupMixedUpdate, mixTabCleanup} from "./modules/modes/mix.js"; import {normalUpdate} from "./modules/modes/normal.js"; import {updateOptions} from "./modules/contentConfig.js"; - +import {getRandomThemeId} from "./modules/utils/random.js"; +import {reloadTabs} from "./modules/utils/browser.js"; /*---CLASSES---*/ /*Class Goal: Holds theme data about all waifus*/ @@ -40,26 +41,28 @@ class Theme { } /*Initialize Local Storage & custom new tab page*/ -function startStorage() { - browser.storage.local.get(["currentThemeId", "loadOnStart", "textSelection", "scrollbar"]) - .then((storage) => { - const initStorage = { - waifuThemes: new WaifuThemes(), - }; - //Retrieve all themes if none exists in local storage - browser.storage.local.set(initStorage); - //Load browser theme - if (storage.currentThemeId) { - loadTheme(initStorage.waifuThemes.themes, storage.currentThemeId); - } - if (storage.loadOnStart) { - //When the browser first opens, redirect to custom new tab page - browser.tabs.update({loadReplace: true, url: "waifus/index.html"}); - } - // Register all styles from option page - updateOptions({optionName: "textSelection", optionValue: !!!storage.textSelection}); - updateOptions({optionName: "scrollbar", optionValue: !!!storage.scrollbar}); - }); +async function startStorage() { + const storage = await browser.storage.local.get(["currentThemeId", "loadOnStart", "textSelection", "scrollbar", "mixedTabs"]); + const initStorage = { + waifuThemes: new WaifuThemes(), + }; + //Retrieve all themes if none exists in local storage + browser.storage.local.set(initStorage); + //Load browser theme + if (storage.mixedTabs) { + let themeId = storage.currentThemeId || getRandomThemeId(initStorage.waifuThemes.themes); + updateTabs({mixState: mixedStates.RESET, currentThemeId: themeId}); + } else if (storage.currentThemeId) { + let themeId = storage.currentThemeId || getRandomThemeId(initStorage.waifuThemes.themes); + updateTabs({currentThemeId: themeId}); + } + if (storage.loadOnStart) { + //When the browser first opens, redirect to custom new tab page + browser.tabs.update({loadReplace: true, url: "waifus/index.html"}); + } + // Register all styles from option page + updateOptions({optionName: "textSelection", optionValue: !!!storage.textSelection}); + updateOptions({optionName: "scrollbar", optionValue: !!!storage.scrollbar}); } /*Update all new tabs with new waifu theme*/ @@ -79,15 +82,19 @@ function updateTabs(msg) { } } -/*Update all theme components*/ +/*MESSAGE: Update all theme components*/ function updateTheme(msg) { - if (!msg.optionName) { + if (!msg.resourceMSG) return; + + if (msg.applyWidget) { + reloadTabs({title: 'New Tab'}); + } else if (msg.optionName && msg.optionValue) { + updateOptions(msg); + } else { updateTabs(msg); for (const optionName of ['textSelection', 'scrollbar']) { updateOptions({optionName}); } - } else { - updateOptions(msg); } } diff --git a/firefoxThemes/waifus/faviconLoader.js b/firefoxThemes/waifus/faviconLoader.js deleted file mode 100644 index c32b9178..00000000 --- a/firefoxThemes/waifus/faviconLoader.js +++ /dev/null @@ -1,25 +0,0 @@ -import {buildSVG, svgToPng} from "../modules/utils/themes/logo.js"; - -function setThemedFavicon(currentTheme) { - const faviconOptions = {width: 32, height: 32}; - const faviconSVG = buildSVG(currentTheme, faviconOptions); - svgToPng(faviconSVG, faviconOptions, (imgData) => { - let link = document.querySelector("link[rel~='icon']"); - if (!link) { - link = document.createElement('link'); - link.rel = 'icon'; - document.head.appendChild(link); - } - link.href = imgData; - }); -} - -function applyFaviconTheme() { - browser.storage.local.get(["currentThemeId", "waifuThemes"]).then((storage) => { - const currentTheme = storage.waifuThemes.themes[storage.currentThemeId] || - storage.waifuThemes.themes["19b65ec8-133c-4655-a77b-13623d8e97d3"]; - setThemedFavicon(currentTheme); - }); -} - -applyFaviconTheme(); \ No newline at end of file diff --git a/firefoxThemes/waifus/index.html b/firefoxThemes/waifus/index.html index 1138d9c3..5bb86613 100644 --- a/firefoxThemes/waifus/index.html +++ b/firefoxThemes/waifus/index.html @@ -7,12 +7,8 @@ - - - - - + diff --git a/firefoxThemes/waifus/tab.js b/firefoxThemes/waifus/tab.js index c25b6e14..b8e8a98d 100644 --- a/firefoxThemes/waifus/tab.js +++ b/firefoxThemes/waifus/tab.js @@ -69,30 +69,25 @@ function displayWidget() { `; } -function applyTabListeners() { - browser.storage.local.get(["showWidget", "waifuThemes", "currentThemeId"]) - .then((storage) => { - const currentTheme = storage.waifuThemes.themes[storage.currentThemeId] || - storage.waifuThemes.themes["19b65ec8-133c-4655-a77b-13623d8e97d3"]; // Ryuko Dark - const root = document.querySelector(':root'); - root.style.setProperty('--accent-color', currentTheme.definition.colors.accentColor); - root.style.setProperty('--base-background-color', currentTheme.definition.colors.baseBackground); +function applyTabListeners(storage) { + const currentTheme = storage.currentTheme; + const root = document.querySelector(':root'); + root.style.setProperty('--accent-color', currentTheme.definition.colors.accentColor); + root.style.setProperty('--base-background-color', currentTheme.definition.colors.baseBackground); + if (storage.showWidget === undefined || storage.showWidget) { + displayWidget(); + // set themed icons + setThemedSearchInputIcon(currentTheme); + setThemedAboutIcon(currentTheme); - if (storage.showWidget === undefined || storage.showWidget) { - displayWidget(); - // set themed icons - setThemedSearchInputIcon(currentTheme); - setThemedAboutIcon(currentTheme); + /*---Event Listeners---*/ + const input = document.querySelector("input[type='search']"); + input.addEventListener("input", searchQuery, true); + input.addEventListener("keydown", keyConfirm, true); - /*---Event Listeners---*/ - const input = document.querySelector("input[type='search']"); - input.addEventListener("input", searchQuery, true); - input.addEventListener("keydown", keyConfirm, true); - - const searchButton = document.querySelector(".search-button"); - searchButton.addEventListener("click", confirmSearch, false); - } - }); + const searchButton = document.querySelector(".search-button"); + searchButton.addEventListener("click", confirmSearch, false); + } } -applyTabListeners(); +export {applyTabListeners}; diff --git a/firefoxThemes/waifus/themeLoader.js b/firefoxThemes/waifus/themeLoader.js new file mode 100644 index 00000000..09e6484c --- /dev/null +++ b/firefoxThemes/waifus/themeLoader.js @@ -0,0 +1,36 @@ +import {buildSVG, svgToPng} from "../modules/utils/themes/logo.js"; +import {getCurrentTheme} from "../modules/utils/themes/browser.js"; +import {applyTheme} from "./waifu.js"; +import {applyTabListeners} from "./tab.js"; + +function setThemedFavicon(currentTheme) { + const faviconOptions = {width: 32, height: 32}; + const faviconSVG = buildSVG(currentTheme, faviconOptions); + svgToPng(faviconSVG, faviconOptions, (imgData) => { + let link = document.querySelector("link[rel~='icon']"); + if (!link) { + link = document.createElement('link'); + link.rel = 'icon'; + document.head.appendChild(link); + } + link.href = imgData; + }); +} + +/*Apply All Themes except toolbar themes*/ +function startTheming(msg) { + if (!msg.pageMSG) return; + msg.currentTheme = getCurrentTheme(msg.waifuThemes.themes, msg.currentThemeId, msg.mixedTabs, msg.pageTab); + applyTheme(msg); + applyTabListeners(msg); + setThemedFavicon(msg.currentTheme); +} + +/*MESSAGE: Alerts background script that page is fully loaded and ready to apply theme*/ +async function sendMessage() { + const tab = await browser.tabs.getCurrent(); + browser.runtime.sendMessage({mixMSG: true, pageTab: tab}); +} + +browser.runtime.onMessage.addListener(startTheming); +sendMessage(); \ No newline at end of file diff --git a/firefoxThemes/waifus/waifu.js b/firefoxThemes/waifus/waifu.js index ba56facb..7e8f9e5a 100644 --- a/firefoxThemes/waifus/waifu.js +++ b/firefoxThemes/waifus/waifu.js @@ -21,9 +21,8 @@ function getSecondaryAnchoring(theme) { /*Prepares the Waifu to be displayed on webpage*/ function getWaifu(storage) { - const themes = storage.waifuThemes.themes; //Retrieve path to the image file - const currentTheme = themes[storage.currentThemeId]; + const currentTheme = storage.currentTheme; const primaryBackgroundRelativeUrl = currentTheme.backgrounds.primary; const isPrimaryBackground = !storage.backgroundType; @@ -54,13 +53,9 @@ function loadDokiTheme(storage) { } /*Apply Theme */ -function applyTheme() { - browser.storage.local.get(["waifuThemes", "currentThemeId", "backgroundType"]) - .then((storage) => { - if (Object.keys(storage.waifuThemes.themes).includes(storage.currentThemeId)) { - loadDokiTheme(storage); - } - }); +function applyTheme(storage) { + if (Object.keys(storage.waifuThemes.themes).includes(storage.currentThemeId)) { + loadDokiTheme(storage); + } } - -applyTheme(); +export {applyTheme}; \ No newline at end of file