开启开发者模式引导
This commit is contained in:
@ -15,7 +15,7 @@ import { subscribeScriptDelete, subscribeScriptEnable, subscribeScriptInstall }
|
|||||||
import { ScriptService } from "./script";
|
import { ScriptService } from "./script";
|
||||||
import { runScript, stopScript } from "../offscreen/client";
|
import { runScript, stopScript } from "../offscreen/client";
|
||||||
import { getRunAt } from "./utils";
|
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 Cache from "@App/app/cache";
|
||||||
import { dealPatternMatches, UrlMatch } from "@App/pkg/utils/match";
|
import { dealPatternMatches, UrlMatch } from "@App/pkg/utils/match";
|
||||||
import { ExtensionContentMessageSend } from "@Packages/message/extension_message";
|
import { ExtensionContentMessageSend } from "@Packages/message/extension_message";
|
||||||
@ -27,6 +27,8 @@ import LoggerCore from "@App/app/logger/core";
|
|||||||
import PermissionVerify from "./permission_verify";
|
import PermissionVerify from "./permission_verify";
|
||||||
import { SystemConfig } from "@App/pkg/config/config";
|
import { SystemConfig } from "@App/pkg/config/config";
|
||||||
import { ResourceService } from "./resource";
|
import { ResourceService } from "./resource";
|
||||||
|
import { LocalStorageDAO } from "@App/app/repo/localStorage";
|
||||||
|
import i18n from "@App/locales/locales";
|
||||||
|
|
||||||
// 为了优化性能,存储到缓存时删除了code、value与resource
|
// 为了优化性能,存储到缓存时删除了code、value与resource
|
||||||
export interface ScriptMatchInfo extends ScriptRunResouce {
|
export interface ScriptMatchInfo extends ScriptRunResouce {
|
||||||
@ -49,6 +51,8 @@ export class RuntimeService {
|
|||||||
scriptCustomizeMatch: UrlMatch<string> = new UrlMatch<string>();
|
scriptCustomizeMatch: UrlMatch<string> = new UrlMatch<string>();
|
||||||
scriptMatchCache: Map<string, ScriptMatchInfo> | null | undefined;
|
scriptMatchCache: Map<string, ScriptMatchInfo> | null | undefined;
|
||||||
|
|
||||||
|
isEnableDeveloperMode = false;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private systemConfig: SystemConfig,
|
private systemConfig: SystemConfig,
|
||||||
private group: Group,
|
private group: Group,
|
||||||
@ -71,9 +75,32 @@ export class RuntimeService {
|
|||||||
this.group.on("pageLoad", this.pageLoad.bind(this));
|
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注入页面
|
// 读取inject.js注入页面
|
||||||
this.registerInjectScript();
|
this.registerInjectScript();
|
||||||
@ -439,7 +466,7 @@ export class RuntimeService {
|
|||||||
this.addScriptMatch(scriptMatchInfo);
|
this.addScriptMatch(scriptMatchInfo);
|
||||||
|
|
||||||
// 如果脚本开启, 则注册脚本
|
// 如果脚本开启, 则注册脚本
|
||||||
if (script.status === SCRIPT_STATUS_ENABLE) {
|
if (this.isEnableDeveloperMode && script.status === SCRIPT_STATUS_ENABLE) {
|
||||||
if (!scriptRes.metadata["noframes"]) {
|
if (!scriptRes.metadata["noframes"]) {
|
||||||
registerScript.allFrames = true;
|
registerScript.allFrames = true;
|
||||||
}
|
}
|
||||||
@ -467,7 +494,7 @@ export class RuntimeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async unregistryPageScript(uuid: string) {
|
async unregistryPageScript(uuid: string) {
|
||||||
if (!(await Cache.getInstance().get("registryScript:" + uuid))) {
|
if (!this.isEnableDeveloperMode || !(await Cache.getInstance().get("registryScript:" + uuid))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
chrome.userScripts.unregister(
|
chrome.userScripts.unregister(
|
||||||
|
@ -21,7 +21,7 @@ import { ResourceService } from "./resource";
|
|||||||
import { ValueService } from "./value";
|
import { ValueService } from "./value";
|
||||||
import { compileScriptCode } from "../content/utils";
|
import { compileScriptCode } from "../content/utils";
|
||||||
import { SystemConfig } from "@App/pkg/config/config";
|
import { SystemConfig } from "@App/pkg/config/config";
|
||||||
import i18n from "@App/locales/locales";
|
import i18n, { localePath } from "@App/locales/locales";
|
||||||
|
|
||||||
export class ScriptService {
|
export class ScriptService {
|
||||||
logger: Logger;
|
logger: Logger;
|
||||||
@ -109,10 +109,6 @@ export class ScriptService {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
// 获取i18n
|
// 获取i18n
|
||||||
let localePath = "";
|
|
||||||
if (i18n.language !== "zh-CN") {
|
|
||||||
localePath = `/en`;
|
|
||||||
}
|
|
||||||
// 重定向到脚本安装页
|
// 重定向到脚本安装页
|
||||||
chrome.declarativeNetRequest.updateDynamicRules(
|
chrome.declarativeNetRequest.updateDynamicRules(
|
||||||
{
|
{
|
||||||
|
@ -27,10 +27,15 @@ i18n.use(initReactI18next).init({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export let localePath = "";
|
||||||
|
|
||||||
chrome.i18n.getAcceptLanguages((lngs) => {
|
chrome.i18n.getAcceptLanguages((lngs) => {
|
||||||
systemConfig.getLanguage().then((lng) => {
|
systemConfig.getLanguage(lngs).then((lng) => {
|
||||||
i18n.changeLanguage(lng);
|
i18n.changeLanguage(lng);
|
||||||
dayjs.locale(lng.toLocaleLowerCase());
|
dayjs.locale(lng.toLocaleLowerCase());
|
||||||
|
if (lng !== "zh-CN") {
|
||||||
|
localePath = "en";
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -368,5 +368,6 @@
|
|||||||
"eslint_config_format_error": "eslint配置格式错误",
|
"eslint_config_format_error": "eslint配置格式错误",
|
||||||
"export_success": "导出成功",
|
"export_success": "导出成功",
|
||||||
"get_backup_dir_url_failed": "获取备份目录地址失败",
|
"get_backup_dir_url_failed": "获取备份目录地址失败",
|
||||||
"get_backup_files_failed": "获取备份文件失败"
|
"get_backup_files_failed": "获取备份文件失败",
|
||||||
|
"develop_mode_guide": "检测到当前未开启开发者模式,您的脚本无法正常使用,<a href=\"https://docs.scriptcat.org/docs/use/open-dev/\" target=\"black\" style=\"color: var(--color-text-1)\">👉点我了解如何开启</a>"
|
||||||
}
|
}
|
@ -18,7 +18,7 @@ import ScriptMenuList from "../components/ScriptMenuList";
|
|||||||
import { popupClient } from "../store/features/script";
|
import { popupClient } from "../store/features/script";
|
||||||
import { ScriptMenu } from "@App/app/service/service_worker/popup";
|
import { ScriptMenu } from "@App/app/service/service_worker/popup";
|
||||||
import { systemConfig } from "../store/global";
|
import { systemConfig } from "../store/global";
|
||||||
import { SystemConfig } from "@App/pkg/config/config";
|
import { isUserScriptsAvailable } from "@App/pkg/utils/utils";
|
||||||
|
|
||||||
const CollapseItem = Collapse.Item;
|
const CollapseItem = Collapse.Item;
|
||||||
|
|
||||||
@ -79,6 +79,10 @@ function App() {
|
|||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
return (
|
return (
|
||||||
|
<>
|
||||||
|
{!isUserScriptsAvailable() && (
|
||||||
|
<Alert type="warning" content={<div dangerouslySetInnerHTML={{ __html: t("develop_mode_guide") }} />} />
|
||||||
|
)}
|
||||||
<Card
|
<Card
|
||||||
size="small"
|
size="small"
|
||||||
title={
|
title={
|
||||||
@ -113,7 +117,6 @@ function App() {
|
|||||||
iconOnly
|
iconOnly
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setShowAlert(!showAlert);
|
setShowAlert(!showAlert);
|
||||||
console.log(checkUpdate);
|
|
||||||
checkUpdate.isRead = true;
|
checkUpdate.isRead = true;
|
||||||
setCheckUpdate(checkUpdate);
|
setCheckUpdate(checkUpdate);
|
||||||
systemConfig.setCheckUpdate(checkUpdate);
|
systemConfig.setCheckUpdate(checkUpdate);
|
||||||
@ -206,7 +209,7 @@ function App() {
|
|||||||
{semver.lt(ExtVersion, checkUpdate.version) && (
|
{semver.lt(ExtVersion, checkUpdate.version) && (
|
||||||
<span
|
<span
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
window.open(`https://github.com/scriptscat/scriptcat/releases/tag/v${version}`);
|
window.open(`https://github.com/scriptscat/scriptcat/releases/tag/v${checkUpdate.version}`);
|
||||||
}}
|
}}
|
||||||
className="text-1 font-500"
|
className="text-1 font-500"
|
||||||
style={{ cursor: "pointer" }}
|
style={{ cursor: "pointer" }}
|
||||||
@ -216,6 +219,7 @@ function App() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</Card>
|
</Card>
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,19 +225,20 @@ export class SystemConfig {
|
|||||||
this.set("menu_expand_num", val);
|
this.set("menu_expand_num", val);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getLanguage() {
|
async getLanguage(acceptLanguages?: string[]): Promise<string> {
|
||||||
const defaultLanguage = await new Promise<string>((resolve) => {
|
const defaultLanguage = await new Promise<string>(async (resolve) => {
|
||||||
chrome.i18n.getAcceptLanguages((lngs) => {
|
if (!acceptLanguages) {
|
||||||
|
acceptLanguages = await chrome.i18n.getAcceptLanguages();
|
||||||
|
}
|
||||||
// 遍历数组寻找匹配语言
|
// 遍历数组寻找匹配语言
|
||||||
for (let i = 0; i < lngs.length; i += 1) {
|
for (let i = 0; i < acceptLanguages.length; i += 1) {
|
||||||
const lng = lngs[i];
|
const lng = acceptLanguages[i];
|
||||||
if (i18n.hasResourceBundle(lng, "translation")) {
|
if (i18n.hasResourceBundle(lng, "translation")) {
|
||||||
resolve(lng);
|
resolve(lng);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
return this.get("language", defaultLanguage || chrome.i18n.getUILanguage());
|
return this.get("language", defaultLanguage || chrome.i18n.getUILanguage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,7 +266,6 @@ export function errorMsg(e: any): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function isUserScriptsAvailable() {
|
export function isUserScriptsAvailable() {
|
||||||
return false;
|
|
||||||
try {
|
try {
|
||||||
// Property access which throws if developer mode is not enabled.
|
// Property access which throws if developer mode is not enabled.
|
||||||
chrome.userScripts;
|
chrome.userScripts;
|
||||||
|
Reference in New Issue
Block a user