消息通讯

This commit is contained in:
2024-12-27 17:48:15 +08:00
parent eeb343bc5f
commit 78152222f3
29 changed files with 4051 additions and 252 deletions

View File

@ -5,18 +5,22 @@ export class Server {
constructor(private env: string) {
chrome.runtime.onConnect.addListener((port) => {
const handler = (msg: { action: string }) => {
const handler = (msg: { action: string; data: any }) => {
port.onMessage.removeListener(handler);
this.connectHandle(msg.action, msg, port);
this.connectHandle(msg.action, msg.data, port);
};
port.onMessage.addListener(handler);
});
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
this.messageHandle(msg.action, msg, sender, sendResponse);
this.messageHandle(msg.action, msg.data, sender, sendResponse);
});
}
group(name: string) {
return new Group(this, name);
}
on(name: string, func: ApiFunction) {
this.apiFunctionMap.set(name, func);
}
@ -45,3 +49,22 @@ export class Server {
}
}
}
export class Group {
constructor(
private server: Server,
private name: string
) {
if (!name.endsWith("/")) {
this.name += "/";
}
}
group(name: string) {
return new Group(this.server, `${this.name}${name}`);
}
on(name: string, func: ApiFunction) {
this.server.on(`${this.name}${name}`, func);
}
}