0

I stuck on message passing in my Google Chrome extension.

I have this popup.js:

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.do == 'popup_refreshMessage')
      jQuery('#message').html(request.message);
  }
); 

jQuery('input[type=button]').click(function(){
  chrome.runtime.sendMessage({
    do: "background_start"
  });
});

and this popup.html:

<html>
<body>
  Message: 
  <div id="message"></div>

  <input type="button" value="Start">
</body>
</html>

Now, I want background page to periodicly refresh message in my popup. So, background.js look like this:

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.do == 'background_start') {
      var periodicFunc = function() {
        request.callback({
          do: "popup_refreshMessage",
          message: Math.random()
        });
        setTimeout(myFunc, 1000);
      }
    periodicFunc();
  }
});

But when I click button, nothing happen and "Uncaught TypeError: Cannot call method 'addListener' of undefined" appear in console.

How can I write background page to periodicly update HTML in my popup? Thank you.

BeardFist
  • 8,031
  • 3
  • 35
  • 40
nanuqcz
  • 1,376
  • 3
  • 16
  • 19
  • 4
    [Using Chrome 25-](http://stackoverflow.com/questions/15718066/chrome-runtime-sendmessage-not-working-as-expected/15718294#15718294)? – Rob W Apr 24 '13 at 17:50
  • Thanks, replacing `chrome.runtime.*` for `chrome.extension.*` works :-) – nanuqcz Apr 24 '13 at 18:06

0 Answers0