运行注册脚本
Some checks failed
test / Run tests (push) Failing after 3s
build / Build (push) Failing after 5s
Some checks failed
test / Run tests (push) Failing after 3s
build / Build (push) Failing after 5s
This commit is contained in:
parent
57bef5a023
commit
2c62566696
@ -37,9 +37,9 @@ export default class LoggerCore {
|
|||||||
this.writer = config.writer;
|
this.writer = config.writer;
|
||||||
this.level = config.level || this.level;
|
this.level = config.level || this.level;
|
||||||
this.labels = config.labels || {};
|
this.labels = config.labels || {};
|
||||||
// 获取日志debug等级, 如果是开发环境, 则默认为trace
|
// 获取日志debug等级, 如果是开发环境, 则默认为debug
|
||||||
if (process.env.NODE_ENV === "development") {
|
if (process.env.NODE_ENV === "development") {
|
||||||
this.debug = "trace";
|
this.debug = "debug";
|
||||||
}
|
}
|
||||||
if (!LoggerCore.instance) {
|
if (!LoggerCore.instance) {
|
||||||
LoggerCore.instance = this;
|
LoggerCore.instance = this;
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
import { WindowMessage } from "@Packages/message/window_message";
|
|
||||||
import { LogLabel, LogLevel, Writer } from "./core";
|
import { LogLabel, LogLevel, Writer } from "./core";
|
||||||
|
import { MessageSend } from "@Packages/message/server";
|
||||||
|
|
||||||
// 通过通讯机制写入日志
|
// 通过通讯机制写入日志
|
||||||
export default class MessageWriter implements Writer {
|
export default class MessageWriter implements Writer {
|
||||||
connect: WindowMessage;
|
send: MessageSend;
|
||||||
|
|
||||||
constructor(connect: WindowMessage) {
|
constructor(connect: MessageSend) {
|
||||||
this.connect = connect;
|
this.send = connect;
|
||||||
}
|
}
|
||||||
|
|
||||||
write(level: LogLevel, message: string, label: LogLabel): void {
|
write(level: LogLevel, message: string, label: LogLabel): void {
|
||||||
this.connect.sendMessage({
|
this.send.sendMessage({
|
||||||
action: "logger",
|
action: "logger",
|
||||||
data: {
|
data: {
|
||||||
id: 0,
|
id: 0,
|
||||||
|
@ -6,6 +6,7 @@ import GMApi from "./gm_api";
|
|||||||
import { subscribeScriptEnable } from "../queue";
|
import { subscribeScriptEnable } from "../queue";
|
||||||
import { ScriptService } from "./script";
|
import { ScriptService } from "./script";
|
||||||
import { runScript, stopScript } from "../offscreen/client";
|
import { runScript, stopScript } from "../offscreen/client";
|
||||||
|
import { dealMatches } from "./utils";
|
||||||
|
|
||||||
export class RuntimeService {
|
export class RuntimeService {
|
||||||
scriptDAO: ScriptDAO = new ScriptDAO();
|
scriptDAO: ScriptDAO = new ScriptDAO();
|
||||||
@ -81,7 +82,34 @@ export class RuntimeService {
|
|||||||
|
|
||||||
registryPageScript(script: ScriptAndCode) {
|
registryPageScript(script: ScriptAndCode) {
|
||||||
console.log(script);
|
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],
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,3 +4,8 @@ export function isExtensionRequest(details: chrome.webRequest.ResourceRequest &
|
|||||||
(details.originUrl && details.originUrl.startsWith(chrome.runtime.getURL("")))
|
(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
22
src/content.ts
Normal 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
23
src/injected.ts
Normal 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();
|
||||||
|
});
|
@ -28,6 +28,7 @@
|
|||||||
"scripting",
|
"scripting",
|
||||||
"activeTab",
|
"activeTab",
|
||||||
"webRequest",
|
"webRequest",
|
||||||
|
"userScripts",
|
||||||
"declarativeNetRequest"
|
"declarativeNetRequest"
|
||||||
],
|
],
|
||||||
"host_permissions": [
|
"host_permissions": [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user