From fa6f68fec393e956dad885d08b8312f870559d58 Mon Sep 17 00:00:00 2001 From: Gabe Date: Sat, 22 Nov 2025 01:36:03 +0800 Subject: [PATCH] fix: add global context type --- src/background.js | 2 +- src/content.js | 2 ++ src/hooks/Setting.js | 5 +++-- src/hooks/Storage.js | 4 +++- src/libs/browser.js | 24 +++++++++++++++++++++++- src/options.js | 2 ++ src/popup.js | 2 ++ 7 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/background.js b/src/background.js index 375ae5a..a2bdf15 100644 --- a/src/background.js +++ b/src/background.js @@ -35,7 +35,7 @@ import { injectInlineJsBg, injectInternalCss } from "./libs/injector"; import { kissLog, logger } from "./libs/log"; import { chromeDetect, chromeTranslate } from "./libs/builtinAI"; -globalThis.ContextType = "BACKGROUND"; +globalThis.__KISS_CONTEXT__ = "background"; const CSP_RULE_START_ID = 1; const ORI_RULE_START_ID = 10000; diff --git a/src/content.js b/src/content.js index 3dc1433..1b74a7e 100644 --- a/src/content.js +++ b/src/content.js @@ -1,3 +1,5 @@ import { run } from "./common"; +globalThis.__KISS_CONTEXT__ = "content"; + run(); diff --git a/src/hooks/Setting.js b/src/hooks/Setting.js index e6ae56e..83312dd 100644 --- a/src/hooks/Setting.js +++ b/src/hooks/Setting.js @@ -25,7 +25,7 @@ const SettingContext = createContext({ reloadSetting: () => {}, }); -export function SettingProvider({ children, isSettingPage }) { +export function SettingProvider({ children, isSettingPage = false }) { const { data: setting, isLoading, @@ -81,12 +81,13 @@ export function SettingProvider({ children, isSettingPage }) { const value = useMemo( () => ({ + isSettingPage, setting, updateSetting, updateChild, reloadSetting: reload, }), - [setting, updateSetting, updateChild, reload] + [isSettingPage, setting, updateSetting, updateChild, reload] ); if (isLoading) { diff --git a/src/hooks/Storage.js b/src/hooks/Storage.js index dbf8bd5..a6b9c3f 100644 --- a/src/hooks/Storage.js +++ b/src/hooks/Storage.js @@ -3,6 +3,7 @@ import { storage } from "../libs/storage"; import { kissLog } from "../libs/log"; import { syncData } from "../libs/sync"; import { useDebouncedCallback } from "./DebouncedCallback"; +import { getContext } from "../libs/browser"; /** * 用于将组件状态与 Storage 同步 @@ -79,7 +80,8 @@ export function useStorage(key, defaultVal = null, syncKey = "") { }); // 触发远端同步 - if (syncKey) { + const context = getContext(); + if (syncKey && context === "options") { debouncedSync(syncKey, data); } }, [key, syncKey, isLoading, data, debouncedSync]); diff --git a/src/libs/browser.js b/src/libs/browser.js index 79ec26f..c6ad0bc 100644 --- a/src/libs/browser.js +++ b/src/libs/browser.js @@ -14,7 +14,29 @@ function _browser() { export const browser = _browser(); -export const isBg = () => globalThis?.ContextType === "BACKGROUND"; +export const getContext = () => { + const context = globalThis.__KISS_CONTEXT__; + if (context) return context; + + // if (typeof window === "undefined" || typeof document === "undefined") { + // return "background"; + // } + + // const extensionOrigin = browser.runtime.getURL(""); + // if (!window.location.href.startsWith(extensionOrigin)) { + // return "content"; + // } + + // const pathname = window.location.pathname; + // if (pathname.includes("popup")) return "popup"; + // if (pathname.includes("options")) return "options"; + // if (pathname.includes("sidepanel")) return "sidepanel"; + // if (pathname.includes("background")) return "background"; + + return "undefined"; +}; + +export const isBg = () => getContext() === "background"; export const isBuiltinAIAvailable = "LanguageDetector" in globalThis && "Translator" in globalThis; diff --git a/src/options.js b/src/options.js index d1cf862..1370cdd 100644 --- a/src/options.js +++ b/src/options.js @@ -2,6 +2,8 @@ import React from "react"; import ReactDOM from "react-dom/client"; import Options from "./views/Options"; +globalThis.__KISS_CONTEXT__ = "options"; + const root = ReactDOM.createRoot(document.getElementById("root")); root.render( diff --git a/src/popup.js b/src/popup.js index cb20c38..6c0c173 100644 --- a/src/popup.js +++ b/src/popup.js @@ -4,6 +4,8 @@ import { SettingProvider } from "./hooks/Setting"; import ThemeProvider from "./hooks/Theme"; import Popup from "./views/Popup"; +globalThis.__KISS_CONTEXT__ = "popup"; + const root = ReactDOM.createRoot(document.getElementById("root")); root.render(