重新梳理Inject/Content的逻辑

This commit is contained in:
xu_yanfeng
2024-12-27 14:14:38 +08:00
parent b29e39a612
commit ae59ee5a10
17 changed files with 872 additions and 825 deletions

View File

@@ -40,12 +40,13 @@ chrome.runtime.onConnect.addListener((port: chrome.runtime.Port) => {
}
});
chrome.runtime.onMessage.addListener((request: PluginEvent, sender: any, sendResponse: any) => {
const event = PluginEvent.create(request);
const tabID = sender.tab.id;
const portMan: PortMan | undefined = portMgr.findPort(tabID);
if (portMan) {
if (PluginEvent.check(request, Page.Content, Page.Background)) {
if (event.check(Page.Content, Page.Background)) {
// 监听来自content.js发来的事件将消息转发到devtools
PluginEvent.reset(request, Page.Background, Page.Devtools);
event.reset(Page.Background, Page.Devtools);
console.log(`%c[Message]url:${sender.url}]\n${JSON.stringify(request)}`, "color:green");
portMgr.sendDevtoolMsg(request);
}

View File

@@ -14,13 +14,13 @@ export class PortDevtools extends PortMan {
portMgr.useFrame(data.data);
} else {
// 从devtools过来的消息统一派发到Content中
if (PluginEvent.check(data, Page.Devtools, Page.Background)) {
if (data.check(Page.Devtools, Page.Background)) {
if (data.msg === Msg.TreeInfo) {
if (portMgr.isCurrentFrme(data.data)) {
console.log(`frameID[${data.data}]不一致`);
}
}
PluginEvent.reset(data, Page.Background, Page.Content);
data.reset(Page.Background, Page.Content);
const port = portMgr.getCurrentUseContent();
if (!port) {
console.warn(`not find andy port`);

View File

@@ -31,12 +31,14 @@ export abstract class PortMan {
this.title = tab.title;
this.terminal = new Terminal(`Port-${this.name}`);
port.onMessage.addListener((data: any, port: chrome.runtime.Port) => {
console.log(... this.terminal.message(JSON.stringify(data)));
const event = PluginEvent.create(data);
console.log(... this.terminal.chunkMessage(event.toChunk()));
// 如果多个页面都监听 onMessage 事件,对于某一次事件只有第一次调用 sendResponse() 能成功发出回应,所有其他回应将被忽略。
// port.postMessage(data);
const cls = PluginEvent.create(data);
if (this.onMessage) {
this.onMessage(cls);
if (event.valid && this.onMessage) {
this.onMessage(event);
} else {
console.log(... this.terminal.log(JSON.stringify(data)));
}
});
port.onDisconnect.addListener((port: chrome.runtime.Port) => {