0

I have a simple content script that is trying to ask the background script for some data. However it seems if the response takes any time at all the callback won't be triggered. Is this just how chrome extensions work? I suppose I could send a message back to the tab manually

This works

            # 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'}

This does not work

            # 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
Jason
  • 71
  • 5
  • Did some more research and found the answer http://stackoverflow.com/questions/20077487/chrome-extension-message-passing-response-not-sent – Jason Apr 12 '15 at 17:52
  • You beat my duplicate flag by mere seconds. – Xan Apr 12 '15 at 17:53

0 Answers0