You've already forked bilibili-subtitle
优化
This commit is contained in:
@@ -12,8 +12,5 @@ export type L2ResMsg<L2Res = any> = {
|
|||||||
data?: L2Res
|
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_INJECT = 'target:inject'
|
||||||
export const TAG_TARGET_APP = 'target:app'
|
export const TAG_TARGET_APP = 'target:app'
|
||||||
|
@@ -27,7 +27,7 @@ class ExtensionMessaging<M extends ExtensionMessage, AllInjectMessagesType exten
|
|||||||
|
|
||||||
init = (methods: L2MethodHandlers<M, L2ReqMsg, L2ResMsg>) => {
|
init = (methods: L2MethodHandlers<M, L2ReqMsg, L2ResMsg>) => {
|
||||||
const innerMethods: L2MethodHandlers<MessagingExtensionMessages, 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
|
const tags = params.tags
|
||||||
let tabId = params.tabId
|
let tabId = params.tabId
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ class ExtensionMessaging<M extends ExtensionMessage, AllInjectMessagesType exten
|
|||||||
portContext.tags = tags
|
portContext.tags = tags
|
||||||
portContext.ready = true
|
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)
|
return this.broadcastMessageExact([context.tabId!], params.tags, params.method as any, params.params)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
import Layer1Protocol from '../layer1/Layer1Protocol'
|
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> {
|
class InjectMessaging<AllExtensionMessagesType extends ExtensionMessage, AllInjectMessagesType extends InjectMessage, AllAPPMessagesType extends AppMessage> {
|
||||||
port?: chrome.runtime.Port
|
port?: chrome.runtime.Port
|
||||||
@@ -69,7 +69,7 @@ class InjectMessaging<AllExtensionMessagesType extends ExtensionMessage, AllInje
|
|||||||
//握手
|
//握手
|
||||||
this.l1protocol.sendMessage({
|
this.l1protocol.sendMessage({
|
||||||
from: 'inject',
|
from: 'inject',
|
||||||
method: 'HANDSHAKE',
|
method: '_HANDSHAKE',
|
||||||
params: {
|
params: {
|
||||||
tags: [TAG_TARGET_INJECT],
|
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']> => {
|
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],
|
tags: [TAG_TARGET_APP],
|
||||||
method,
|
method,
|
||||||
params,
|
params,
|
||||||
|
@@ -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']> => {
|
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],
|
tags: [TAG_TARGET_INJECT],
|
||||||
method,
|
method,
|
||||||
params: params ?? {},
|
params: params ?? {},
|
||||||
|
@@ -77,7 +77,7 @@ const useMessagingService = <AllAPPMessagesType extends AppMessage>(methods?: {
|
|||||||
// 初始化
|
// 初始化
|
||||||
pmh.sendMessage({
|
pmh.sendMessage({
|
||||||
from: 'app',
|
from: 'app',
|
||||||
method: 'HANDSHAKE',
|
method: '_HANDSHAKE',
|
||||||
params: {
|
params: {
|
||||||
tabId,
|
tabId,
|
||||||
tags: [TAG_TARGET_APP],
|
tags: [TAG_TARGET_APP],
|
||||||
|
4
src/messaging/messaging-typings.d.ts
vendored
4
src/messaging/messaging-typings.d.ts
vendored
@@ -18,11 +18,11 @@ interface AppMessage<T = any> extends Message<T> {
|
|||||||
|
|
||||||
|
|
||||||
interface ExtensionHandshakeMessage extends ExtensionMessage<{ tabId?: number, tags: string[] }> {
|
interface ExtensionHandshakeMessage extends ExtensionMessage<{ tabId?: number, tags: string[] }> {
|
||||||
method: 'HANDSHAKE';
|
method: '_HANDSHAKE';
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ExtensionRouteMessage extends ExtensionMessage<{ tags: string[], method: string, params: any }> {
|
interface ExtensionRouteMessage extends ExtensionMessage<{ tags: string[], method: string, params: any }> {
|
||||||
method: 'ROUTE';
|
method: '_ROUTE';
|
||||||
}
|
}
|
||||||
|
|
||||||
type MessagingExtensionMessages = ExtensionHandshakeMessage | ExtensionRouteMessage
|
type MessagingExtensionMessages = ExtensionHandshakeMessage | ExtensionRouteMessage
|
||||||
|
Reference in New Issue
Block a user