From 86746dc2a3306621be254bb78541ed8c612fb0d0 Mon Sep 17 00:00:00 2001 From: IndieKKY Date: Sun, 6 Oct 2024 13:50:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/messaging/layer2/ExtensionMessaging.ts | 8 ++++---- src/messaging/layer2/InjectMessaging.ts | 8 ++++---- src/messaging/layer2/useMessagingService.ts | 12 ++++++------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/messaging/layer2/ExtensionMessaging.ts b/src/messaging/layer2/ExtensionMessaging.ts index 7931ba2..164ab5e 100644 --- a/src/messaging/layer2/ExtensionMessaging.ts +++ b/src/messaging/layer2/ExtensionMessaging.ts @@ -5,7 +5,7 @@ type PortContext = { id: string name: string //暂时没什么用 port: chrome.runtime.Port - portMessageHandler: Layer1Protocol + l1protocol: Layer1Protocol ready: boolean tabId?: number // 所属tab @@ -57,7 +57,7 @@ class ExtensionMessaging { const id = crypto.randomUUID() const name = port.name // 创建消息处理器 - const portMessageHandler = new Layer1Protocol(async (req: L2ReqMsg) => { + const l1protocol = new Layer1Protocol(async (req: L2ReqMsg) => { const { tabId } = portContext const method = this.methods?.[req.method] if (method != null) { @@ -84,7 +84,7 @@ class ExtensionMessaging { } }, port) // 创建portContext - const portContext: PortContext = { id, name, port, portMessageHandler, ready: false } + const portContext: PortContext = { id, name, port, l1protocol, ready: false } this.portIdToPort.set(id, portContext) // 监听断开连接 @@ -110,7 +110,7 @@ class ExtensionMessaging { //check tags if (tags == null || tags.some(tag => portContext.tags?.includes(tag))) { try { - res = await portContext.portMessageHandler.sendMessage({ method, params, from: 'extension' }) + res = await portContext.l1protocol.sendMessage({ method, params, from: 'extension' }) } catch (e) { console.error('send message to port error', portContext.id, e) } diff --git a/src/messaging/layer2/InjectMessaging.ts b/src/messaging/layer2/InjectMessaging.ts index e009e0b..b0bae73 100644 --- a/src/messaging/layer2/InjectMessaging.ts +++ b/src/messaging/layer2/InjectMessaging.ts @@ -3,7 +3,7 @@ import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_HANDSHAKE, MESSAGE_TO_EXTENSIO class InjectMessaging { port?: chrome.runtime.Port - portMessageHandler?: Layer1Protocol + l1protocol?: Layer1Protocol //类实例 methods?: { [key: string]: (params: any, context: MethodContext) => Promise @@ -65,9 +65,9 @@ class InjectMessaging { this.port = chrome.runtime.connect(import.meta.env.VITE_EXTENSION_ID, { name: 'bilibili-inject', }) - this.portMessageHandler = new Layer1Protocol(this.messageHandler, this.port) + this.l1protocol = new Layer1Protocol(this.messageHandler, this.port) //握手 - this.portMessageHandler.sendMessage({ + this.l1protocol.sendMessage({ from: 'inject', method: MESSAGE_TO_EXTENSION_HANDSHAKE, params: { @@ -78,7 +78,7 @@ class InjectMessaging { } sendExtension = async (method: string, params?: any): Promise => { - return await this.portMessageHandler!.sendMessage({ + return await this.l1protocol!.sendMessage({ from: 'inject', method, params: params ?? {}, diff --git a/src/messaging/layer2/useMessagingService.ts b/src/messaging/layer2/useMessagingService.ts index 3d21daa..6afa067 100644 --- a/src/messaging/layer2/useMessagingService.ts +++ b/src/messaging/layer2/useMessagingService.ts @@ -7,13 +7,13 @@ const debug = (...args: any[]) => { console.debug('[App Messaging]', ...args) } -let portMessageHandlerInit: boolean = false -let portMessageHandler: Layer1Protocol | undefined +let l1protocolInit: boolean = false +let l1protocol: Layer1Protocol | undefined // let postInjectMessage: (method: string, params: PostMessagePayload) => Promise | undefined export const msgWaiter = new Waiter>(() => ({ - finished: portMessageHandlerInit, - data: portMessageHandler!, + finished: l1protocolInit, + data: l1protocol!, }), 100, 15000) const useMessagingService = (methods?: { @@ -67,7 +67,7 @@ const useMessagingService = (methods?: { name: 'bilibili-app', }) }, []) - portMessageHandler = useMemo(() => { + l1protocol = useMemo(() => { if (messageHandler && port) { const pmh = new Layer1Protocol(messageHandler, port) @@ -83,7 +83,7 @@ const useMessagingService = (methods?: { tags: [TAG_TARGET_APP], }, }) - portMessageHandlerInit = true + l1protocolInit = true return pmh }