You've already forked bilibili-subtitle
优化
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { msgWaiter } from './useMessagingService'
|
||||
import { useCallback } from 'react'
|
||||
import Layer1Protocol from '../layer1/Layer1Protocol'
|
||||
import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_ROUTE, TAG_TARGET_INJECT } from '../const'
|
||||
import { L2ReqMsg, L2ResMsg, TAG_TARGET_INJECT } from '../const'
|
||||
|
||||
const useMessaging = () => {
|
||||
const sendExtension = useCallback(async <T = any>(method: string, params?: any) => {
|
||||
const sendExtension = useCallback(async <M extends AllExtensionMessages | MessagingExtensionMessages, K extends M['method']>(method: K, params?: Extract<M, { method: K }>['params']): Promise<Extract<M, { method: K }>['return']> => {
|
||||
// wait
|
||||
const pmh = await msgWaiter.wait() as Layer1Protocol<L2ReqMsg, L2ResMsg>
|
||||
// send message
|
||||
@@ -14,13 +14,13 @@ const useMessaging = () => {
|
||||
params: params ?? {},
|
||||
})
|
||||
if (res.code === 200) {
|
||||
return res.data as T
|
||||
return res.data
|
||||
} else {
|
||||
throw new Error(res.msg)
|
||||
}
|
||||
}, [])
|
||||
|
||||
const sendInject = useCallback(async <T = any>(method: string, params?: any): Promise<T> => {
|
||||
const sendInject = useCallback(async <M extends AllInjectMessages, K extends M['method']>(method: K, params?: Extract<M, { method: K }>['params']): Promise<Extract<M, { method: K }>['return']> => {
|
||||
return await sendExtension('ROUTE', {
|
||||
tags: [TAG_TARGET_INJECT],
|
||||
method,
|
||||
|
28
src/messaging/messaging-typings.d.ts
vendored
Normal file
28
src/messaging/messaging-typings.d.ts
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
interface Message<T = any, R = any> {
|
||||
method: string
|
||||
params: T
|
||||
return: R
|
||||
}
|
||||
|
||||
interface ExtensionMessage<T = any> extends Message<T> {
|
||||
}
|
||||
|
||||
interface InjectMessage<T = any> extends Message<T> {
|
||||
}
|
||||
|
||||
interface AppMessage<T = any> extends Message<T> {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
interface ExtensionHandshakeMessage extends ExtensionMessage<{ tabId?: number, tags: string[] }> {
|
||||
method: 'HANDSHAKE';
|
||||
}
|
||||
|
||||
interface ExtensionRouteMessage extends ExtensionMessage<{ tags: string[], method: string, params: any }> {
|
||||
method: 'ROUTE';
|
||||
}
|
||||
|
||||
type MessagingExtensionMessages = ExtensionHandshakeMessage | ExtensionRouteMessage
|
Reference in New Issue
Block a user