【问题标题】:Activate Chrome Extension Background Page Via Message通过消息激活 Chrome 扩展后台页面
【发布时间】:2016-05-28 03:17:35
【问题描述】:

我正在尝试编写我的第一个 chrome 扩展,并致力于让消息传递和 background.js 正常工作。看起来我的 background.js 从未被调用或接收任何消息。有人可以告诉我我做错了什么吗?这是一个最小的例子:

ma​​nifest.json

{
  "manifest_version": 2,
  "name": "RAData",
  "version": "0.1",
  "background": {
    "scripts": ["background.js"] },
  "content_scripts": [{
    "matches": [
      "<all_urls>"],
    "js": ["content.js"] }]
}

content.js

chrome.runtime.sendMessage("message from content");
console.log("content.js loaded");

background.js

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  console.log("message received");
});
console.log("background.js loaded");

我认为应该发生的是每当我加载页面时,应该加载 content.js,将“content.js 加载”打印到控制台,然后用消息唤醒 background.js。然后,Background.js 应将“收到的消息”和“已加载的background.js”打印到控制台。

相反,控制台只显示“content.js 已加载”——它看起来不像 background.js 中的任何代码被执行过。

【问题讨论】:

    标签: javascript google-chrome-extension


    【解决方案1】:

    您没有查看后台页面的控制台。您可以通过在 chrome://extensions 上为您的扩展点击“检查视图:”旁边的“背景页面”来访问它

    【讨论】:

    • 哦。我没有意识到他们有不同的控制台。嗯,这很简单。 :-)
    猜你喜欢
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 2012-01-20
    • 1970-01-01
    相关资源
    最近更新 更多