添加GM element API

This commit is contained in:
2025-04-11 17:40:25 +08:00
parent a2870eb18e
commit 7ca85801ef
15 changed files with 393 additions and 128 deletions

View File

@ -17,7 +17,7 @@ export class CustomEventMessage implements Message {
EE: EventEmitter = new EventEmitter();
// 关联dom目标
relatedTarget: Map<number, Document> = new Map();
relatedTarget: Map<number, EventTarget> = new Map();
constructor(
protected flag: string,
@ -25,7 +25,7 @@ export class CustomEventMessage implements Message {
) {
window.addEventListener((isContent ? "ct" : "fd") + flag, (event) => {
if (event instanceof MouseEvent) {
this.relatedTarget.set(event.clientX, <Document>event.relatedTarget);
this.relatedTarget.set(event.clientX, event.relatedTarget!);
return;
} else if (event instanceof CustomEvent) {
this.messageHandle(event.detail, new CustomEventPostMessage(this));
@ -82,23 +82,7 @@ export class CustomEventMessage implements Message {
});
}
nativeSend(data: any) {
let detail = data;
// 特殊处理relatedTarget
if (detail.data && typeof detail.data.relatedTarget === "object") {
// 先将relatedTarget转换成id发送过去
const target = detail.data.relatedTarget;
delete detail.data.relatedTarget;
detail.data.relatedTarget = Math.ceil(Math.random() * 1000000);
// 可以使用此种方式交互element
const ev = new MouseEvent((this.isContent ? "fd" : "ct") + this.flag, {
clientX: detail.data.relatedTarget,
relatedTarget: target,
});
window.dispatchEvent(ev);
}
nativeSend(detail: any) {
if (typeof cloneInto !== "undefined") {
try {
LoggerCore.logger().info("nativeSend");
@ -131,6 +115,40 @@ export class CustomEventMessage implements Message {
});
}
// 同步发送消息
// 与content页的消息通讯实际是同步,此方法不需要经过background
// 但是请注意中间不要有promise
syncSendMessage(data: any): any {
const body: WindowMessageBody = {
messageId: uuidv4(),
type: "sendMessage",
data,
};
let ret: any;
const callback = (body: WindowMessageBody) => {
this.EE.removeListener("response:" + body.messageId, callback);
ret = body.data;
};
this.EE.addListener("response:" + body.messageId, callback);
this.nativeSend(body);
return ret;
}
relateId = 0;
sendRelatedTarget(target: EventTarget): number {
// 特殊处理relatedTarget返回id进行关联
// 先将relatedTarget转换成id发送过去
const id = ++this.relateId;
// 可以使用此种方式交互element
const ev = new MouseEvent((this.isContent ? "fd" : "ct") + this.flag, {
clientX: id,
relatedTarget: target,
});
window.dispatchEvent(ev);
return id;
}
getAndDelRelatedTarget(id: number) {
const target = this.relatedTarget.get(id);
this.relatedTarget.delete(id);

View File

@ -1,5 +1,5 @@
import LoggerCore from "@App/app/logger/core";
import { connect } from "./client";
import { connect, sendMessage } from "./client";
export interface Message extends MessageSend {
onConnect(callback: (data: any, con: MessageConnect) => void): void;
@ -20,6 +20,12 @@ export interface MessageConnect {
export type MessageSender = chrome.runtime.MessageSender;
export type ExtMessageSender = {
tabId: number;
frameId?: number;
documentId?: string;
};
export class GetSender {
constructor(private sender: MessageConnect | MessageSender) {}
@ -27,12 +33,22 @@ export class GetSender {
return this.sender as MessageSender;
}
getExtMessageSender(): ExtMessageSender {
const sender = this.sender as MessageSender;
return {
tabId: sender.tab?.id || -1, // -1表示后台脚本
frameId: sender.frameId,
documentId: sender.documentId,
};
}
getConnect(): MessageConnect {
return this.sender as MessageConnect;
}
}
export type ApiFunction = (params: any, con: GetSender) => Promise<any> | void;
export type ApiFunctionSync = (params: any, con: GetSender) => any;
export class Server {
private apiFunctionMap: Map<string, ApiFunction> = new Map();
@ -115,11 +131,24 @@ export class Group {
}
// 转发消息
export function forwardMessage(prefix: string, path: string, from: Server, to: MessageSend, middleware?: ApiFunction) {
from.on(path, async (params, fromCon) => {
export function forwardMessage(
prefix: string,
path: string,
from: Server,
to: MessageSend,
middleware?: ApiFunctionSync
) {
from.on(path, (params, fromCon) => {
if (middleware) {
const resp = await middleware(params, fromCon);
if (resp !== false) {
// 此处是为了处理CustomEventMessage的同步消息情况
const resp = middleware(params, fromCon) as any;
if (resp instanceof Promise) {
return resp.then((data) => {
if (data !== false) {
return data;
}
});
} else if (resp !== false) {
return resp;
}
}
@ -140,7 +169,7 @@ export function forwardMessage(prefix: string, path: string, from: Server, to: M
});
});
} else {
return to.sendMessage({ action: prefix + "/" + path, data: params });
return sendMessage(to, prefix + "/" + path, params);
}
});
}