From 5646cef0b91d9e6f26aeba940d7d9a09aa0b78a7 Mon Sep 17 00:00:00 2001 From: PascalPixel Date: Thu, 3 Aug 2023 16:32:48 +0100 Subject: [PATCH 1/4] update api createOrUpdateDynamicTheme function --- src/api/index.ts | 6 ++---- src/inject/dynamic-theme/index.ts | 10 +--------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index f388172e7d84..c4c4c0554f63 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -3,7 +3,7 @@ import {setFetchMethod as setFetch} from './fetch'; import {DEFAULT_THEME} from '../defaults'; import type {Theme, DynamicThemeFix} from '../definitions'; import {ThemeEngine} from '../generators/theme-engines'; -import {createOrUpdateDynamicThemeInternal, removeDynamicTheme} from '../inject/dynamic-theme'; +import {createOrUpdateDynamicTheme, removeDynamicTheme} from '../inject/dynamic-theme'; import {collectCSS} from '../inject/dynamic-theme/css-collection'; import {isMatchMediaChangeEventListenerSupported} from '../utils/platform'; @@ -23,9 +23,7 @@ export function enable(themeOptions: Partial | null = {}, fixes: DynamicT if (theme.engine !== ThemeEngine.dynamicTheme) { throw new Error('Theme engine is not supported.'); } - // TODO: repalce with createOrUpdateDynamicTheme() and make fixes signature - // DynamicThemeFix | DynamicThemeFix[] - createOrUpdateDynamicThemeInternal(theme, fixes, isIFrame); + createOrUpdateDynamicTheme(theme, fixes ? [fixes] : [], isIFrame); isDarkReaderEnabled = true; } diff --git a/src/inject/dynamic-theme/index.ts b/src/inject/dynamic-theme/index.ts index 2a5d38d1d014..b5c290e71231 100644 --- a/src/inject/dynamic-theme/index.ts +++ b/src/inject/dynamic-theme/index.ts @@ -496,9 +496,6 @@ function selectRelevantFix(documentURL: string, fixes: DynamicThemeFix[]): Dynam return relevantFixIndex ? combineFixes([fixes[0], fixes[relevantFixIndex]]) : fixes[0]; } -/** - * TODO: expose this function to API builds via src/api function enable() - */ export function createOrUpdateDynamicTheme(filterConfig: FilterConfig, dynamicThemeFixes: DynamicThemeFix[], iframe: boolean): void { const dynamicThemeFix = selectRelevantFix(document.location.href, dynamicThemeFixes); @@ -509,12 +506,7 @@ export function createOrUpdateDynamicTheme(filterConfig: FilterConfig, dynamicTh createOrUpdateDynamicThemeInternal(filterConfig, dynamicThemeFix, iframe); } -/** - * Note: This function should be directly used only in API builds, it is exported by this fle - * only for use in src/api/enable() for backwards compatibility, - * extension should use only createOrUpdateDynamicTheme() - */ -export function createOrUpdateDynamicThemeInternal(filterConfig: FilterConfig, dynamicThemeFixes: DynamicThemeFix | null, iframe: boolean): void { +function createOrUpdateDynamicThemeInternal(filterConfig: FilterConfig, dynamicThemeFixes: DynamicThemeFix | null, iframe: boolean): void { filter = filterConfig; fixes = dynamicThemeFixes; if (fixes) { From 00149b07a12dbb9ae8d96ce2a90695e773c19b80 Mon Sep 17 00:00:00 2001 From: PascalPixel Date: Fri, 4 Aug 2023 11:58:31 +0100 Subject: [PATCH 2/4] api: accept fixes array --- src/api/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index c4c4c0554f63..435b9076fa26 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -17,13 +17,13 @@ const isIFrame = (() => { } })(); -export function enable(themeOptions: Partial | null = {}, fixes: DynamicThemeFix | null = null): void { +export function enable(themeOptions: Partial | null = {}, fixes: DynamicThemeFix[] | null = null): void { const theme = {...DEFAULT_THEME, ...themeOptions}; if (theme.engine !== ThemeEngine.dynamicTheme) { throw new Error('Theme engine is not supported.'); } - createOrUpdateDynamicTheme(theme, fixes ? [fixes] : [], isIFrame); + createOrUpdateDynamicTheme(theme, fixes || [], isIFrame); isDarkReaderEnabled = true; } @@ -39,7 +39,7 @@ export function disable(): void { const darkScheme = matchMedia('(prefers-color-scheme: dark)'); let store = { themeOptions: null as Partial | null, - fixes: null as DynamicThemeFix | null, + fixes: null as DynamicThemeFix[] | null, }; function handleColorScheme(): void { @@ -50,7 +50,7 @@ function handleColorScheme(): void { } } -export function auto(themeOptions: Partial | false = {}, fixes: DynamicThemeFix | null = null): void { +export function auto(themeOptions: Partial | false = {}, fixes: DynamicThemeFix[] | null = null): void { if (themeOptions) { store = {themeOptions, fixes}; handleColorScheme(); From 66f7704f08dfe8052e17e1c5ca0671dbc5fbfdde Mon Sep 17 00:00:00 2001 From: PascalPixel Date: Fri, 4 Aug 2023 23:15:26 +0100 Subject: [PATCH 3/4] fix typo --- src/background/extension.ts | 14 +++++++------- src/background/tab-manager.ts | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/background/extension.ts b/src/background/extension.ts index 5d53786e66c5..8090e1e37466 100644 --- a/src/background/extension.ts +++ b/src/background/extension.ts @@ -627,18 +627,18 @@ export class Extension { }; } - private static getTabMessage = (tabURl: string, url: string, isTopFrame: boolean): TabData => { + private static getTabMessage = (tabURL: string, url: string, isTopFrame: boolean): TabData => { const settings = UserStorage.settings; - const tabInfo = Extension.getTabInfo(tabURl); - if (Extension.isExtensionSwitchedOn() && isURLEnabled(tabURl, settings, tabInfo)) { - const custom = settings.customThemes.find(({url: urlList}) => isURLInList(tabURl, urlList)); - const preset = custom ? null : settings.presets.find(({urls}) => isURLInList(tabURl, urls)); + const tabInfo = Extension.getTabInfo(tabURL); + if (Extension.isExtensionSwitchedOn() && isURLEnabled(tabURL, settings, tabInfo)) { + const custom = settings.customThemes.find(({url: urlList}) => isURLInList(tabURL, urlList)); + const preset = custom ? null : settings.presets.find(({urls}) => isURLInList(tabURL, urls)); let theme = custom ? custom.theme : preset ? preset.theme : settings.theme; if (Extension.autoState === 'scheme-dark' || Extension.autoState === 'scheme-light') { const mode = Extension.autoState === 'scheme-dark' ? 1 : 0; theme = {...theme, mode}; } - const detectDarkTheme = isTopFrame && settings.detectDarkTheme && !isURLInList(tabURl, settings.siteListEnabled) && !isPDF(tabURl); + const detectDarkTheme = isTopFrame && settings.detectDarkTheme && !isURLInList(tabURL, settings.siteListEnabled) && !isPDF(tabURL); logInfo(`Creating CSS for url: ${url}`); logInfo(`Custom theme ${custom ? 'was found' : 'was not found'}, Preset theme ${preset ? 'was found' : 'was not found'} @@ -701,7 +701,7 @@ export class Extension { } } - logInfo(`Site is not inverted: ${tabURl}`); + logInfo(`Site is not inverted: ${tabURL}`); return { type: MessageTypeBGtoCS.CLEAN_UP, }; diff --git a/src/background/tab-manager.ts b/src/background/tab-manager.ts index 0ba2dcaeaabc..ec044472081d 100644 --- a/src/background/tab-manager.ts +++ b/src/background/tab-manager.ts @@ -16,7 +16,7 @@ declare const __CHROMIUM_MV3__: boolean; declare const __THUNDERBIRD__: boolean; interface TabManagerOptions { - getConnectionMessage: (tabURl: string, url: string, isTopFrame: boolean) => Promise; + getConnectionMessage: (tabURL: string, url: string, isTopFrame: boolean) => Promise; getTabMessage: (tabURL: string, url: string, isTopFrame: boolean) => MessageBGtoCS; onColorSchemeChange: (isDark: boolean) => void; } From e65f200eaa118b12275e404600c458740a72dc6e Mon Sep 17 00:00:00 2001 From: PascalPixel Date: Wed, 9 Aug 2023 13:09:11 +0100 Subject: [PATCH 4/4] fix with upstream --- src/background/extension.ts | 14 +++++++------- src/background/tab-manager.ts | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/background/extension.ts b/src/background/extension.ts index 8090e1e37466..5d53786e66c5 100644 --- a/src/background/extension.ts +++ b/src/background/extension.ts @@ -627,18 +627,18 @@ export class Extension { }; } - private static getTabMessage = (tabURL: string, url: string, isTopFrame: boolean): TabData => { + private static getTabMessage = (tabURl: string, url: string, isTopFrame: boolean): TabData => { const settings = UserStorage.settings; - const tabInfo = Extension.getTabInfo(tabURL); - if (Extension.isExtensionSwitchedOn() && isURLEnabled(tabURL, settings, tabInfo)) { - const custom = settings.customThemes.find(({url: urlList}) => isURLInList(tabURL, urlList)); - const preset = custom ? null : settings.presets.find(({urls}) => isURLInList(tabURL, urls)); + const tabInfo = Extension.getTabInfo(tabURl); + if (Extension.isExtensionSwitchedOn() && isURLEnabled(tabURl, settings, tabInfo)) { + const custom = settings.customThemes.find(({url: urlList}) => isURLInList(tabURl, urlList)); + const preset = custom ? null : settings.presets.find(({urls}) => isURLInList(tabURl, urls)); let theme = custom ? custom.theme : preset ? preset.theme : settings.theme; if (Extension.autoState === 'scheme-dark' || Extension.autoState === 'scheme-light') { const mode = Extension.autoState === 'scheme-dark' ? 1 : 0; theme = {...theme, mode}; } - const detectDarkTheme = isTopFrame && settings.detectDarkTheme && !isURLInList(tabURL, settings.siteListEnabled) && !isPDF(tabURL); + const detectDarkTheme = isTopFrame && settings.detectDarkTheme && !isURLInList(tabURl, settings.siteListEnabled) && !isPDF(tabURl); logInfo(`Creating CSS for url: ${url}`); logInfo(`Custom theme ${custom ? 'was found' : 'was not found'}, Preset theme ${preset ? 'was found' : 'was not found'} @@ -701,7 +701,7 @@ export class Extension { } } - logInfo(`Site is not inverted: ${tabURL}`); + logInfo(`Site is not inverted: ${tabURl}`); return { type: MessageTypeBGtoCS.CLEAN_UP, }; diff --git a/src/background/tab-manager.ts b/src/background/tab-manager.ts index ec044472081d..0ba2dcaeaabc 100644 --- a/src/background/tab-manager.ts +++ b/src/background/tab-manager.ts @@ -16,7 +16,7 @@ declare const __CHROMIUM_MV3__: boolean; declare const __THUNDERBIRD__: boolean; interface TabManagerOptions { - getConnectionMessage: (tabURL: string, url: string, isTopFrame: boolean) => Promise; + getConnectionMessage: (tabURl: string, url: string, isTopFrame: boolean) => Promise; getTabMessage: (tabURL: string, url: string, isTopFrame: boolean) => MessageBGtoCS; onColorSchemeChange: (isDark: boolean) => void; }