【问题标题】:Content script not being allowed to access extension resources不允许内容脚本访问扩展资源
【发布时间】:2022-11-26 19:57:33
【问题描述】:

在我的扩展中,我动态地注入了一个内容脚本。内容脚本是

let txt = fetch(chrome.runtime.getURL('example.txt')).then(r=>r.text());

我得到的错误是GET chrome-extension://invalid/ net::ERR_FAILED (anonymous) @ content-script.js:36

在我的研究中,我找到了 https://developer.chrome.com/docs/extensions/mv3/manifest/web_accessible_resources/ 页面,该页面讨论了一些脚本如何获得被拒绝的资源。但它说内容脚本不受影响。

问题是什么? 文档错了吗?

【问题讨论】:

    标签: google-chrome-extension content-script chrome-extension-manifest-v3


    【解决方案1】:

    我假设你指的是来自 Manifest - Web Accessible Resources 的这句话:
    “内容脚本本身不需要被允许。”

    这意味着您无需将 content-script.js 声明为可通过网络访问的资源。

    另一方面,example.txt 不是内容脚本。它只是一个文件,是您的扩展程序的一部分。因为 content-script.js 在网页上下文中运行,所以如果您将 example.txt 声明为可通过网络访问的资源,content-script.js 只能获取 example.txt。

    【讨论】:

      猜你喜欢
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 2016-11-19
      • 2012-07-04
      相关资源
      最近更新 更多