(Chrome扩展程序)无法将消息从background.js传递到popup.js

时间:2019-05-24 00:30:03

标签: javascript google-chrome-extension

我没有收到弹出脚本收到消息时应该显示的警报。

manifest.json:

{
  "manifest_version": 2,
  "name": "Drill Sergeant",
  "description": "Tracks time spent on 'watchlist' websites",
  "version": "1.0.0",
  "browser_action": {
    "default_popup": "popup.html"
  },
  "background": {"scripts": ["background.js"]},
  "web_accessible_resources": ["background.js"],
  "permissions": ["activeTab", "webNavigation", "notifications", "tabs"]
}

在我的html文件中包含popup.js:

<body>
     ....
    <script src = popup.js></script>

</body>

来自后台的消息通话:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
{
  chrome.runtime.sendMessage({
    text: "please work"
  });
   checkMe();
});

popup.js代码:

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        alert("PLEASE WORK");
        console.log("PLEASE WORK");
    }
);

每当我更改页面时都会执行checkMe()函数(我知道也是因为它也具有调试警报),但是我没有从popup.js收到警报...我看过其他线程,使用了runtime.onMessage而不是tabs.onMessage,但仍然无法正常工作。感谢您的帮助

0 个答案:

没有答案