chrome扩展,popup.html和background.html之间的消息传递

时间:2012-04-12 20:52:57

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

我的Chrome扩展程序有一个奇怪的问题。 我希望我的弹出页面充满数据库的东西。所以在background.html中我创建了与DB的连接。 DB答案应该显示在弹出窗口中,所以我决定使用chrome.extension.sendRequest方法。 (希望这对它的架构来说是个好主意吗?)

这是设置:

POPUP.js

chrome.extension.sendRequest({greeting: "hello"}, function(response) {
  alert("out"); console.log(response);

  if(response == null){
    alert("No response. :( and the error was "+chrome.extension.lastError.message);
  }
});

Background.html

chrome.extension.onRequest.addListener(   function(request, sender, sendResponse) {
  alert("inside");
  sendResponse({farewell: "goodbye"});
});

会发生什么: 我的alert("inside")开火了,所以我知道从弹出窗口到后台的通信都没问题。 但现在没有答案了!

同样有趣: 在开发人员工具模式下运行相同的程序(检查弹出窗口)一切正常!

0 个答案:

没有答案
相关问题