diff --git a/src/app/service/service_worker/runtime.ts b/src/app/service/service_worker/runtime.ts index 362376a..f8ab520 100644 --- a/src/app/service/service_worker/runtime.ts +++ b/src/app/service/service_worker/runtime.ts @@ -15,7 +15,7 @@ import { subscribeScriptDelete, subscribeScriptEnable, subscribeScriptInstall } import { ScriptService } from "./script"; import { runScript, stopScript } from "../offscreen/client"; import { getRunAt } from "./utils"; -import { isUserScriptsAvailable, randomString } from "@App/pkg/utils/utils"; +import { InfoNotification, isUserScriptsAvailable, randomString } from "@App/pkg/utils/utils"; import Cache from "@App/app/cache"; import { dealPatternMatches, UrlMatch } from "@App/pkg/utils/match"; import { ExtensionContentMessageSend } from "@Packages/message/extension_message"; @@ -27,6 +27,8 @@ import LoggerCore from "@App/app/logger/core"; import PermissionVerify from "./permission_verify"; import { SystemConfig } from "@App/pkg/config/config"; import { ResourceService } from "./resource"; +import { LocalStorageDAO } from "@App/app/repo/localStorage"; +import i18n from "@App/locales/locales"; // 为了优化性能,存储到缓存时删除了code、value与resource export interface ScriptMatchInfo extends ScriptRunResouce { @@ -49,6 +51,8 @@ export class RuntimeService { scriptCustomizeMatch: UrlMatch = new UrlMatch(); scriptMatchCache: Map | null | undefined; + isEnableDeveloperMode = false; + constructor( private systemConfig: SystemConfig, private group: Group, @@ -71,9 +75,32 @@ export class RuntimeService { this.group.on("pageLoad", this.pageLoad.bind(this)); // 检查是否开启了开发者模式 - if(!isUserScriptsAvailable()){ - // 未开启加上警告引导 - + this.isEnableDeveloperMode = isUserScriptsAvailable(); + if (!this.isEnableDeveloperMode) { + // 未开启加上警告引导 + // 判断是否首次 + const localStorage = new LocalStorageDAO(); + localStorage.get("firstShowDeveloperMode").then((res) => { + if (!res) { + localStorage.save({ + key: "firstShowDeveloperMode", + value: true, + }); + // 打开页面 + chrome.tabs.create({ + url: `https://docs.scriptcat.org/docs/use/open-dev/`, + }); + } + }); + chrome.action.setBadgeBackgroundColor({ + color: "#ff8c00", + }); + chrome.action.setBadgeTextColor({ + color: "#ffffff", + }); + chrome.action.setBadgeText({ + text: "!", + }); } // 读取inject.js注入页面 this.registerInjectScript(); @@ -439,7 +466,7 @@ export class RuntimeService { this.addScriptMatch(scriptMatchInfo); // 如果脚本开启, 则注册脚本 - if (script.status === SCRIPT_STATUS_ENABLE) { + if (this.isEnableDeveloperMode && script.status === SCRIPT_STATUS_ENABLE) { if (!scriptRes.metadata["noframes"]) { registerScript.allFrames = true; } @@ -467,7 +494,7 @@ export class RuntimeService { } async unregistryPageScript(uuid: string) { - if (!(await Cache.getInstance().get("registryScript:" + uuid))) { + if (!this.isEnableDeveloperMode || !(await Cache.getInstance().get("registryScript:" + uuid))) { return; } chrome.userScripts.unregister( diff --git a/src/app/service/service_worker/script.ts b/src/app/service/service_worker/script.ts index bb8ed23..25ea657 100644 --- a/src/app/service/service_worker/script.ts +++ b/src/app/service/service_worker/script.ts @@ -21,7 +21,7 @@ import { ResourceService } from "./resource"; import { ValueService } from "./value"; import { compileScriptCode } from "../content/utils"; import { SystemConfig } from "@App/pkg/config/config"; -import i18n from "@App/locales/locales"; +import i18n, { localePath } from "@App/locales/locales"; export class ScriptService { logger: Logger; @@ -109,10 +109,6 @@ export class ScriptService { } ); // 获取i18n - let localePath = ""; - if (i18n.language !== "zh-CN") { - localePath = `/en`; - } // 重定向到脚本安装页 chrome.declarativeNetRequest.updateDynamicRules( { diff --git a/src/locales/locales.ts b/src/locales/locales.ts index 3739a4b..2e8d8c9 100644 --- a/src/locales/locales.ts +++ b/src/locales/locales.ts @@ -27,10 +27,15 @@ i18n.use(initReactI18next).init({ }, }); +export let localePath = ""; + chrome.i18n.getAcceptLanguages((lngs) => { - systemConfig.getLanguage().then((lng) => { + systemConfig.getLanguage(lngs).then((lng) => { i18n.changeLanguage(lng); dayjs.locale(lng.toLocaleLowerCase()); + if (lng !== "zh-CN") { + localePath = "en"; + } }); }); diff --git a/src/locales/zh-CN/translation.json b/src/locales/zh-CN/translation.json index 46db070..cdf42f5 100644 --- a/src/locales/zh-CN/translation.json +++ b/src/locales/zh-CN/translation.json @@ -368,5 +368,6 @@ "eslint_config_format_error": "eslint配置格式错误", "export_success": "导出成功", "get_backup_dir_url_failed": "获取备份目录地址失败", - "get_backup_files_failed": "获取备份文件失败" + "get_backup_files_failed": "获取备份文件失败", + "develop_mode_guide": "检测到当前未开启开发者模式,您的脚本无法正常使用,👉点我了解如何开启" } \ No newline at end of file diff --git a/src/pages/popup/App.tsx b/src/pages/popup/App.tsx index c74a236..56b419f 100644 --- a/src/pages/popup/App.tsx +++ b/src/pages/popup/App.tsx @@ -18,7 +18,7 @@ import ScriptMenuList from "../components/ScriptMenuList"; import { popupClient } from "../store/features/script"; import { ScriptMenu } from "@App/app/service/service_worker/popup"; import { systemConfig } from "../store/global"; -import { SystemConfig } from "@App/pkg/config/config"; +import { isUserScriptsAvailable } from "@App/pkg/utils/utils"; const CollapseItem = Collapse.Item; @@ -79,143 +79,147 @@ function App() { }); }, []); return ( - - ScriptCat -
- { - setIsEnableScript(val); - if (val) { - localStorage.enable_script = "true"; - } else { - localStorage.enable_script = "false"; - } - }} - /> -
- - } - bodyStyle={{ padding: 0 }} - > - } - /> - - - - - - - - - -
- {`v${ExtVersion}`} - {semver.lt(ExtVersion, checkUpdate.version) && ( - { - window.open(`https://github.com/scriptscat/scriptcat/releases/tag/v${version}`); - }} - className="text-1 font-500" - style={{ cursor: "pointer" }} + } + bodyStyle={{ padding: 0 }} + > + } + /> + + - {t("popup.new_version_available")} - - )} -
-
+ + + + + + + +
+ {`v${ExtVersion}`} + {semver.lt(ExtVersion, checkUpdate.version) && ( + { + window.open(`https://github.com/scriptscat/scriptcat/releases/tag/v${checkUpdate.version}`); + }} + className="text-1 font-500" + style={{ cursor: "pointer" }} + > + {t("popup.new_version_available")} + + )} +
+ + ); } diff --git a/src/pkg/config/config.ts b/src/pkg/config/config.ts index 8aad7ce..108126c 100644 --- a/src/pkg/config/config.ts +++ b/src/pkg/config/config.ts @@ -225,18 +225,19 @@ export class SystemConfig { this.set("menu_expand_num", val); } - async getLanguage() { - const defaultLanguage = await new Promise((resolve) => { - chrome.i18n.getAcceptLanguages((lngs) => { - // 遍历数组寻找匹配语言 - for (let i = 0; i < lngs.length; i += 1) { - const lng = lngs[i]; - if (i18n.hasResourceBundle(lng, "translation")) { - resolve(lng); - break; - } + async getLanguage(acceptLanguages?: string[]): Promise { + const defaultLanguage = await new Promise(async (resolve) => { + if (!acceptLanguages) { + acceptLanguages = await chrome.i18n.getAcceptLanguages(); + } + // 遍历数组寻找匹配语言 + for (let i = 0; i < acceptLanguages.length; i += 1) { + const lng = acceptLanguages[i]; + if (i18n.hasResourceBundle(lng, "translation")) { + resolve(lng); + break; } - }); + } }); return this.get("language", defaultLanguage || chrome.i18n.getUILanguage()); } diff --git a/src/pkg/utils/utils.ts b/src/pkg/utils/utils.ts index f6b9f76..8be269b 100644 --- a/src/pkg/utils/utils.ts +++ b/src/pkg/utils/utils.ts @@ -266,7 +266,6 @@ export function errorMsg(e: any): string { } export function isUserScriptsAvailable() { - return false; try { // Property access which throws if developer mode is not enabled. chrome.userScripts;