You've already forked bilibili-subtitle
优化
This commit is contained in:
@@ -21,7 +21,7 @@ import {useAsyncEffect, useInterval} from 'ahooks'
|
|||||||
import {getModelMaxTokens, getWholeText} from '../utils/biz_util'
|
import {getModelMaxTokens, getWholeText} from '../utils/biz_util'
|
||||||
import {MESSAGE_TO_INJECT_GET_SUBTITLE} from '../consts/const'
|
import {MESSAGE_TO_INJECT_GET_SUBTITLE} from '../consts/const'
|
||||||
import useMessage from '../messaging/layer2/useMessage'
|
import useMessage from '../messaging/layer2/useMessage'
|
||||||
import { injectWaiter } from '@/messaging/layer2/useMessageService'
|
import { msgWaiter } from '@/messaging/layer2/useMessageService'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service是单例,类似后端的服务概念
|
* Service是单例,类似后端的服务概念
|
||||||
@@ -95,7 +95,7 @@ const useSubtitleService = () => {
|
|||||||
|
|
||||||
useAsyncEffect(async () => {
|
useAsyncEffect(async () => {
|
||||||
// 等待inject准备好
|
// 等待inject准备好
|
||||||
await injectWaiter.wait()
|
await msgWaiter.wait()
|
||||||
// 初始获取列表
|
// 初始获取列表
|
||||||
sendInject(MESSAGE_TO_INJECT_REFRESH_VIDEO_INFO, {force: true})
|
sendInject(MESSAGE_TO_INJECT_REFRESH_VIDEO_INFO, {force: true})
|
||||||
// 初始获取设置信息
|
// 初始获取设置信息
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
## 消息通信库封装
|
## 消息通信库封装
|
||||||
|
此消息通信库设计为通用的,后面可能会提取到单独的项目中。
|
||||||
|
|
||||||
### 消息端
|
### 消息端
|
||||||
|
|
||||||
|
@@ -13,7 +13,7 @@ export type L2ResMsg<L2Res = any> = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const MESSAGE_TO_EXTENSION_HANDSHAKE = '_handshake'
|
export const MESSAGE_TO_EXTENSION_HANDSHAKE = '_handshake'
|
||||||
export const MESSAGE_TO_EXTENSION_ROUTE_MSG = '_routeMsg'
|
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'
|
||||||
|
@@ -1,33 +1,33 @@
|
|||||||
import Layer1Protocol from '../layer1/Layer1Protocol'
|
import Layer1Protocol from '../layer1/Layer1Protocol'
|
||||||
import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_HANDSHAKE, MESSAGE_TO_EXTENSION_ROUTE_MSG } from '../const'
|
import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_HANDSHAKE, MESSAGE_TO_EXTENSION_ROUTE } from '../const'
|
||||||
|
|
||||||
type PortContext = {
|
type PortContext<L2ReqMsg, L2ResMsg> = {
|
||||||
id: string
|
id: string
|
||||||
name: string //暂时没什么用
|
name: string //暂时没什么用
|
||||||
port: chrome.runtime.Port
|
port: chrome.runtime.Port
|
||||||
portMessageHandler: Layer1Protocol
|
portMessageHandler: Layer1Protocol<L2ReqMsg, L2ResMsg>
|
||||||
ready: boolean
|
ready: boolean
|
||||||
|
|
||||||
tabId?: number // 所属tab
|
tabId?: number // 所属tab
|
||||||
tags?: string[] // 标签,用来筛选消息发送目标
|
tags?: string[] // 标签,用来筛选消息发送目标
|
||||||
}
|
}
|
||||||
|
|
||||||
type L2MethodHandler = (params: any, context: MethodContext, portContext: PortContext) => Promise<any>
|
type L2MethodHandler<L2ReqMsg, L2ResMsg> = (params: any, context: MethodContext, portContext: PortContext<L2ReqMsg, L2ResMsg>) => Promise<any>
|
||||||
type L2MethodHandlers = {
|
type L2MethodHandlers<L2ReqMsg, L2ResMsg> = {
|
||||||
[key: string]: L2MethodHandler
|
[key: string]: L2MethodHandler<L2ReqMsg, L2ResMsg>
|
||||||
}
|
}
|
||||||
|
|
||||||
class ExtensionMessage {
|
class ExtensionMessage {
|
||||||
portIdToPort: Map<string, PortContext> = new Map()
|
portIdToPort: Map<string, PortContext<L2ReqMsg, L2ResMsg>> = new Map()
|
||||||
methods?: L2MethodHandlers
|
methods?: L2MethodHandlers<L2ReqMsg, L2ResMsg>
|
||||||
|
|
||||||
debug = (...args: any[]) => {
|
debug = (...args: any[]) => {
|
||||||
console.debug('[Extension Messaging]', ...args)
|
console.debug('[Extension Messaging]', ...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
init = (methods: L2MethodHandlers) => {
|
init = (methods: L2MethodHandlers<L2ReqMsg, L2ResMsg>) => {
|
||||||
const innerMethods: L2MethodHandlers = {
|
const innerMethods: L2MethodHandlers<L2ReqMsg, L2ResMsg> = {
|
||||||
[MESSAGE_TO_EXTENSION_HANDSHAKE]: async (params: any, context: MethodContext, portContext: PortContext) => {
|
[MESSAGE_TO_EXTENSION_HANDSHAKE]: async (params: any, 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 ExtensionMessage {
|
|||||||
portContext.tags = tags
|
portContext.tags = tags
|
||||||
portContext.ready = true
|
portContext.ready = true
|
||||||
},
|
},
|
||||||
[MESSAGE_TO_EXTENSION_ROUTE_MSG]: async (params: any, context: MethodContext) => {
|
[MESSAGE_TO_EXTENSION_ROUTE]: async (params: any, context: MethodContext) => {
|
||||||
return this.broadcastMessageExact([context.tabId!], params.tags, params.method, params.params)
|
return this.broadcastMessageExact([context.tabId!], params.tags, params.method, params.params)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -84,7 +84,7 @@ class ExtensionMessage {
|
|||||||
}
|
}
|
||||||
}, port)
|
}, port)
|
||||||
// 创建portContext
|
// 创建portContext
|
||||||
const portContext: PortContext = { id, name, port, portMessageHandler, ready: false }
|
const portContext: PortContext<L2ReqMsg, L2ResMsg> = { id, name, port, portMessageHandler, ready: false }
|
||||||
this.portIdToPort.set(id, portContext)
|
this.portIdToPort.set(id, portContext)
|
||||||
|
|
||||||
// 监听断开连接
|
// 监听断开连接
|
||||||
@@ -110,8 +110,7 @@ class ExtensionMessage {
|
|||||||
//check tags
|
//check tags
|
||||||
if (tags == null || tags.some(tag => portContext.tags?.includes(tag))) {
|
if (tags == null || tags.some(tag => portContext.tags?.includes(tag))) {
|
||||||
try {
|
try {
|
||||||
const req: L2ReqMsg = { method, params, from: 'extension' }
|
res = await portContext.portMessageHandler.sendMessage({ method, params, from: 'extension' })
|
||||||
res = await portContext.portMessageHandler.sendMessage(req)
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('send message to port error', portContext.id, e)
|
console.error('send message to port error', portContext.id, e)
|
||||||
}
|
}
|
||||||
|
@@ -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_MSG, TAG_TARGET_APP, TAG_TARGET_INJECT } from '../const'
|
import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_HANDSHAKE, MESSAGE_TO_EXTENSION_ROUTE, TAG_TARGET_APP, TAG_TARGET_INJECT } from '../const'
|
||||||
|
|
||||||
class InjectMessage {
|
class InjectMessage {
|
||||||
port?: chrome.runtime.Port
|
port?: chrome.runtime.Port
|
||||||
@@ -94,7 +94,7 @@ class InjectMessage {
|
|||||||
if (method === 'setVideoInfo') {
|
if (method === 'setVideoInfo') {
|
||||||
console.log('sendApp>>>', method, params)
|
console.log('sendApp>>>', method, params)
|
||||||
}
|
}
|
||||||
return this.sendExtension(MESSAGE_TO_EXTENSION_ROUTE_MSG, {
|
return this.sendExtension(MESSAGE_TO_EXTENSION_ROUTE, {
|
||||||
tags: [TAG_TARGET_APP],
|
tags: [TAG_TARGET_APP],
|
||||||
method,
|
method,
|
||||||
params,
|
params,
|
||||||
|
@@ -1,12 +1,12 @@
|
|||||||
import { injectWaiter } from './useMessageService'
|
import { msgWaiter } from './useMessageService'
|
||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import Layer1Protocol from '../layer1/Layer1Protocol'
|
import Layer1Protocol from '../layer1/Layer1Protocol'
|
||||||
import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_ROUTE_MSG, TAG_TARGET_INJECT } from '../const'
|
import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_ROUTE, TAG_TARGET_INJECT } from '../const'
|
||||||
|
|
||||||
const useMessage = () => {
|
const useMessage = () => {
|
||||||
const sendExtension = useCallback(async <T = any>(method: string, params?: any) => {
|
const sendExtension = useCallback(async <T = any>(method: string, params?: any) => {
|
||||||
// wait
|
// wait
|
||||||
const pmh = await injectWaiter.wait() as Layer1Protocol<L2ReqMsg, L2ResMsg>
|
const pmh = await msgWaiter.wait() as Layer1Protocol<L2ReqMsg, L2ResMsg>
|
||||||
// send message
|
// send message
|
||||||
const res = await pmh.sendMessage({
|
const res = await pmh.sendMessage({
|
||||||
from: 'app',
|
from: 'app',
|
||||||
@@ -21,7 +21,7 @@ const useMessage = () => {
|
|||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const sendInject = useCallback(async <T = any>(method: string, params?: any): Promise<T> => {
|
const sendInject = useCallback(async <T = any>(method: string, params?: any): Promise<T> => {
|
||||||
return await sendExtension(MESSAGE_TO_EXTENSION_ROUTE_MSG, {
|
return await sendExtension(MESSAGE_TO_EXTENSION_ROUTE, {
|
||||||
tags: [TAG_TARGET_INJECT],
|
tags: [TAG_TARGET_INJECT],
|
||||||
method,
|
method,
|
||||||
params: params ?? {},
|
params: params ?? {},
|
||||||
|
@@ -11,9 +11,9 @@ let portMessageHandlerInit: boolean = false
|
|||||||
let portMessageHandler: Layer1Protocol<L2ReqMsg, L2ResMsg> | undefined
|
let portMessageHandler: Layer1Protocol<L2ReqMsg, L2ResMsg> | undefined
|
||||||
// let postInjectMessage: (method: string, params: PostMessagePayload) => Promise<PostMessageResponse> | undefined
|
// let postInjectMessage: (method: string, params: PostMessagePayload) => Promise<PostMessageResponse> | undefined
|
||||||
|
|
||||||
export const injectWaiter = new Waiter<any>(() => ({
|
export const msgWaiter = new Waiter<Layer1Protocol<L2ReqMsg, L2ResMsg>>(() => ({
|
||||||
finished: portMessageHandlerInit,
|
finished: portMessageHandlerInit,
|
||||||
data: portMessageHandler
|
data: portMessageHandler!,
|
||||||
}), 100, 15000)
|
}), 100, 15000)
|
||||||
|
|
||||||
const useMessageService = (methods?: {
|
const useMessageService = (methods?: {
|
||||||
|
Reference in New Issue
Block a user