【问题标题】:Chrome extension background page not adding new tab?Chrome扩展背景页面没有添加新标签?
【发布时间】:2014-03-18 08:59:15
【问题描述】:

我已经编写了一个示例扩展,只是在后台页面中将新标签添加到 chrome。它不工作。我是 chrome 扩展程序的新手,您能帮帮我吗?

manifest.json

{
  "manifest_version": 2,

  "name": "My extension",
  "description": "Test Extension.",
  "version": "1.0",
  "background": {
   "page": "bg.html"
   },

  "permissions": [
    "history",
    "tabs"
  ],
  "browser_action": {
    "default_icon": "icon.png"
  }
}

bg.html

chrome.browserAction.onClicked.addListener(function(activeTab)
{
    var newURL = "www.google.com";
    chrome.tabs.create({ url: newURL });
});

【问题讨论】:

    标签: google-chrome-extension


    【解决方案1】:

    尝试将 bg.html 重命名为 bg.js 并像这样更改清单背景声明

    manifest.json:

      ...
      "background": {
        "scripts": ["bg.js"]
      },
      ...
    

    bg.js:

    chrome.browserAction.onClicked.addListener(function(activeTab)
    {
        var newURL = "www.google.com";
        chrome.tabs.create({ url: newURL });
    });
    

    【讨论】:

    • 感谢您的帮助。我是新来的。你能帮我解释为什么需要java脚本吗?为什么它不适用于 stackoverflow.com/questions/3188384/… 中建议的简单 html 背景页面
    • html后台页面中不允许使用内联javascript。您可以使用脚本标签<script src="bg.js"></script> 并使用"page" 保留清单声明
    • 知道了。感谢@Kirween 的解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 2015-03-07
    • 1970-01-01
    • 2016-03-27
    相关资源
    最近更新 更多