You've already forked bilibili-subtitle
优化
This commit is contained in:
@@ -5,7 +5,7 @@ type PortContext<L2ReqMsg, L2ResMsg> = {
|
|||||||
id: string
|
id: string
|
||||||
name: string //暂时没什么用
|
name: string //暂时没什么用
|
||||||
port: chrome.runtime.Port
|
port: chrome.runtime.Port
|
||||||
portMessageHandler: Layer1Protocol<L2ReqMsg, L2ResMsg>
|
l1protocol: Layer1Protocol<L2ReqMsg, L2ResMsg>
|
||||||
ready: boolean
|
ready: boolean
|
||||||
|
|
||||||
tabId?: number // 所属tab
|
tabId?: number // 所属tab
|
||||||
@@ -57,7 +57,7 @@ class ExtensionMessaging {
|
|||||||
const id = crypto.randomUUID()
|
const id = crypto.randomUUID()
|
||||||
const name = port.name
|
const name = port.name
|
||||||
// 创建消息处理器
|
// 创建消息处理器
|
||||||
const portMessageHandler = new Layer1Protocol<L2ReqMsg, L2ResMsg>(async (req: L2ReqMsg) => {
|
const l1protocol = new Layer1Protocol<L2ReqMsg, L2ResMsg>(async (req: L2ReqMsg) => {
|
||||||
const { tabId } = portContext
|
const { tabId } = portContext
|
||||||
const method = this.methods?.[req.method]
|
const method = this.methods?.[req.method]
|
||||||
if (method != null) {
|
if (method != null) {
|
||||||
@@ -84,7 +84,7 @@ class ExtensionMessaging {
|
|||||||
}
|
}
|
||||||
}, port)
|
}, port)
|
||||||
// 创建portContext
|
// 创建portContext
|
||||||
const portContext: PortContext<L2ReqMsg, L2ResMsg> = { id, name, port, portMessageHandler, ready: false }
|
const portContext: PortContext<L2ReqMsg, L2ResMsg> = { id, name, port, l1protocol, ready: false }
|
||||||
this.portIdToPort.set(id, portContext)
|
this.portIdToPort.set(id, portContext)
|
||||||
|
|
||||||
// 监听断开连接
|
// 监听断开连接
|
||||||
@@ -110,7 +110,7 @@ class ExtensionMessaging {
|
|||||||
//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 {
|
||||||
res = await portContext.portMessageHandler.sendMessage({ method, params, from: 'extension' })
|
res = await portContext.l1protocol.sendMessage({ method, params, from: 'extension' })
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('send message to port error', portContext.id, e)
|
console.error('send message to port error', portContext.id, e)
|
||||||
}
|
}
|
||||||
|
@@ -3,7 +3,7 @@ import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_HANDSHAKE, MESSAGE_TO_EXTENSIO
|
|||||||
|
|
||||||
class InjectMessaging {
|
class InjectMessaging {
|
||||||
port?: chrome.runtime.Port
|
port?: chrome.runtime.Port
|
||||||
portMessageHandler?: Layer1Protocol<L2ReqMsg, L2ResMsg>
|
l1protocol?: Layer1Protocol<L2ReqMsg, L2ResMsg>
|
||||||
//类实例
|
//类实例
|
||||||
methods?: {
|
methods?: {
|
||||||
[key: string]: (params: any, context: MethodContext) => Promise<L2ResMsg>
|
[key: string]: (params: any, context: MethodContext) => Promise<L2ResMsg>
|
||||||
@@ -65,9 +65,9 @@ class InjectMessaging {
|
|||||||
this.port = chrome.runtime.connect(import.meta.env.VITE_EXTENSION_ID, {
|
this.port = chrome.runtime.connect(import.meta.env.VITE_EXTENSION_ID, {
|
||||||
name: 'bilibili-inject',
|
name: 'bilibili-inject',
|
||||||
})
|
})
|
||||||
this.portMessageHandler = new Layer1Protocol<L2ReqMsg, L2ResMsg>(this.messageHandler, this.port)
|
this.l1protocol = new Layer1Protocol<L2ReqMsg, L2ResMsg>(this.messageHandler, this.port)
|
||||||
//握手
|
//握手
|
||||||
this.portMessageHandler.sendMessage({
|
this.l1protocol.sendMessage({
|
||||||
from: 'inject',
|
from: 'inject',
|
||||||
method: MESSAGE_TO_EXTENSION_HANDSHAKE,
|
method: MESSAGE_TO_EXTENSION_HANDSHAKE,
|
||||||
params: {
|
params: {
|
||||||
@@ -78,7 +78,7 @@ class InjectMessaging {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sendExtension = async <T = any>(method: string, params?: any): Promise<T> => {
|
sendExtension = async <T = any>(method: string, params?: any): Promise<T> => {
|
||||||
return await this.portMessageHandler!.sendMessage({
|
return await this.l1protocol!.sendMessage({
|
||||||
from: 'inject',
|
from: 'inject',
|
||||||
method,
|
method,
|
||||||
params: params ?? {},
|
params: params ?? {},
|
||||||
|
@@ -7,13 +7,13 @@ const debug = (...args: any[]) => {
|
|||||||
console.debug('[App Messaging]', ...args)
|
console.debug('[App Messaging]', ...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
let portMessageHandlerInit: boolean = false
|
let l1protocolInit: boolean = false
|
||||||
let portMessageHandler: Layer1Protocol<L2ReqMsg, L2ResMsg> | undefined
|
let l1protocol: Layer1Protocol<L2ReqMsg, L2ResMsg> | undefined
|
||||||
// let postInjectMessage: (method: string, params: PostMessagePayload) => Promise<PostMessageResponse> | undefined
|
// let postInjectMessage: (method: string, params: PostMessagePayload) => Promise<PostMessageResponse> | undefined
|
||||||
|
|
||||||
export const msgWaiter = new Waiter<Layer1Protocol<L2ReqMsg, L2ResMsg>>(() => ({
|
export const msgWaiter = new Waiter<Layer1Protocol<L2ReqMsg, L2ResMsg>>(() => ({
|
||||||
finished: portMessageHandlerInit,
|
finished: l1protocolInit,
|
||||||
data: portMessageHandler!,
|
data: l1protocol!,
|
||||||
}), 100, 15000)
|
}), 100, 15000)
|
||||||
|
|
||||||
const useMessagingService = (methods?: {
|
const useMessagingService = (methods?: {
|
||||||
@@ -67,7 +67,7 @@ const useMessagingService = (methods?: {
|
|||||||
name: 'bilibili-app',
|
name: 'bilibili-app',
|
||||||
})
|
})
|
||||||
}, [])
|
}, [])
|
||||||
portMessageHandler = useMemo(() => {
|
l1protocol = useMemo(() => {
|
||||||
if (messageHandler && port) {
|
if (messageHandler && port) {
|
||||||
const pmh = new Layer1Protocol<L2ReqMsg, L2ResMsg>(messageHandler, port)
|
const pmh = new Layer1Protocol<L2ReqMsg, L2ResMsg>(messageHandler, port)
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ const useMessagingService = (methods?: {
|
|||||||
tags: [TAG_TARGET_APP],
|
tags: [TAG_TARGET_APP],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
portMessageHandlerInit = true
|
l1protocolInit = true
|
||||||
|
|
||||||
return pmh
|
return pmh
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user