mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-06-06 03:34:40 +00:00
refactor: reduce browser storage dependence, remove unused code
This commit is contained in:
+1
-2
@@ -33,7 +33,6 @@ declare global {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export let isChrome = window.chrome
|
|
||||||
let SettingsClicked = false
|
let SettingsClicked = false
|
||||||
export let MenuOptionsOpen = false
|
export let MenuOptionsOpen = false
|
||||||
let currentSelectedDate = new Date()
|
let currentSelectedDate = new Date()
|
||||||
@@ -283,7 +282,7 @@ export function OpenWhatsNewPopup() {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
browser.storage.local.remove(['justupdated'])
|
delete settingsState.justupdated
|
||||||
|
|
||||||
bkelement!.addEventListener('click', function (event) {
|
bkelement!.addEventListener('click', function (event) {
|
||||||
// Check if the click event originated from the element itself and not any of its children
|
// Check if the click event originated from the element itself and not any of its children
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import browser from 'webextension-polyfill';
|
|
||||||
import { CustomThemeBase64 } from '../../../interface/types/CustomThemes';
|
import { CustomThemeBase64 } from '../../../interface/types/CustomThemes';
|
||||||
import { imageData, removeImageFromDocument, UpdateImageData, applyCustomCSS } from './Themes';
|
import { imageData, removeImageFromDocument, UpdateImageData, applyCustomCSS } from './Themes';
|
||||||
|
import { settingsState } from '../../utils/listeners/SettingsState';
|
||||||
|
|
||||||
|
|
||||||
export const UpdateThemePreview = async (updatedTheme: CustomThemeBase64 /* Omit<CustomTheme, 'CustomImages'> & { CustomImages: Omit<CustomImage, 'blob'>[] } */) => {
|
export const UpdateThemePreview = async (updatedTheme: CustomThemeBase64 /* Omit<CustomTheme, 'CustomImages'> & { CustomImages: Omit<CustomImage, 'blob'>[] } */) => {
|
||||||
@@ -52,6 +52,6 @@ export const UpdateThemePreview = async (updatedTheme: CustomThemeBase64 /* Omit
|
|||||||
|
|
||||||
// Apply default color
|
// Apply default color
|
||||||
if (defaultColour !== '') {
|
if (defaultColour !== '') {
|
||||||
browser.storage.local.set({ selectedColor: defaultColour });
|
settingsState.selectedColor = defaultColour
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import browser from 'webextension-polyfill';
|
|
||||||
import localforage from 'localforage';
|
import localforage from 'localforage';
|
||||||
import { CustomTheme } from '../../../interface/types/CustomThemes';
|
import { CustomTheme } from '../../../interface/types/CustomThemes';
|
||||||
import { removeTheme } from './removeTheme';
|
import { removeTheme } from './removeTheme';
|
||||||
|
import { settingsState } from '../../utils/listeners/SettingsState';
|
||||||
|
|
||||||
|
|
||||||
export const deleteTheme = async (themeId: string) => {
|
export const deleteTheme = async (themeId: string) => {
|
||||||
@@ -16,7 +16,7 @@ export const deleteTheme = async (themeId: string) => {
|
|||||||
await localforage.setItem('customThemes', updatedThemeIds);
|
await localforage.setItem('customThemes', updatedThemeIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
await browser.storage.local.set({ selectedTheme: '' });
|
settingsState.selectedTheme = ''
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error deleting theme:', error);
|
console.error('Error deleting theme:', error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +1,33 @@
|
|||||||
import browser from 'webextension-polyfill';
|
|
||||||
import localforage from 'localforage';
|
import localforage from 'localforage';
|
||||||
import { CustomTheme } from '../../../interface/types/CustomThemes';
|
import { CustomTheme } from '../../../interface/types/CustomThemes';
|
||||||
import { removeTheme } from './removeTheme';
|
import { removeTheme } from './removeTheme';
|
||||||
import { Mutex } from '../../utils/mutex';
|
import { Mutex } from '../../utils/mutex';
|
||||||
|
import { settingsState } from '../../utils/listeners/SettingsState';
|
||||||
|
|
||||||
const mutex = new Mutex();
|
const mutex = new Mutex();
|
||||||
let isDisabling = false;
|
let isDisabling = false;
|
||||||
|
|
||||||
export const disableTheme = async () => {
|
export const disableTheme = async () => {
|
||||||
|
console.log('Disabling theme', isDisabling)
|
||||||
if (isDisabling) return;
|
if (isDisabling) return;
|
||||||
|
|
||||||
const { selectedTheme } = await browser.storage.local.get('selectedTheme') as { selectedTheme: string; };
|
if (!settingsState.selectedTheme || settingsState.selectedTheme === '') {
|
||||||
if (!selectedTheme || selectedTheme === '') {
|
console.log('Theme is already disabled, exit early')
|
||||||
// Theme is already disabled, exit early
|
// Theme is already disabled, exit early
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
isDisabling = true;
|
isDisabling = true;
|
||||||
const unlock = await mutex.lock();
|
const unlock = await mutex.lock();
|
||||||
try {
|
try {
|
||||||
await browser.storage.local.set({ selectedTheme: '' });
|
if (settingsState.selectedTheme) {
|
||||||
|
console.log('Disabling theme:', settingsState.selectedTheme);
|
||||||
if (selectedTheme) {
|
const theme = await localforage.getItem(settingsState.selectedTheme) as CustomTheme;
|
||||||
const theme = await localforage.getItem(selectedTheme) as CustomTheme;
|
|
||||||
if (theme) {
|
if (theme) {
|
||||||
await removeTheme(theme);
|
await removeTheme(theme);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await browser.storage.local.set({ selectedTheme: '' });
|
|
||||||
|
settingsState.selectedTheme = ''
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error disabling theme:', error);
|
console.error('Error disabling theme:', error);
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
import browser from 'webextension-polyfill';
|
|
||||||
import localforage from 'localforage';
|
import localforage from 'localforage';
|
||||||
import { CustomTheme } from '../../../interface/types/CustomThemes';
|
import { CustomTheme } from '../../../interface/types/CustomThemes';
|
||||||
import { applyTheme } from './applyTheme';
|
import { applyTheme } from './applyTheme';
|
||||||
|
import { settingsState } from '../../utils/listeners/SettingsState';
|
||||||
|
|
||||||
|
|
||||||
export const enableCurrentTheme = async () => {
|
export const enableCurrentTheme = async () => {
|
||||||
const { selectedTheme } = await browser.storage.local.get('selectedTheme') as { selectedTheme: string; };
|
if (settingsState.selectedTheme) {
|
||||||
if (selectedTheme) {
|
const theme = await localforage.getItem(settingsState.selectedTheme) as CustomTheme;
|
||||||
const theme = await localforage.getItem(selectedTheme) as CustomTheme;
|
|
||||||
if (theme) {
|
if (theme) {
|
||||||
await applyTheme(theme);
|
await applyTheme(theme);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import browser from 'webextension-polyfill';
|
|
||||||
import localforage from 'localforage';
|
import localforage from 'localforage';
|
||||||
import { CustomTheme, ThemeList } from '../../../interface/types/CustomThemes';
|
import { CustomTheme, ThemeList } from '../../../interface/types/CustomThemes';
|
||||||
import { blobToBase64 } from '../../utils/blobToBase64';
|
import { blobToBase64 } from '../../utils/blobToBase64';
|
||||||
|
import { settingsState } from '../../utils/listeners/SettingsState';
|
||||||
|
|
||||||
export const getAvailableThemes = async (): Promise<ThemeList | {}> => {
|
export const getAvailableThemes = async (): Promise<ThemeList | {}> => {
|
||||||
try {
|
try {
|
||||||
@@ -19,9 +18,7 @@ export const getAvailableThemes = async (): Promise<ThemeList | {}> => {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
const selectedTheme = await browser.storage.local.get('selectedTheme') as { selectedTheme: string; };
|
return { themes, selectedTheme: settingsState.selectedTheme ? settingsState.selectedTheme : '' };
|
||||||
|
|
||||||
return { themes, selectedTheme: selectedTheme.selectedTheme ? selectedTheme.selectedTheme : '' };
|
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
themes: [],
|
themes: [],
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import localforage from 'localforage';
|
import localforage from 'localforage';
|
||||||
import { CustomTheme } from '../../../interface/types/CustomThemes';
|
import { CustomTheme } from '../../../interface/types/CustomThemes';
|
||||||
import browser from 'webextension-polyfill';
|
import { settingsState } from '../../utils/listeners/SettingsState';
|
||||||
|
|
||||||
export const removeTheme = async (theme: CustomTheme) => {
|
export const removeTheme = async (theme: CustomTheme) => {
|
||||||
// Remove custom CSS
|
// Remove custom CSS
|
||||||
@@ -9,18 +9,15 @@ export const removeTheme = async (theme: CustomTheme) => {
|
|||||||
styleElement.parentNode?.removeChild(styleElement);
|
styleElement.parentNode?.removeChild(styleElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
const themeSelectedColor = await browser.storage.local.get('selectedColor') as { selectedColor: string; };
|
|
||||||
|
|
||||||
const selectedTheme = await localforage.getItem(theme.id) as CustomTheme;
|
const selectedTheme = await localforage.getItem(theme.id) as CustomTheme;
|
||||||
localforage.setItem(theme.id, {
|
localforage.setItem(theme.id, {
|
||||||
...selectedTheme,
|
...selectedTheme,
|
||||||
selectedColor: themeSelectedColor.selectedColor
|
selectedColor: settingsState.selectedColor
|
||||||
})
|
})
|
||||||
|
|
||||||
// Reset default color
|
// Reset default color
|
||||||
const originalSelectedColor = await browser.storage.local.get('originalSelectedColor') as { originalSelectedColor: string; };
|
if (settingsState.originalSelectedColor !== '') {
|
||||||
if (originalSelectedColor.originalSelectedColor !== '') {
|
settingsState.selectedColor = settingsState.originalSelectedColor
|
||||||
await browser.storage.local.set({ selectedColor: originalSelectedColor.originalSelectedColor });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove custom images
|
// Remove custom images
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ import localforage from 'localforage';
|
|||||||
import { CustomTheme } from '../../../interface/types/CustomThemes';
|
import { CustomTheme } from '../../../interface/types/CustomThemes';
|
||||||
import { applyTheme } from './applyTheme';
|
import { applyTheme } from './applyTheme';
|
||||||
import { removeTheme } from './removeTheme';
|
import { removeTheme } from './removeTheme';
|
||||||
|
import { settingsState } from '../../utils/listeners/SettingsState';
|
||||||
|
|
||||||
|
|
||||||
export const setTheme = async (themeId: string) => {
|
export const setTheme = async (themeId: string) => {
|
||||||
try {
|
try {
|
||||||
const enabledTheme = await browser.storage.local.get('selectedTheme') as { selectedTheme: string; };
|
|
||||||
const theme = await localforage.getItem(themeId) as CustomTheme;
|
const theme = await localforage.getItem(themeId) as CustomTheme;
|
||||||
|
|
||||||
console.debug('Loading theme', theme);
|
console.debug('Loading theme', theme);
|
||||||
@@ -17,25 +17,21 @@ export const setTheme = async (themeId: string) => {
|
|||||||
const styleElement = document.getElementById('custom-theme');
|
const styleElement = document.getElementById('custom-theme');
|
||||||
|
|
||||||
// Remove the currently enabled theme
|
// Remove the currently enabled theme
|
||||||
if (enabledTheme.selectedTheme || styleElement) {
|
if (settingsState.selectedTheme || styleElement) {
|
||||||
const currentTheme = await localforage.getItem(enabledTheme.selectedTheme) as CustomTheme;
|
const currentTheme = await localforage.getItem(settingsState.selectedTheme) as CustomTheme;
|
||||||
if (currentTheme) {
|
if (currentTheme) {
|
||||||
await removeTheme(currentTheme);
|
await removeTheme(currentTheme);
|
||||||
}
|
}
|
||||||
const color = await browser.storage.local.get('originalSelectedColor') as { originalSelectedColor: string; };
|
originalSelectedColor = { selectedColor: settingsState.originalSelectedColor };
|
||||||
originalSelectedColor = { selectedColor: color.originalSelectedColor };
|
|
||||||
} else {
|
} else {
|
||||||
originalSelectedColor = await browser.storage.local.get('selectedColor') as { selectedColor: string; };
|
originalSelectedColor = { selectedColor: settingsState.selectedColor };
|
||||||
}
|
}
|
||||||
|
|
||||||
await applyTheme(theme);
|
await applyTheme(theme);
|
||||||
|
|
||||||
await browser.storage.local.set({
|
settingsState.selectedTheme = themeId
|
||||||
selectedTheme: themeId,
|
settingsState.selectedColor = theme.selectedColor ? theme.selectedColor : (theme.defaultColour !== '' ? theme.defaultColour : '#007bff')
|
||||||
selectedColor: theme.selectedColor ? theme.selectedColor : (theme.defaultColour !== '' ? theme.defaultColour : '#007bff'),
|
settingsState.originalSelectedColor = originalSelectedColor.selectedColor
|
||||||
originalSelectedColor: originalSelectedColor.selectedColor
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error setting theme:', error);
|
console.error('Error setting theme:', error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,19 @@ class StorageManager {
|
|||||||
Reflect.set(target.data, prop, value);
|
Reflect.set(target.data, prop, value);
|
||||||
target.saveToStorage();
|
target.saveToStorage();
|
||||||
return true;
|
return true;
|
||||||
|
},
|
||||||
|
deleteProperty: (target, prop: keyof SettingsState) => {
|
||||||
|
const oldValue = target.data[prop];
|
||||||
|
if (oldValue !== undefined) {
|
||||||
|
delete target.data[prop];
|
||||||
|
target.removeFromStorage(prop);
|
||||||
|
if (target.listeners[prop]) {
|
||||||
|
for (const listener of target.listeners[prop]) {
|
||||||
|
listener(undefined, oldValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -54,6 +67,10 @@ class StorageManager {
|
|||||||
await browser.storage.local.set(this.data);
|
await browser.storage.local.set(this.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async removeFromStorage(key: string): Promise<void> {
|
||||||
|
await browser.storage.local.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
private initStorageListener(): void {
|
private initStorageListener(): void {
|
||||||
browser.storage.onChanged.addListener((changes, areaName) => {
|
browser.storage.onChanged.addListener((changes, areaName) => {
|
||||||
if (areaName === 'local') {
|
if (areaName === 'local') {
|
||||||
@@ -73,6 +90,11 @@ class StorageManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a listener for a setting.
|
||||||
|
* @param prop The setting to listen to.
|
||||||
|
* @param listener The listener to call when the setting changes.
|
||||||
|
*/
|
||||||
public register(prop: keyof SettingsState, listener: ChangeListener): void {
|
public register(prop: keyof SettingsState, listener: ChangeListener): void {
|
||||||
if (!this.listeners[prop]) {
|
if (!this.listeners[prop]) {
|
||||||
this.listeners[prop] = [];
|
this.listeners[prop] = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user