为什么来自后台脚本的延迟回调不会触发(chrome扩展名)

时间:2015-04-12 17:46:55

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

我有一个简单的内容脚本,试图向后台脚本询问一些数据。然而,似乎响应需要任何时间才能触发回调。这只是chrome扩展的工作原理吗?我想我可以手动将消息发回标签

这有效

            # CONTENT_SCRIPT
            chrome.runtime.sendMessage { greeting: 'hello' }, (response) ->
              console.log 'RECEIVED MESSAGE FROM BACKROUND'
              console.log JSON.stringify(response.action, null, 3)
              #successfully outputs panda here
              return


            # BACKROUND_SCRIPT
            chrome.runtime.onMessage.addListener (request, sender, sendResponse) ->
              console.log 'RECEIVED MESSAGE FROM CONTENT'

              sendResponse {action: 'panda'}

这不起作用

            # CONTENT_SCRIPT
            chrome.runtime.sendMessage { greeting: 'hello' }, (response) ->
              # this code is never triggered
              console.log 'RECEIVED MESSAGE FROM BACKROUND'
              console.log JSON.stringify(response.action, null, 3)
              return


            # BACKROUND_SCRIPT
            chrome.runtime.onMessage.addListener (request, sender, sendResponse) ->
              console.log 'RECEIVED MESSAGE FROM CONTENT'

              setTimeout (->
                # I see the response in my logs
                console.log 'response'
                sendResponse {action: 'panda'}
              ), 3000

0 个答案:

没有答案