Chrome Extension: Message Passing

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.

链接地址: http://www.djcxy.com/p/63718.html

上一篇: chrome.tabCapture.capture返回的流未定义

下一篇: Chrome扩展程序:消息传递