运行注册脚本
Some checks failed
test / Run tests (push) Failing after 3s
build / Build (push) Failing after 5s

This commit is contained in:
2025-03-30 19:16:44 +08:00
parent 57bef5a023
commit 2c62566696
7 changed files with 87 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import GMApi from "./gm_api";
import { subscribeScriptEnable } from "../queue";
import { ScriptService } from "./script";
import { runScript, stopScript } from "../offscreen/client";
import { dealMatches } from "./utils";
export class RuntimeService {
scriptDAO: ScriptDAO = new ScriptDAO();
@ -81,7 +82,34 @@ export class RuntimeService {
registryPageScript(script: ScriptAndCode) {
console.log(script);
const matches = script.metadata["match"];
if (!matches) {
return;
}
matches.push(...(script.metadata["include"] || []));
const registerScript: chrome.userScripts.RegisteredUserScript = {
id: script.uuid,
js: [{ code: script.code }],
matches: dealMatches(matches),
world: "MAIN",
};
if (!script.metadata["noframes"]) {
registerScript.allFrames = true;
}
if (script.metadata["exclude-match"]) {
const excludeMatches = script.metadata["exclude-match"];
excludeMatches.push(...(script.metadata["exclude"] || []));
registerScript.excludeMatches = dealMatches(excludeMatches);
}
if (script.metadata["run-at"]) {
registerScript.runAt = script.metadata["run-at"][0] as chrome.userScripts.RunAt;
}
chrome.userScripts.register([registerScript]);
}
unregistryPageScript(script: Script) {}
unregistryPageScript(script: Script) {
chrome.userScripts.unregister({
ids: [script.uuid],
});
}
}

View File

@ -4,3 +4,8 @@ export function isExtensionRequest(details: chrome.webRequest.ResourceRequest &
(details.originUrl && details.originUrl.startsWith(chrome.runtime.getURL("")))
);
}
// 处理油猴的match和include为chrome的matches
export function dealMatches(matches: string[]) {
return matches;
}