Skip to content

Commit

Permalink
fix sendmessage warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
yaozhiwang committed Apr 28, 2023
1 parent d6996c6 commit 2faf264
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -116,5 +120,6 @@ chrome.runtime.onMessage.addListener(async function (msg, sender) {
}
} else if (msg.name === MessageNames.FixChatGPTWebAppAuthState) {
await chatGPTWebAppClient.fixAuthState()
sendResponse()
}
})
10 changes: 6 additions & 4 deletions src/contents/chatgpt-inpage-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion src/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions src/provider/chatgpt-webapp/requesters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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!)
}
})
Expand Down

0 comments on commit 2faf264

Please sign in to comment.