gpt4 book ai didi

javascript - Chrome 扩展消息传递

转载 作者:行者123 更新时间:2023-11-28 15:44:18 25 4
gpt4 key购买 nike

我有一个正在发送登录消息的 Chrome 扩展程序:

chrome.runtime.sendMessage data, (response) ->
debugger
if response.api_key
$("body").fadeOut 1000, -> window.close()

else
App.Ui.clearForm()
App.Ui.showErrorMessage()

但是,回调永远不会被命中:

chrome.runtime.onMessage.addListener (request, sender, sendResponse) ->
if request and request.action is "login"

$.ajax(
type: "POST"
url: App.Config.authUrl()
data: request.data
dataType: "json"
).then( (data) ->

App.Storage.saveSession(data.user)
sendResponse(data.user)

, (data) ->

sendResponse(data)

)

我做错了什么吗?

最佳答案

参见documentation for onMessage关于sendResponse:

This function becomes invalid when the event listener returns, unless you return true from the event listener to indicate you wish to send a response asynchronously (this will keep the message channel open to the other end until sendResponse is called).

因此,要修复您的代码,您需要在异步调用后在监听器中返回 true

关于javascript - Chrome 扩展消息传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22864520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com