Files
kiss-translator/src/libs/browser.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-08-30 18:05:37 +08:00
// import { CLIENT_EXTS, CLIENT_USERSCRIPT, CLIENT_WEB } from "../config";
2023-08-05 18:15:01 +08:00
2023-07-20 13:45:41 +08:00
/**
* 浏览器兼容插件另可用于判断是插件模式还是网页模式方便开发
* @returns
*/
function _browser() {
try {
return require("webextension-polyfill");
} catch (err) {
// kissLog("browser", err);
2023-07-20 13:45:41 +08:00
}
}
2023-08-04 16:48:40 +08:00
export const browser = _browser();
2023-09-20 16:02:17 +08:00
2025-11-22 01:36:03 +08:00
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";
2025-10-04 21:25:54 +08:00
export const isBuiltinAIAvailable =
"LanguageDetector" in globalThis && "Translator" in globalThis;