【问题标题】:Chrome Extension Message Passing [duplicate]Chrome扩展消息传递[重复]
【发布时间】:2014-04-04 21:56:27
【问题描述】:

我有一个正在发送登录消息的 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)

    )

我做错了吗?

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension coffeescript


    【解决方案1】:

    参见documentation for onMessage 了解sendResponse

    当事件侦听器返回时,此函数无效,除非您从事件侦听器返回 true 以指示您希望异步发送响应(这将保持消息通道对另一端开放,直到调用 sendResponse)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-01
      • 2021-07-27
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      相关资源
      最近更新 更多