运行注册脚本
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

@ -37,9 +37,9 @@ export default class LoggerCore {
this.writer = config.writer;
this.level = config.level || this.level;
this.labels = config.labels || {};
// 获取日志debug等级, 如果是开发环境, 则默认为trace
// 获取日志debug等级, 如果是开发环境, 则默认为debug
if (process.env.NODE_ENV === "development") {
this.debug = "trace";
this.debug = "debug";
}
if (!LoggerCore.instance) {
LoggerCore.instance = this;

View File

@ -1,16 +1,16 @@
import { WindowMessage } from "@Packages/message/window_message";
import { LogLabel, LogLevel, Writer } from "./core";
import { MessageSend } from "@Packages/message/server";
// 通过通讯机制写入日志
export default class MessageWriter implements Writer {
connect: WindowMessage;
send: MessageSend;
constructor(connect: WindowMessage) {
this.connect = connect;
constructor(connect: MessageSend) {
this.send = connect;
}
write(level: LogLevel, message: string, label: LogLabel): void {
this.connect.sendMessage({
this.send.sendMessage({
action: "logger",
data: {
id: 0,

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;
}

22
src/content.ts Normal file
View File

@ -0,0 +1,22 @@
import LoggerCore from "./app/logger/core";
import MessageWriter from "./app/logger/message_writer";
import { SandboxManager } from "./app/service/sandbox";
import { ExtensionMessageSend } from "@Packages/message/extension_message";
function main() {
// 建立与service_worker页面的连接
const msg = new ExtensionMessageSend();
// 初始化日志组件
const loggerCore = new LoggerCore({
writer: new MessageWriter(msg),
labels: { env: "sandbox" },
});
loggerCore.logger().debug("offscreen start");
// 初始化管理器
const manager = new SandboxManager(msg);
manager.initManager();
}
main();

23
src/injected.ts Normal file
View File

@ -0,0 +1,23 @@
import LoggerCore from "./app/logger/core";
import MessageWriter from "./app/logger/message_writer";
import MessageContent from "./app/message/content";
import InjectRuntime from "./runtime/content/inject";
// 通过flag与content建立通讯,这个ScriptFlag是后端注入时候生成的
// eslint-disable-next-line no-undef
const flag = ScriptFlag;
const message = new MessageContent(flag, false);
// 加载logger组件
const logger = new LoggerCore({
debug: process.env.NODE_ENV === "development",
writer: new MessageWriter(message),
labels: { env: "inject", href: window.location.href },
});
message.setHandler("pageLoad", (_action, data) => {
logger.logger().debug("inject start");
const runtime = new InjectRuntime(message, data.scripts, flag);
runtime.start();
});

View File

@ -28,6 +28,7 @@
"scripting",
"activeTab",
"webRequest",
"userScripts",
"declarativeNetRequest"
],
"host_permissions": [