You've already forked bilibili-subtitle
fix
This commit is contained in:
@@ -8,15 +8,17 @@ const useMessagingService = () => {
|
||||
const dispatch = useAppDispatch()
|
||||
|
||||
//methods
|
||||
const methods = useMemo(() => ({
|
||||
[MESSAGE_TO_APP_SET_INFOS]: (params: any, from: string, context: MethodContext) => {
|
||||
const methods: {
|
||||
[key: string]: (params: any, context: MethodContext) => boolean
|
||||
} = useMemo(() => ({
|
||||
[MESSAGE_TO_APP_SET_INFOS]: (params: any, context: MethodContext) => {
|
||||
dispatch(setInfos(params.infos))
|
||||
dispatch(setCurInfo(undefined))
|
||||
dispatch(setCurFetched(false))
|
||||
dispatch(setData(undefined))
|
||||
return true
|
||||
},
|
||||
[MESSAGE_TO_APP_SET_VIDEO_INFO]: (params: any, from: string, context: MethodContext) => {
|
||||
[MESSAGE_TO_APP_SET_VIDEO_INFO]: (params: any, context: MethodContext) => {
|
||||
dispatch(setInfos(params.infos))
|
||||
dispatch(setUrl(params.url))
|
||||
dispatch(setTitle(params.title))
|
||||
|
@@ -26,6 +26,7 @@ class ExtensionMessage {
|
||||
const method = this.methods?.[event.method]
|
||||
if (method != null) {
|
||||
method(event.params, {
|
||||
from: event.from,
|
||||
event,
|
||||
sender,
|
||||
}).then(data => sendResponse({
|
||||
|
@@ -25,6 +25,7 @@ class InjectMessage {
|
||||
const method = this.methods?.[event.method]
|
||||
if (method != null) {
|
||||
method(event.params, {
|
||||
from: event.from,
|
||||
event,
|
||||
sender,
|
||||
}).then(data => {
|
||||
@@ -71,6 +72,7 @@ class InjectMessage {
|
||||
this.postMessageToApp = postMessage
|
||||
listenMessage((method, params, sendResponse) => {
|
||||
this.messageHandler({
|
||||
from: 'app',
|
||||
target: MESSAGE_TARGET_INJECT,
|
||||
method,
|
||||
params,
|
||||
@@ -87,6 +89,7 @@ class InjectMessage {
|
||||
|
||||
sendExtension = async <T = any>(method: string, params?: any): Promise<T> => {
|
||||
return await chrome.runtime.sendMessage<MessageData, MessageResult>({
|
||||
from: 'inject',
|
||||
target: MESSAGE_TARGET_EXTENSION,
|
||||
method,
|
||||
params: params ?? {},
|
||||
|
@@ -5,6 +5,7 @@ import { useCallback } from 'react'
|
||||
const useMessage = () => {
|
||||
const sendExtension = useCallback(async <T = any>(method: string, params?: any) => {
|
||||
return await chrome.runtime.sendMessage<MessageData, MessageResult>({
|
||||
from: 'app',
|
||||
target: MESSAGE_TARGET_EXTENSION,
|
||||
method,
|
||||
params: params ?? {},
|
||||
|
@@ -19,12 +19,12 @@ export const injectWaiter = new Waiter<typeof postInjectMessage>(() => ({
|
||||
}), 100, 15000)
|
||||
|
||||
const useMessageService = (methods?: {
|
||||
[key: string]: (params: any, from: string, context: MethodContext) => boolean
|
||||
[key: string]: (params: any, context: MethodContext) => boolean
|
||||
}) => {
|
||||
const messageHandler = useCallback((method: string, params: any, from: string, context: any): boolean => {
|
||||
const messageHandler = useCallback((method: string, params: any, context: MethodContext): boolean => {
|
||||
const handler = methods?.[method]
|
||||
if (handler != null) {
|
||||
return handler(params, from, context)
|
||||
return handler(params, context)
|
||||
}else {
|
||||
debug('unknown message method: ', method)
|
||||
return false
|
||||
@@ -48,7 +48,13 @@ const useMessageService = (methods?: {
|
||||
listenMessage((method, params, sendResponse) => {
|
||||
debug('inject => ', method, params)
|
||||
|
||||
const success = messageHandler(method, params, MESSAGE_TARGET_INJECT, {})
|
||||
const success = messageHandler(method, params, {
|
||||
from: 'inject',
|
||||
event: {
|
||||
method,
|
||||
params,
|
||||
},
|
||||
})
|
||||
sendResponse({
|
||||
success,
|
||||
code: success ? 200 : 500
|
||||
@@ -69,8 +75,10 @@ const useMessageService = (methods?: {
|
||||
// check event target
|
||||
if (!event || event.target !== MESSAGE_TARGET_APP) return
|
||||
|
||||
messageHandler(event.method, event.params, MESSAGE_TARGET_EXTENSION, {
|
||||
sender
|
||||
messageHandler(event.method, event.params, {
|
||||
from: 'extension',
|
||||
event,
|
||||
sender,
|
||||
})
|
||||
}, [messageHandler])
|
||||
|
||||
|
3
src/typings.d.ts
vendored
3
src/typings.d.ts
vendored
@@ -1,5 +1,7 @@
|
||||
type MessageFrom = 'extension' | 'inject' | 'app'
|
||||
|
||||
interface MessageData {
|
||||
from: MessageFrom
|
||||
target: string
|
||||
method: string
|
||||
params?: any
|
||||
@@ -14,6 +16,7 @@ interface MessageResult {
|
||||
}
|
||||
|
||||
interface MethodContext {
|
||||
from: MessageFrom
|
||||
event: any
|
||||
sender?: chrome.runtime.MessageSender | null
|
||||
}
|
||||
|
Reference in New Issue
Block a user