fix: add global context type

This commit is contained in:
Gabe
2025-11-22 01:36:03 +08:00
parent 0705e8a65a
commit fa6f68fec3
7 changed files with 36 additions and 5 deletions

View File

@@ -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;

View File

@@ -1,3 +1,5 @@
import { run } from "./common";
globalThis.__KISS_CONTEXT__ = "content";
run();

View File

@@ -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) {

View File

@@ -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]);

View File

@@ -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;

View File

@@ -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(
<React.StrictMode>

View File

@@ -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(
<React.StrictMode>