This commit is contained in:
IndieKKY
2024-10-05 21:02:47 +08:00
parent b44a73cd2f
commit 4f03e75b6d
5 changed files with 2 additions and 23 deletions

View File

@@ -71,7 +71,7 @@ class ExtensionMessage {
const id = crypto.randomUUID()
const name = port.name
const portMessageHandler = new Layer1Protocol<MessageData, MessageResult>(async (value: MessageData) => {
// 初始化
// 初始化消息
if (value.method === '_init') {
const type = value.params.type
let tabId = value.params.tabId
@@ -95,6 +95,7 @@ class ExtensionMessage {
} as MessageResult
}
// 处理消息
return handler(value, portContext)
}, port)
const portContext: PortContext = {id, name, port, portMessageHandler, ready: false}

View File

@@ -8,15 +8,11 @@ class InjectMessage {
methods?: {
[key: string]: (params: any, context: MethodContext) => Promise<any>
}
// postMessageToApp?: (method: string, payload: PostMessagePayload) => Promise<PostMessageResponse>
debug = (...args: any[]) => {
console.debug('[Inject Messaging]', ...args)
}
/**
* @param sendResponse No matter what is returned, this method will definitely be called.
*/
messageHandler = async (event: MessageData): Promise<MessageResult> => {
this.debug(`${event.from} => `, JSON.stringify(event))

View File

@@ -30,13 +30,6 @@ class Layer1Protocol<L1Req = any, L1Res = any> {
this.handler = handler
}
// init(type: 'inject' | 'app', tabId?: number) {
// this.port.postMessage({
// type,
// tabId,
// })
// }
startListen() {
// 持久监听 port.onMessage
this.port.onMessage.addListener((msg: ReqMsg<L1Req, L1Res>) => {