From 2faf2648a11998b857634412c0943552d19f5087 Mon Sep 17 00:00:00 2001 From: Yaozhi Wang Date: Sat, 29 Apr 2023 00:41:54 +0800 Subject: [PATCH] fix sendmessage warnings --- src/background/index.ts | 7 ++++++- src/contents/chatgpt-inpage-proxy.ts | 10 ++++++---- src/messaging.ts | 3 ++- src/provider/chatgpt-webapp/requesters.ts | 8 ++++++-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/background/index.ts b/src/background/index.ts index 51b41bf..e618b27 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -99,7 +99,11 @@ chrome.runtime.onConnect.addListener((port) => { } }) -chrome.runtime.onMessage.addListener(async function (msg, sender) { +chrome.runtime.onMessage.addListener(async function ( + msg, + sender, + sendResponse +) { if (msg.name == MessageNames.UpdateActionIcon) { const { pageEnabled, hostEnabled } = msg.enabledDetails @@ -116,5 +120,6 @@ chrome.runtime.onMessage.addListener(async function (msg, sender) { } } else if (msg.name === MessageNames.FixChatGPTWebAppAuthState) { await chatGPTWebAppClient.fixAuthState() + sendResponse() } }) diff --git a/src/contents/chatgpt-inpage-proxy.ts b/src/contents/chatgpt-inpage-proxy.ts index 68fcd33..05513fe 100644 --- a/src/contents/chatgpt-inpage-proxy.ts +++ b/src/contents/chatgpt-inpage-proxy.ts @@ -16,11 +16,13 @@ function injectTip() { } async function main() { - chrome.runtime.onMessage.addListener(async (message) => { - if (message === "url") { - return location.href + chrome.runtime.onMessage.addListener( + async (message, sender, sendResponse) => { + if (message === MessageNames.QueryUrl) { + sendResponse(location.href) + } } - }) + ) if ((window as any).__NEXT_DATA__) { await chrome.runtime.sendMessage({ name: MessageNames.ProxyTabReady }) injectTip() diff --git a/src/messaging.ts b/src/messaging.ts index 48b9243..9b9635d 100644 --- a/src/messaging.ts +++ b/src/messaging.ts @@ -6,7 +6,8 @@ export enum MessageNames { ProxyResponseMetadata = "proxy-response-metadata", ProxyResponseBodyChunk = "proxy-response-body-chunk", ProxyTabReady = "proxy-tab-ready", - FixChatGPTWebAppAuthState = "fix-chatgpt-webapp-auth-state" + FixChatGPTWebAppAuthState = "fix-chatgpt-webapp-auth-state", + QueryUrl = "query-url" } export enum PortNames { diff --git a/src/provider/chatgpt-webapp/requesters.ts b/src/provider/chatgpt-webapp/requesters.ts index 2b10909..5f7f7c2 100644 --- a/src/provider/chatgpt-webapp/requesters.ts +++ b/src/provider/chatgpt-webapp/requesters.ts @@ -20,7 +20,9 @@ class ProxyFetchRequester implements Requester { if (tab.url) { return tab.url } - return chrome.tabs.sendMessage(tab.id!, "url").catch(() => undefined) + return chrome.tabs + .sendMessage(tab.id!, MessageNames.QueryUrl) + .catch(() => undefined) }) ) for (let i = 0; i < results.length; i++) { @@ -33,10 +35,12 @@ class ProxyFetchRequester implements Requester { waitForProxyTabReady(onReady: (tab: chrome.tabs.Tab) => void) { chrome.runtime.onMessage.addListener(async function listener( message, - sender + sender, + sendResponse ) { if (message.name === MessageNames.ProxyTabReady) { chrome.runtime.onMessage.removeListener(listener) + sendResponse() onReady(sender.tab!) } })