This commit is contained in:
IndieKKY
2024-10-06 19:22:01 +08:00
parent d11b517d5e
commit 5ba2b9fd6d
6 changed files with 9 additions and 12 deletions

View File

@@ -12,8 +12,5 @@ export type L2ResMsg<L2Res = any> = {
data?: L2Res
}
export const MESSAGE_TO_EXTENSION_HANDSHAKE = '_handshake'
export const MESSAGE_TO_EXTENSION_ROUTE = '_route'
export const TAG_TARGET_INJECT = 'target:inject'
export const TAG_TARGET_APP = 'target:app'

View File

@@ -27,7 +27,7 @@ class ExtensionMessaging<M extends ExtensionMessage, AllInjectMessagesType exten
init = (methods: L2MethodHandlers<M, L2ReqMsg, L2ResMsg>) => {
const innerMethods: L2MethodHandlers<MessagingExtensionMessages, L2ReqMsg, L2ResMsg> = {
HANDSHAKE: async (params, context: MethodContext, portContext: PortContext<L2ReqMsg, L2ResMsg>) => {
_HANDSHAKE: async (params, context: MethodContext, portContext: PortContext<L2ReqMsg, L2ResMsg>) => {
const tags = params.tags
let tabId = params.tabId
@@ -44,7 +44,7 @@ class ExtensionMessaging<M extends ExtensionMessage, AllInjectMessagesType exten
portContext.tags = tags
portContext.ready = true
},
ROUTE: async (params, context: MethodContext) => {
_ROUTE: async (params, context: MethodContext) => {
return this.broadcastMessageExact([context.tabId!], params.tags, params.method as any, params.params)
},
}

View File

@@ -1,5 +1,5 @@
import Layer1Protocol from '../layer1/Layer1Protocol'
import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_HANDSHAKE, MESSAGE_TO_EXTENSION_ROUTE, TAG_TARGET_APP, TAG_TARGET_INJECT } from '../const'
import { L2ReqMsg, L2ResMsg, TAG_TARGET_APP, TAG_TARGET_INJECT } from '../const'
class InjectMessaging<AllExtensionMessagesType extends ExtensionMessage, AllInjectMessagesType extends InjectMessage, AllAPPMessagesType extends AppMessage> {
port?: chrome.runtime.Port
@@ -69,7 +69,7 @@ class InjectMessaging<AllExtensionMessagesType extends ExtensionMessage, AllInje
//握手
this.l1protocol.sendMessage({
from: 'inject',
method: 'HANDSHAKE',
method: '_HANDSHAKE',
params: {
tags: [TAG_TARGET_INJECT],
},
@@ -91,7 +91,7 @@ class InjectMessaging<AllExtensionMessagesType extends ExtensionMessage, AllInje
}
sendApp = async <M extends AllAPPMessagesType, K extends M['method']>(method: K, params?: Extract<M, { method: K }>['params']): Promise<Extract<M, { method: K }>['return']> => {
return this.sendExtension('ROUTE' as any, {
return this.sendExtension('_ROUTE' as any, {
tags: [TAG_TARGET_APP],
method,
params,

View File

@@ -21,7 +21,7 @@ const useMessaging = <AllExtensionMessagesType extends ExtensionMessage, AllInje
}, [])
const sendInject = useCallback(async <M extends AllInjectMessagesType, K extends M['method']>(method: K, params?: Extract<M, { method: K }>['params']): Promise<Extract<M, { method: K }>['return']> => {
return await sendExtension('ROUTE' as any, {
return await sendExtension('_ROUTE' as any, {
tags: [TAG_TARGET_INJECT],
method,
params: params ?? {},

View File

@@ -77,7 +77,7 @@ const useMessagingService = <AllAPPMessagesType extends AppMessage>(methods?: {
// 初始化
pmh.sendMessage({
from: 'app',
method: 'HANDSHAKE',
method: '_HANDSHAKE',
params: {
tabId,
tags: [TAG_TARGET_APP],

View File

@@ -18,11 +18,11 @@ interface AppMessage<T = any> extends Message<T> {
interface ExtensionHandshakeMessage extends ExtensionMessage<{ tabId?: number, tags: string[] }> {
method: 'HANDSHAKE';
method: '_HANDSHAKE';
}
interface ExtensionRouteMessage extends ExtensionMessage<{ tags: string[], method: string, params: any }> {
method: 'ROUTE';
method: '_ROUTE';
}
type MessagingExtensionMessages = ExtensionHandshakeMessage | ExtensionRouteMessage