【问题标题】:How can I add chrome-search://local-ntp/local-ntp.html to manifest in chrome extension如何将 chrome-search://local-ntp/local-ntp.html 添加到 chrome 扩展中的清单
【发布时间】:2020-05-25 19:41:31
【问题描述】:

我有一个 chrome 扩展程序, 我在 activeTab 上运行了一个内容脚本,它在其他选项卡上运行良好,但是当我在新选项卡上运行脚本时,浏览器抛出错误“Unchecked runtime.lastError: Cannot access contents of url”chrome-search:/ /local-ntp/local-ntp.html"。扩展清单必须请求访问此主机的权限。" 我以为我可以将此网址添加到清单文件中,但它不起作用。我在stackoverflow上看到了一些其他问题的解决方案,但到目前为止都没有。

这是我执行脚本的方式:

var link = 'http://localhost/example/abc.html';
chrome.tabs.query({'active': true, 'currentWindow': true}, function (tabs) {

            chrome.tabs.executeScript(tabs[0].id, {
                    file: "lib/prefetch.js"
                  }, function() {
                    chrome.tabs.sendMessage(tabs[0].id, {link: link});
                    resolve(0);//resolve the promise

            });

        });

Manifest.json 如下:

{
  "name": "Example Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Example extension",
  "browser_action": {
    "default_icon": "img/grey-16.png",
    "default_popup": "index.html"
  },
  "permissions": [
    "tabs", 
    "notifications",
    "background", 
    "activeTab",
    "webRequest",
    "webRequestBlocking",
    "contentSettings",
    "browsingData",
    "http://*/*",
    "https://*/*"
  ],
  "content_scripts" : [
   {
     "js": [ "index.js", "ext/jquery-2.1.4.min.js"],
     "matches": ["*://*/*"]
   },
   {
     "js": [ "content.js", "onexecute.js"],
     "run_at": "document_start",
     "matches": ["*://*/*"]
   }
  ],
  "background": {
      "scripts" : ["ext/jquery-2.1.4.min.js","ext/async.min.js","ext/himalaya.js","background.js"],
      "persistent": true,
      "matches": ["*://*/*"]
  },
  "content_security_policy": "script-src 'self' 'unsafe-eval' https://rawgit.com/caolan/async/master/dist/async.min.js https://ajax.googleapis.com;  object-src 'self'"
}

【问题讨论】:

  • 我看到了,谢谢,但它不起作用。
  • 你能显示清单文件吗?这就是错误告诉您更新的内容。
  • 我添加了清单文件
  • 内容脚本无法在默认的内置新标签页上运行。
  • 但我尝试在将新 url 放到地址栏并在“headersRecevied”事件上执行后运行它。在这种情况下是否仍然适用?

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


【解决方案1】:

我也不能在新标签上运行它。我有这些权限:

"permissions": [
  "contextMenus",
  "tabs",
  "activeTab",
  "https://*/",
  "notifications",
  "<all_urls>",
  "storage"
],

我认为新标签没有设置 url,因此您将无法从新标签运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 2016-06-18
    • 2021-09-30
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2020-07-23
    相关资源
    最近更新 更多