This commit is contained in:
IndieKKY
2024-10-04 15:31:51 +08:00
parent 6ffb9073ab
commit 564f032e3a
6 changed files with 27 additions and 9 deletions

View File

@@ -8,15 +8,17 @@ const useMessagingService = () => {
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
//methods //methods
const methods = useMemo(() => ({ const methods: {
[MESSAGE_TO_APP_SET_INFOS]: (params: any, from: string, context: MethodContext) => { [key: string]: (params: any, context: MethodContext) => boolean
} = useMemo(() => ({
[MESSAGE_TO_APP_SET_INFOS]: (params: any, context: MethodContext) => {
dispatch(setInfos(params.infos)) dispatch(setInfos(params.infos))
dispatch(setCurInfo(undefined)) dispatch(setCurInfo(undefined))
dispatch(setCurFetched(false)) dispatch(setCurFetched(false))
dispatch(setData(undefined)) dispatch(setData(undefined))
return true 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(setInfos(params.infos))
dispatch(setUrl(params.url)) dispatch(setUrl(params.url))
dispatch(setTitle(params.title)) dispatch(setTitle(params.title))

View File

@@ -26,6 +26,7 @@ class ExtensionMessage {
const method = this.methods?.[event.method] const method = this.methods?.[event.method]
if (method != null) { if (method != null) {
method(event.params, { method(event.params, {
from: event.from,
event, event,
sender, sender,
}).then(data => sendResponse({ }).then(data => sendResponse({

View File

@@ -25,6 +25,7 @@ class InjectMessage {
const method = this.methods?.[event.method] const method = this.methods?.[event.method]
if (method != null) { if (method != null) {
method(event.params, { method(event.params, {
from: event.from,
event, event,
sender, sender,
}).then(data => { }).then(data => {
@@ -71,6 +72,7 @@ class InjectMessage {
this.postMessageToApp = postMessage this.postMessageToApp = postMessage
listenMessage((method, params, sendResponse) => { listenMessage((method, params, sendResponse) => {
this.messageHandler({ this.messageHandler({
from: 'app',
target: MESSAGE_TARGET_INJECT, target: MESSAGE_TARGET_INJECT,
method, method,
params, params,
@@ -87,6 +89,7 @@ class InjectMessage {
sendExtension = async <T = any>(method: string, params?: any): Promise<T> => { sendExtension = async <T = any>(method: string, params?: any): Promise<T> => {
return await chrome.runtime.sendMessage<MessageData, MessageResult>({ return await chrome.runtime.sendMessage<MessageData, MessageResult>({
from: 'inject',
target: MESSAGE_TARGET_EXTENSION, target: MESSAGE_TARGET_EXTENSION,
method, method,
params: params ?? {}, params: params ?? {},

View File

@@ -5,6 +5,7 @@ import { useCallback } from 'react'
const useMessage = () => { const useMessage = () => {
const sendExtension = useCallback(async <T = any>(method: string, params?: any) => { const sendExtension = useCallback(async <T = any>(method: string, params?: any) => {
return await chrome.runtime.sendMessage<MessageData, MessageResult>({ return await chrome.runtime.sendMessage<MessageData, MessageResult>({
from: 'app',
target: MESSAGE_TARGET_EXTENSION, target: MESSAGE_TARGET_EXTENSION,
method, method,
params: params ?? {}, params: params ?? {},

View File

@@ -19,12 +19,12 @@ export const injectWaiter = new Waiter<typeof postInjectMessage>(() => ({
}), 100, 15000) }), 100, 15000)
const useMessageService = (methods?: { 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] const handler = methods?.[method]
if (handler != null) { if (handler != null) {
return handler(params, from, context) return handler(params, context)
}else { }else {
debug('unknown message method: ', method) debug('unknown message method: ', method)
return false return false
@@ -48,7 +48,13 @@ const useMessageService = (methods?: {
listenMessage((method, params, sendResponse) => { listenMessage((method, params, sendResponse) => {
debug('inject => ', method, params) debug('inject => ', method, params)
const success = messageHandler(method, params, MESSAGE_TARGET_INJECT, {}) const success = messageHandler(method, params, {
from: 'inject',
event: {
method,
params,
},
})
sendResponse({ sendResponse({
success, success,
code: success ? 200 : 500 code: success ? 200 : 500
@@ -69,8 +75,10 @@ const useMessageService = (methods?: {
// check event target // check event target
if (!event || event.target !== MESSAGE_TARGET_APP) return if (!event || event.target !== MESSAGE_TARGET_APP) return
messageHandler(event.method, event.params, MESSAGE_TARGET_EXTENSION, { messageHandler(event.method, event.params, {
sender from: 'extension',
event,
sender,
}) })
}, [messageHandler]) }, [messageHandler])

3
src/typings.d.ts vendored
View File

@@ -1,5 +1,7 @@
type MessageFrom = 'extension' | 'inject' | 'app'
interface MessageData { interface MessageData {
from: MessageFrom
target: string target: string
method: string method: string
params?: any params?: any
@@ -14,6 +16,7 @@ interface MessageResult {
} }
interface MethodContext { interface MethodContext {
from: MessageFrom
event: any event: any
sender?: chrome.runtime.MessageSender | null sender?: chrome.runtime.MessageSender | null
} }