调整
This commit is contained in:
parent
20124be0e4
commit
21899f0040
@ -39,7 +39,9 @@ export class Server {
|
|||||||
private logger = LoggerCore.getInstance().logger({ service: "messageServer" });
|
private logger = LoggerCore.getInstance().logger({ service: "messageServer" });
|
||||||
|
|
||||||
constructor(prefix: string, message: Message) {
|
constructor(prefix: string, message: Message) {
|
||||||
|
console.log("constructor", prefix, message);
|
||||||
message.onConnect((msg: any, con: MessageConnect) => {
|
message.onConnect((msg: any, con: MessageConnect) => {
|
||||||
|
console.log("onConnect", this.apiFunctionMap, this.apiFunctionMap.size);
|
||||||
this.logger.trace("server onConnect", { msg });
|
this.logger.trace("server onConnect", { msg });
|
||||||
if (msg.action.startsWith(prefix)) {
|
if (msg.action.startsWith(prefix)) {
|
||||||
return this.connectHandle(msg.action.slice(prefix.length + 1), msg.data, con);
|
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) => {
|
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 });
|
this.logger.trace("server onMessage", { msg: msg as any });
|
||||||
if (msg.action.startsWith(prefix)) {
|
if (msg.action.startsWith(prefix)) {
|
||||||
return this.messageHandle(msg.action.slice(prefix.length + 1), msg.data, sendResponse, sender);
|
return this.messageHandle(msg.action.slice(prefix.length + 1), msg.data, sendResponse, sender);
|
||||||
|
@ -15,11 +15,6 @@ import { dealMatches } from "@App/pkg/utils/match";
|
|||||||
export class RuntimeService {
|
export class RuntimeService {
|
||||||
scriptDAO: ScriptDAO = new ScriptDAO();
|
scriptDAO: ScriptDAO = new ScriptDAO();
|
||||||
|
|
||||||
scriptFlag: string = "";
|
|
||||||
|
|
||||||
// 运行中的页面脚本
|
|
||||||
runningPageScript = new Map<string, ScriptRunResouce>();
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private group: Group,
|
private group: Group,
|
||||||
private sender: MessageSend,
|
private sender: MessageSend,
|
||||||
@ -29,9 +24,14 @@ export class RuntimeService {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
this.scriptFlag = await Cache.getInstance().getOrSet("scriptInjectFlag", () => {
|
// 启动gm api
|
||||||
return Promise.resolve(randomString(16));
|
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注入页面
|
// 读取inject.js注入页面
|
||||||
this.registerInjectScript();
|
this.registerInjectScript();
|
||||||
// 监听脚本开启
|
// 监听脚本开启
|
||||||
@ -90,21 +90,20 @@ export class RuntimeService {
|
|||||||
this.mq.publish("enableScript", { uuid: script.uuid, enable: true });
|
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;
|
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) {
|
if (res.length == 0) {
|
||||||
fetch("inject.js")
|
fetch("inject.js")
|
||||||
.then((res) => res.text())
|
.then((res) => res.text())
|
||||||
.then((injectJs) => {
|
.then(async (injectJs) => {
|
||||||
// 替换ScriptFlag
|
// 替换ScriptFlag
|
||||||
const code = `(function (ScriptFlag) {\n${injectJs}\n})('${this.scriptFlag}')`;
|
const code = `(function (ScriptFlag) {\n${injectJs}\n})('${await this.scriptFlag()}')`;
|
||||||
chrome.userScripts.register([
|
chrome.userScripts.register([
|
||||||
{
|
{
|
||||||
id: "scriptcat-inject",
|
id: "scriptcat-inject",
|
||||||
@ -167,8 +166,6 @@ export class RuntimeService {
|
|||||||
scriptRes.code = compileScriptCode(scriptRes);
|
scriptRes.code = compileScriptCode(scriptRes);
|
||||||
scriptRes.code = compileInjectScript(scriptRes);
|
scriptRes.code = compileInjectScript(scriptRes);
|
||||||
|
|
||||||
this.runningPageScript.set(scriptRes.uuid, scriptRes);
|
|
||||||
|
|
||||||
matches.push(...(script.metadata["include"] || []));
|
matches.push(...(script.metadata["include"] || []));
|
||||||
const registerScript: chrome.userScripts.RegisteredUserScript = {
|
const registerScript: chrome.userScripts.RegisteredUserScript = {
|
||||||
id: scriptRes.uuid,
|
id: scriptRes.uuid,
|
||||||
|
@ -279,7 +279,7 @@ export class ScriptService {
|
|||||||
return Promise.resolve(ret);
|
return Promise.resolve(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
init() {
|
||||||
this.listenerScriptInstall();
|
this.listenerScriptInstall();
|
||||||
|
|
||||||
this.group.on("getInstallInfo", this.getInstallInfo);
|
this.group.on("getInstallInfo", this.getInstallInfo);
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
import { describe, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { dealMatches, parseURL } from "./match";
|
import { dealMatches } from "./match";
|
||||||
|
|
||||||
// https://developer.chrome.com/docs/extensions/mv3/match_patterns/
|
// https://developer.chrome.com/docs/extensions/mv3/match_patterns/
|
||||||
describe("dealMatches", () => {
|
describe("dealMatches", () => {
|
||||||
it("*://link.17173.com*", () => {
|
it("*://link.17173.com*", () => {
|
||||||
const url = parseURL("*://link.17173.com*");
|
|
||||||
const matches = dealMatches(["*://link.17173.com*"]);
|
const matches = dealMatches(["*://link.17173.com*"]);
|
||||||
console.log(url, matches);
|
expect(matches).toEqual(["*://link.17173.com/*"]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -44,8 +44,10 @@ export function dealMatches(matches: string[]) {
|
|||||||
// 删除开头的*号
|
// 删除开头的*号
|
||||||
url.host = url.host.slice(1);
|
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;
|
return result;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user