代码之家  ›  专栏  ›  技术社区  ›  Mick

后台服务修改新的选项卡页面,清单V3,-需要什么权限

  •  0
  • Mick  · 技术社区  · 3 年前

    我正在尝试制作一个chrome扩展,它覆盖新的选项卡页面,然后后台服务对该页面进行更改。因此,在我的清单中,我有(除其他外):

      "manifest_version": 3,
          "chrome_url_overrides": {
            "newtab": "newtab.html"
          },
      "permissions": [  "scripting", "activeTab" ... and many many others!
    

    这很好,因为当我添加一个新的选项卡时,我确实看到了我放在自己的自定义“newtab.html”中的文件的内容。

    在清单中,我还放了。。。

    "host_permissions": [
        "<all_urls>",
        "chrome://newtab/"
    ],
    

    …然后在我的后台服务中,作为一个测试,我输入:

    chrome.scripting.executeScript({
      target: { tabId: tabId },
        func: () => {
        const h1 = document.createElement('h1');
        h1.textContent = 'Hello';
        document.body.appendChild(h1);}
      });
    

    但是当这个代码运行时,我得到:

    Uncaught (in promise) Error: Cannot access contents of url "chrome-extension://mhjkkbnnfoaeadgpnoheabgocjfhgkli/newtab.html". Extension manifest must request permission to access this host.
    

    我已经广泛搜索了答案,并添加了许多不同的权限和host_permissions,但到目前为止没有任何效果。

    1 回复  |  直到 3 年前
        1
  •  1
  •   woxxom    3 年前

    Chrome不允许在扩展自己的页面上执行executeScript,但无论如何都不需要它。

    1. 去除 permissions , host_permissions background 来自manifest.json。
    2. 添加 <script src=newtab.js></script> 在html的末尾
    3. 在newtab.JS中使用标准的JS和DOM方法