脚本开启总开关
This commit is contained in:
parent
44041f4735
commit
ec28795dbb
@ -1,4 +1,4 @@
|
|||||||
import { MessageQueue } from "@Packages/message/message_queue";
|
import { MessageQueue, Unsubscribe } from "@Packages/message/message_queue";
|
||||||
import { ExtMessageSender, GetSender, Group, MessageSend } from "@Packages/message/server";
|
import { ExtMessageSender, GetSender, Group, MessageSend } from "@Packages/message/server";
|
||||||
import {
|
import {
|
||||||
Script,
|
Script,
|
||||||
@ -49,6 +49,7 @@ export class RuntimeService {
|
|||||||
scriptMatchCache: Map<string, ScriptMatchInfo> | null | undefined;
|
scriptMatchCache: Map<string, ScriptMatchInfo> | null | undefined;
|
||||||
|
|
||||||
isEnableDeveloperMode = false;
|
isEnableDeveloperMode = false;
|
||||||
|
isEnableUserscribe = true;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private systemConfig: SystemConfig,
|
private systemConfig: SystemConfig,
|
||||||
@ -99,8 +100,7 @@ export class RuntimeService {
|
|||||||
text: "!",
|
text: "!",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// 读取inject.js注入页面
|
|
||||||
this.registerInjectScript();
|
|
||||||
// 监听脚本开启
|
// 监听脚本开启
|
||||||
subscribeScriptEnable(this.mq, async (data) => {
|
subscribeScriptEnable(this.mq, async (data) => {
|
||||||
const script = await this.scriptDAO.getAndCode(data.uuid);
|
const script = await this.scriptDAO.getAndCode(data.uuid);
|
||||||
@ -111,6 +111,7 @@ export class RuntimeService {
|
|||||||
// 如果是后台脚本, 在offscreen中进行处理
|
// 如果是后台脚本, 在offscreen中进行处理
|
||||||
if (script.type === SCRIPT_TYPE_NORMAL) {
|
if (script.type === SCRIPT_TYPE_NORMAL) {
|
||||||
// 加载页面脚本
|
// 加载页面脚本
|
||||||
|
// 不管开没开启都要加载一次脚本信息
|
||||||
await this.loadPageScript(script);
|
await this.loadPageScript(script);
|
||||||
if (!data.enable) {
|
if (!data.enable) {
|
||||||
await this.unregistryPageScript(script.uuid);
|
await this.unregistryPageScript(script.uuid);
|
||||||
@ -133,6 +134,32 @@ export class RuntimeService {
|
|||||||
this.deleteScriptMatch(uuid);
|
this.deleteScriptMatch(uuid);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.systemConfig.addListener("enable_script", (enable) => {
|
||||||
|
this.isEnableUserscribe = enable;
|
||||||
|
if (enable) {
|
||||||
|
this.registerUserscripts();
|
||||||
|
} else {
|
||||||
|
this.unregisterUserscripts();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// 检查是否开启
|
||||||
|
this.isEnableUserscribe = await this.systemConfig.getEnableScript();
|
||||||
|
if (this.isEnableUserscribe) {
|
||||||
|
this.registerUserscripts();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsubscribe: Unsubscribe[] = [];
|
||||||
|
|
||||||
|
// 取消脚本注册
|
||||||
|
unregisterUserscripts() {
|
||||||
|
chrome.userScripts.unregister();
|
||||||
|
this.deleteMessageFlag();
|
||||||
|
}
|
||||||
|
|
||||||
|
async registerUserscripts() {
|
||||||
|
// 读取inject.js注入页面
|
||||||
|
this.registerInjectScript();
|
||||||
// 将开启的脚本发送一次enable消息
|
// 将开启的脚本发送一次enable消息
|
||||||
const scriptDao = new ScriptDAO();
|
const scriptDao = new ScriptDAO();
|
||||||
const list = await scriptDao.all();
|
const list = await scriptDao.all();
|
||||||
@ -161,6 +188,10 @@ export class RuntimeService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deleteMessageFlag() {
|
||||||
|
return Cache.getInstance().del("scriptInjectMessageFlag");
|
||||||
|
}
|
||||||
|
|
||||||
getMessageFlag() {
|
getMessageFlag() {
|
||||||
return Cache.getInstance().get("scriptInjectMessageFlag");
|
return Cache.getInstance().get("scriptInjectMessageFlag");
|
||||||
}
|
}
|
||||||
@ -417,8 +448,11 @@ export class RuntimeService {
|
|||||||
if (!this.scriptMatchCache) {
|
if (!this.scriptMatchCache) {
|
||||||
await this.loadScriptMatchInfo();
|
await this.loadScriptMatchInfo();
|
||||||
}
|
}
|
||||||
this.scriptMatchCache!.get(uuid)!.status = status;
|
const script = await this.scriptMatchCache!.get(uuid);
|
||||||
this.saveScriptMatchInfo();
|
if (script) {
|
||||||
|
script.status = status;
|
||||||
|
this.saveScriptMatchInfo();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteScriptMatch(uuid: string) {
|
async deleteScriptMatch(uuid: string) {
|
||||||
@ -484,7 +518,7 @@ export class RuntimeService {
|
|||||||
this.addScriptMatch(scriptMatchInfo);
|
this.addScriptMatch(scriptMatchInfo);
|
||||||
|
|
||||||
// 如果脚本开启, 则注册脚本
|
// 如果脚本开启, 则注册脚本
|
||||||
if (this.isEnableDeveloperMode && script.status === SCRIPT_STATUS_ENABLE) {
|
if (this.isEnableDeveloperMode && this.isEnableUserscribe && script.status === SCRIPT_STATUS_ENABLE) {
|
||||||
if (scriptRes.metadata["noframes"]) {
|
if (scriptRes.metadata["noframes"]) {
|
||||||
registerScript.allFrames = false;
|
registerScript.allFrames = false;
|
||||||
} else {
|
} else {
|
||||||
@ -523,19 +557,17 @@ export class RuntimeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async unregistryPageScript(uuid: string) {
|
async unregistryPageScript(uuid: string) {
|
||||||
if (!this.isEnableDeveloperMode || !(await Cache.getInstance().get("registryScript:" + uuid))) {
|
if (
|
||||||
|
!this.isEnableDeveloperMode ||
|
||||||
|
!this.isEnableUserscribe ||
|
||||||
|
!(await Cache.getInstance().get("registryScript:" + uuid))
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
chrome.userScripts.unregister(
|
// 删除缓存
|
||||||
{
|
Cache.getInstance().del("registryScript:" + uuid);
|
||||||
ids: [uuid],
|
// 修改脚本状态为disable
|
||||||
},
|
this.updateScriptStatus(uuid, SCRIPT_STATUS_DISABLE);
|
||||||
() => {
|
chrome.userScripts.unregister({ ids: [uuid] });
|
||||||
// 删除缓存
|
|
||||||
Cache.getInstance().del("registryScript:" + uuid);
|
|
||||||
// 修改脚本状态为disable
|
|
||||||
this.updateScriptStatus(uuid, SCRIPT_STATUS_DISABLE);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ import {
|
|||||||
IconSearch,
|
IconSearch,
|
||||||
} from "@arco-design/web-react/icon";
|
} from "@arco-design/web-react/icon";
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { RiMessage2Line } from "react-icons/ri";
|
import { RiMessage2Line, RiZzzFill } from "react-icons/ri";
|
||||||
import semver from "semver";
|
import semver from "semver";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import ScriptMenuList from "../components/ScriptMenuList";
|
import ScriptMenuList from "../components/ScriptMenuList";
|
||||||
@ -38,7 +38,7 @@ function App() {
|
|||||||
isRead: false,
|
isRead: false,
|
||||||
});
|
});
|
||||||
const [currentUrl, setCurrentUrl] = useState("");
|
const [currentUrl, setCurrentUrl] = useState("");
|
||||||
const [isEnableScript, setIsEnableScript] = useState(localStorage.enable_script !== "false");
|
const [isEnableScript, setIsEnableScript] = useState(true);
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
let url: URL | undefined;
|
let url: URL | undefined;
|
||||||
@ -49,9 +49,15 @@ function App() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
systemConfig.getCheckUpdate().then((res) => {
|
const loadConfig = async () => {
|
||||||
setCheckUpdate(res);
|
const [isEnableScript, checkUpdate] = await Promise.all([
|
||||||
});
|
systemConfig.getEnableScript(),
|
||||||
|
systemConfig.getCheckUpdate(),
|
||||||
|
]);
|
||||||
|
setIsEnableScript(isEnableScript);
|
||||||
|
setCheckUpdate(checkUpdate);
|
||||||
|
};
|
||||||
|
loadConfig();
|
||||||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
||||||
if (!tabs.length) {
|
if (!tabs.length) {
|
||||||
return;
|
return;
|
||||||
@ -95,9 +101,9 @@ function App() {
|
|||||||
onChange={(val) => {
|
onChange={(val) => {
|
||||||
setIsEnableScript(val);
|
setIsEnableScript(val);
|
||||||
if (val) {
|
if (val) {
|
||||||
localStorage.enable_script = "true";
|
systemConfig.setEnableScript(true);
|
||||||
} else {
|
} else {
|
||||||
localStorage.enable_script = "false";
|
systemConfig.setEnableScript(false);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
@ -35,8 +35,11 @@ export class SystemConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addListener(key: string, callback: (value: any) => void) {
|
addListener(key: string, callback: (value: any) => void) {
|
||||||
this.mq.subscribe(key, (msg) => {
|
this.mq.subscribe(SystamConfigChange, (data: { key: string; value: string }) => {
|
||||||
const { value } = msg;
|
if (data.key !== key) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const { value } = data;
|
||||||
callback(value);
|
callback(value);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -263,4 +266,12 @@ export class SystemConfig {
|
|||||||
version: ExtVersion,
|
version: ExtVersion,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setEnableScript(enable: boolean) {
|
||||||
|
this.set("enable_script", enable);
|
||||||
|
}
|
||||||
|
|
||||||
|
getEnableScript(): Promise<boolean> {
|
||||||
|
return this.get("enable_script", true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user