diff --git a/src/messaging/ExtensionMessage.ts b/src/messaging/ExtensionMessage.ts index 4bd452c..7e72528 100644 --- a/src/messaging/ExtensionMessage.ts +++ b/src/messaging/ExtensionMessage.ts @@ -108,52 +108,6 @@ class ExtensionMessage { this.portIdToPort.delete(id) }) }) - - /** - * Note: Return true when sending a response asynchronously. - */ - // chrome.runtime.onMessage.addListener((event: MessageData, sender: chrome.runtime.MessageSender, sendResponse: (result: any) => void) => { - // this.debug((sender.tab != null) ? `tab ${sender.tab.url ?? ''} => ` : 'extension => ', event) - - // // check event target - // if (event.target !== MESSAGE_TARGET_EXTENSION) return - - // const method = this.methods?.[event.method] - // if (method != null) { - // method(event.params, { - // from: event.from, - // event, - // sender, - // }).then(data => sendResponse({ - // success: true, - // code: 200, - // data, - // })).catch(err => { - // console.error(err) - // let message - // if (err instanceof Error) { - // message = err.message - // } else if (typeof err === 'string') { - // message = err - // } else { - // message = 'error: ' + JSON.stringify(err) - // } - // sendResponse({ - // success: false, - // code: 500, - // message, - // }) - // }) - // return true - // } else { - // console.error('Unknown method:', event.method) - // sendResponse({ - // success: false, - // code: 501, - // message: 'Unknown method: ' + event.method, - // }) - // } - // }) } broadcastMessageExact = async (tabIds: number[], target: string, method: string, params?: any) => { diff --git a/src/messaging/InjectMessage.ts b/src/messaging/InjectMessage.ts index 7e181ca..fbad9c0 100644 --- a/src/messaging/InjectMessage.ts +++ b/src/messaging/InjectMessage.ts @@ -75,25 +75,6 @@ class InjectMessage { this.portMessageHandler!.startListen() this.portMessageHandler!.init('inject') this.methods = methods - // listen message from app - // startListening({}).then(e => { - // const { postMessage, listenMessage, destroy } = e - // this.postMessageToApp = postMessage - // listenMessage((method, params, sendResponse) => { - // this.messageHandler({ - // from: 'app', - // target: MESSAGE_TARGET_INJECT, - // method, - // params, - // }, null, sendResponse) - // }) - // }).catch(console.error) - - /** - * listen message from extension - * Attention: return true if you need to sendResponse asynchronously - */ - // chrome.runtime.onMessage.addListener(this.messageHandler) } sendExtension = async (method: string, params?: any): Promise => { diff --git a/src/messaging/useMessageService.ts b/src/messaging/useMessageService.ts index dc5da8f..41484e9 100644 --- a/src/messaging/useMessageService.ts +++ b/src/messaging/useMessageService.ts @@ -92,65 +92,6 @@ const useMessageService = (methods?: { return pmh } }, [messageHandler, port]) - - // connect to inject - // useEffect(() => { - // let destroyFunc: (() => void) | undefined - - // const serverObject = { - // server: window.parent, // openedWindow / window.parent / window.opener; - // origin: '*', // target-window's origin or * - // } - // const options = {} - // callServer(serverObject, options).then(e => { - // const { postMessage, listenMessage, destroy } = e - // postInjectMessage = postMessage - // destroyFunc = destroy - - // listenMessage((method, params, sendResponse) => { - // debug('inject => ', method, params) - - // const success = messageHandler(method, params, { - // from: 'inject', - // event: { - // method, - // params, - // }, - // }) - // sendResponse({ - // success, - // code: success ? 200 : 500 - // }) - // }) - - // debug('message ready') - // }).catch(console.error) - - // return () => { - // destroyFunc?.() - // } - // }, [messageHandler]) - - // const extensionMessageCallback = useCallback((event: MessageData, sender: chrome.runtime.MessageSender, sendResponse: (response?: any) => void) => { - // debug((sender.tab != null) ? `tab ${sender.tab.url??''} => ` : 'extension => ', JSON.stringify(event)) - - // // check event target - // if (!event || event.target !== MESSAGE_TARGET_APP) return - - // messageHandler(event.method, event.params, { - // from: 'extension', - // event, - // sender, - // }) - // }, [messageHandler]) - - // // listen for message - // useEffect(() => { - // chrome.runtime.onMessage.addListener(extensionMessageCallback) - // return () => { - // chrome.runtime.onMessage.removeListener(extensionMessageCallback) - // } - // }, [extensionMessageCallback]) } export default useMessageService