diff --git a/src/messaging/ExtensionMessage.ts b/src/messaging/ExtensionMessage.ts index 3a7d797..76f84f8 100644 --- a/src/messaging/ExtensionMessage.ts +++ b/src/messaging/ExtensionMessage.ts @@ -4,10 +4,12 @@ import Layer1Protocol from './Layer1Protocol' export type PortContext = { id: string name: string - tabId: number - type: 'inject' | 'app' port: chrome.runtime.Port portMessageHandler: Layer1Protocol + ready: boolean + + tabId?: number + type?: 'inject' | 'app' } class ExtensionMessage { @@ -68,42 +70,43 @@ class ExtensionMessage { const id = crypto.randomUUID() const name = port.name + const portMessageHandler = new Layer1Protocol(async (value: MessageData) => { + // 初始化 + if (value.method === '_init') { + const type = value.params.type + let tabId = value.params.tabId - const listener = async (firstMessage: any) => { - console.log('firstMessage', name, firstMessage) - - let tabId = firstMessage.tabId - let type = firstMessage.type - if (tabId == null) { //get current tabId - const tabs = await chrome.tabs.query({ - active: true, - currentWindow: true, - }) - tabId = tabs[0]?.id - console.log('current tabId: ', tabId) - } - if (tabId != null) { - // @ts-ignore - const portContext: PortContext = {id, name, tabId, port, type} - const portMessageHandler = new Layer1Protocol(async (value: MessageData) => { - return handler(value, portContext) - }, port) - portContext.portMessageHandler = portMessageHandler - this.portIdToPort.set(id, portContext) + if (tabId == null) { + const tabs = await chrome.tabs.query({ + active: true, + currentWindow: true, + }) + tabId = tabs[0]?.id + } - // 移除监听 - port.onMessage.removeListener(listener) - // 开始监听 - portMessageHandler.startListen() - } else { - console.log('no tabId>>>', name) - } - } - port.onMessage.addListener(listener) + portContext.tabId = tabId + portContext.type = type + portContext.ready = true + return { + success: true, + code: 200, + } as MessageResult + } + + return handler(value, portContext) + }, port) + const portContext: PortContext = {id, name, port, portMessageHandler, ready: false} + this.portIdToPort.set(id, portContext) + + // 开始监听 + portMessageHandler.startListen() + + // 监听断开连接 port.onDisconnect.addListener(() => { this.portIdToPort.delete(id) + this.debug('onDisconnect', id) }) }) } @@ -113,7 +116,7 @@ class ExtensionMessage { const targetType = target === MESSAGE_TARGET_INJECT ? 'inject' : 'app' let resp: MessageResult | undefined for (const portContext of this.portIdToPort.values()) { - if (tabIds.includes(portContext.tabId)) { + if (tabIds.includes(portContext.tabId!)) { if (targetType === portContext.type) { try { const messageData: MessageData = {target, method, params, from: 'extension'} diff --git a/src/messaging/InjectMessage.ts b/src/messaging/InjectMessage.ts index b85b0f5..6b1ae33 100644 --- a/src/messaging/InjectMessage.ts +++ b/src/messaging/InjectMessage.ts @@ -68,13 +68,18 @@ class InjectMessage { init(methods: { [key: string]: (params: any, context: MethodContext) => Promise }) { + this.methods = methods this.port = chrome.runtime.connect(import.meta.env.VITE_EXTENSION_ID, { name: MESSAGE_TARGET_INJECT, }) this.portMessageHandler = new Layer1Protocol(this.messageHandler, this.port) this.portMessageHandler!.startListen() - this.portMessageHandler!.init('inject') - this.methods = methods + this.portMessageHandler!.sendMessage({ + method: '_init', + params: { + type: 'inject', + }, + } as MessageData) } sendExtension = async (method: string, params?: any): Promise => { diff --git a/src/messaging/Layer1Protocol.ts b/src/messaging/Layer1Protocol.ts index d7c03be..1cc8e45 100644 --- a/src/messaging/Layer1Protocol.ts +++ b/src/messaging/Layer1Protocol.ts @@ -30,12 +30,12 @@ class Layer1Protocol { this.handler = handler } - init(type: 'inject' | 'app', tabId?: number) { - this.port.postMessage({ - type, - tabId, - }) - } + // init(type: 'inject' | 'app', tabId?: number) { + // this.port.postMessage({ + // type, + // tabId, + // }) + // } startListen() { // 持久监听 port.onMessage diff --git a/src/messaging/useMessageService.ts b/src/messaging/useMessageService.ts index 75d94a1..b31fc43 100644 --- a/src/messaging/useMessageService.ts +++ b/src/messaging/useMessageService.ts @@ -77,17 +77,19 @@ const useMessageService = (methods?: { if (messageHandler && port) { const pmh = new Layer1Protocol(messageHandler, port) + pmh.startListen() //get tabId from url params - let tabId = window.location.search.split('tabId=')[1] - if (!tabId) { - pmh.startListen() - pmh.init('app') - portMessageHandlerInit = true - }else { - pmh.startListen() - pmh.init('app', parseInt(tabId)) - portMessageHandlerInit = true - } + let tabIdStr = window.location.search.split('tabId=')[1] + let tabId = tabIdStr ? parseInt(tabIdStr) : undefined + // 初始化 + pmh.sendMessage({ + method: '_init', + params: { + type: 'app', + tabId, + }, + } as MessageData) + portMessageHandlerInit = true return pmh }