From 21899f00408712c38b447b3b38fc369f458475a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=80=E4=B9=8B?= Date: Wed, 2 Apr 2025 01:03:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/message/server.ts | 3 ++ src/app/service/service_worker/runtime.ts | 41 +++++++++++------------ src/app/service/service_worker/script.ts | 2 +- src/pkg/utils/match.test.ts | 7 ++-- src/pkg/utils/match.ts | 4 ++- 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/packages/message/server.ts b/packages/message/server.ts index ade01be..7aed1f4 100644 --- a/packages/message/server.ts +++ b/packages/message/server.ts @@ -39,7 +39,9 @@ export class Server { private logger = LoggerCore.getInstance().logger({ service: "messageServer" }); constructor(prefix: string, message: Message) { + console.log("constructor", prefix, message); message.onConnect((msg: any, con: MessageConnect) => { + console.log("onConnect", this.apiFunctionMap, this.apiFunctionMap.size); this.logger.trace("server onConnect", { msg }); if (msg.action.startsWith(prefix)) { return this.connectHandle(msg.action.slice(prefix.length + 1), msg.data, con); @@ -48,6 +50,7 @@ export class Server { }); message.onMessage((msg: { action: string; data: any }, sendResponse, sender) => { + console.log("onConnect", this.apiFunctionMap, this.apiFunctionMap.size); this.logger.trace("server onMessage", { msg: msg as any }); if (msg.action.startsWith(prefix)) { return this.messageHandle(msg.action.slice(prefix.length + 1), msg.data, sendResponse, sender); diff --git a/src/app/service/service_worker/runtime.ts b/src/app/service/service_worker/runtime.ts index fd319d7..1158680 100644 --- a/src/app/service/service_worker/runtime.ts +++ b/src/app/service/service_worker/runtime.ts @@ -15,11 +15,6 @@ import { dealMatches } from "@App/pkg/utils/match"; export class RuntimeService { scriptDAO: ScriptDAO = new ScriptDAO(); - scriptFlag: string = ""; - - // 运行中的页面脚本 - runningPageScript = new Map(); - constructor( private group: Group, private sender: MessageSend, @@ -29,9 +24,14 @@ export class RuntimeService { ) {} async init() { - this.scriptFlag = await Cache.getInstance().getOrSet("scriptInjectFlag", () => { - return Promise.resolve(randomString(16)); - }); + // 启动gm api + const gmApi = new GMApi(this.group, this.sender, this.value); + gmApi.start(); + + this.group.on("stopScript", this.stopScript.bind(this)); + this.group.on("runScript", this.runScript.bind(this)); + this.group.on("pageLoad", this.pageLoad.bind(this)); + // 读取inject.js注入页面 this.registerInjectScript(); // 监听脚本开启 @@ -90,21 +90,20 @@ export class RuntimeService { this.mq.publish("enableScript", { uuid: script.uuid, enable: true }); }); }); - - // 启动gm api - const gmApi = new GMApi(this.group, this.sender, this.value); - gmApi.start(); - - this.group.on("stopScript", this.stopScript.bind(this)); - this.group.on("runScript", this.runScript.bind(this)); - this.group.on("pageLoad", this.pageLoad.bind(this)); } - pageLoad(_, sender: GetSender) { + scriptFlag() { + return Cache.getInstance().getOrSet("scriptInjectFlag", () => { + return Promise.resolve(randomString(16)); + }); + } + + async pageLoad(_, sender: GetSender) { + const scriptFlag = await this.scriptFlag(); const chromeSender = sender.getSender() as chrome.runtime.MessageSender; // 匹配当前页面的脚本 - return Promise.resolve({ flag: this.scriptFlag }); + return Promise.resolve({ flag: scriptFlag }); } // 停止脚本 @@ -127,9 +126,9 @@ export class RuntimeService { if (res.length == 0) { fetch("inject.js") .then((res) => res.text()) - .then((injectJs) => { + .then(async (injectJs) => { // 替换ScriptFlag - const code = `(function (ScriptFlag) {\n${injectJs}\n})('${this.scriptFlag}')`; + const code = `(function (ScriptFlag) {\n${injectJs}\n})('${await this.scriptFlag()}')`; chrome.userScripts.register([ { id: "scriptcat-inject", @@ -167,8 +166,6 @@ export class RuntimeService { scriptRes.code = compileScriptCode(scriptRes); scriptRes.code = compileInjectScript(scriptRes); - this.runningPageScript.set(scriptRes.uuid, scriptRes); - matches.push(...(script.metadata["include"] || [])); const registerScript: chrome.userScripts.RegisteredUserScript = { id: scriptRes.uuid, diff --git a/src/app/service/service_worker/script.ts b/src/app/service/service_worker/script.ts index 37d167f..38fa16b 100644 --- a/src/app/service/service_worker/script.ts +++ b/src/app/service/service_worker/script.ts @@ -279,7 +279,7 @@ export class ScriptService { return Promise.resolve(ret); } - async init() { + init() { this.listenerScriptInstall(); this.group.on("getInstallInfo", this.getInstallInfo); diff --git a/src/pkg/utils/match.test.ts b/src/pkg/utils/match.test.ts index cc96be2..fd09332 100644 --- a/src/pkg/utils/match.test.ts +++ b/src/pkg/utils/match.test.ts @@ -1,11 +1,10 @@ -import { describe, it } from "vitest"; -import { dealMatches, parseURL } from "./match"; +import { describe, expect, it } from "vitest"; +import { dealMatches } from "./match"; // https://developer.chrome.com/docs/extensions/mv3/match_patterns/ describe("dealMatches", () => { it("*://link.17173.com*", () => { - const url = parseURL("*://link.17173.com*"); const matches = dealMatches(["*://link.17173.com*"]); - console.log(url, matches); + expect(matches).toEqual(["*://link.17173.com/*"]); }); }); diff --git a/src/pkg/utils/match.ts b/src/pkg/utils/match.ts index 34dea1f..2f03c68 100644 --- a/src/pkg/utils/match.ts +++ b/src/pkg/utils/match.ts @@ -44,8 +44,10 @@ export function dealMatches(matches: string[]) { // 删除开头的*号 url.host = url.host.slice(1); } + } else if (url.host.endsWith("*")) { + url.host = url.host.slice(0, -1); } - result.push(`${url.scheme}://${url.host}${url.path}` + (url.search ? "?" + url.search : "")); + result.push(`${url.scheme}://${url.host}/${url.path}` + (url.search ? "?" + url.search : "")); } } return result;