fix: add global context type
This commit is contained in:
@@ -35,7 +35,7 @@ import { injectInlineJsBg, injectInternalCss } from "./libs/injector";
|
|||||||
import { kissLog, logger } from "./libs/log";
|
import { kissLog, logger } from "./libs/log";
|
||||||
import { chromeDetect, chromeTranslate } from "./libs/builtinAI";
|
import { chromeDetect, chromeTranslate } from "./libs/builtinAI";
|
||||||
|
|
||||||
globalThis.ContextType = "BACKGROUND";
|
globalThis.__KISS_CONTEXT__ = "background";
|
||||||
|
|
||||||
const CSP_RULE_START_ID = 1;
|
const CSP_RULE_START_ID = 1;
|
||||||
const ORI_RULE_START_ID = 10000;
|
const ORI_RULE_START_ID = 10000;
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
import { run } from "./common";
|
import { run } from "./common";
|
||||||
|
|
||||||
|
globalThis.__KISS_CONTEXT__ = "content";
|
||||||
|
|
||||||
run();
|
run();
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ const SettingContext = createContext({
|
|||||||
reloadSetting: () => {},
|
reloadSetting: () => {},
|
||||||
});
|
});
|
||||||
|
|
||||||
export function SettingProvider({ children, isSettingPage }) {
|
export function SettingProvider({ children, isSettingPage = false }) {
|
||||||
const {
|
const {
|
||||||
data: setting,
|
data: setting,
|
||||||
isLoading,
|
isLoading,
|
||||||
@@ -81,12 +81,13 @@ export function SettingProvider({ children, isSettingPage }) {
|
|||||||
|
|
||||||
const value = useMemo(
|
const value = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
|
isSettingPage,
|
||||||
setting,
|
setting,
|
||||||
updateSetting,
|
updateSetting,
|
||||||
updateChild,
|
updateChild,
|
||||||
reloadSetting: reload,
|
reloadSetting: reload,
|
||||||
}),
|
}),
|
||||||
[setting, updateSetting, updateChild, reload]
|
[isSettingPage, setting, updateSetting, updateChild, reload]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { storage } from "../libs/storage";
|
|||||||
import { kissLog } from "../libs/log";
|
import { kissLog } from "../libs/log";
|
||||||
import { syncData } from "../libs/sync";
|
import { syncData } from "../libs/sync";
|
||||||
import { useDebouncedCallback } from "./DebouncedCallback";
|
import { useDebouncedCallback } from "./DebouncedCallback";
|
||||||
|
import { getContext } from "../libs/browser";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用于将组件状态与 Storage 同步
|
* 用于将组件状态与 Storage 同步
|
||||||
@@ -79,7 +80,8 @@ export function useStorage(key, defaultVal = null, syncKey = "") {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 触发远端同步
|
// 触发远端同步
|
||||||
if (syncKey) {
|
const context = getContext();
|
||||||
|
if (syncKey && context === "options") {
|
||||||
debouncedSync(syncKey, data);
|
debouncedSync(syncKey, data);
|
||||||
}
|
}
|
||||||
}, [key, syncKey, isLoading, data, debouncedSync]);
|
}, [key, syncKey, isLoading, data, debouncedSync]);
|
||||||
|
|||||||
@@ -14,7 +14,29 @@ function _browser() {
|
|||||||
|
|
||||||
export const browser = _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 =
|
export const isBuiltinAIAvailable =
|
||||||
"LanguageDetector" in globalThis && "Translator" in globalThis;
|
"LanguageDetector" in globalThis && "Translator" in globalThis;
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import React from "react";
|
|||||||
import ReactDOM from "react-dom/client";
|
import ReactDOM from "react-dom/client";
|
||||||
import Options from "./views/Options";
|
import Options from "./views/Options";
|
||||||
|
|
||||||
|
globalThis.__KISS_CONTEXT__ = "options";
|
||||||
|
|
||||||
const root = ReactDOM.createRoot(document.getElementById("root"));
|
const root = ReactDOM.createRoot(document.getElementById("root"));
|
||||||
root.render(
|
root.render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import { SettingProvider } from "./hooks/Setting";
|
|||||||
import ThemeProvider from "./hooks/Theme";
|
import ThemeProvider from "./hooks/Theme";
|
||||||
import Popup from "./views/Popup";
|
import Popup from "./views/Popup";
|
||||||
|
|
||||||
|
globalThis.__KISS_CONTEXT__ = "popup";
|
||||||
|
|
||||||
const root = ReactDOM.createRoot(document.getElementById("root"));
|
const root = ReactDOM.createRoot(document.getElementById("root"));
|
||||||
root.render(
|
root.render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
|
|||||||
Reference in New Issue
Block a user