gpt4 book ai didi

Firefox 内容脚本未在某些页面中加载

转载 作者:行者123 更新时间:2023-12-05 00:14:02 26 4
gpt4 key购买 nike

语境

我目前正在开发一个浏览器扩展,它可以在 Chrome 和 Opera 上按预期工作,但我在使用 Firefox 时遇到了问题。这是 manifest.json 的最小版本需要重现问题:

{
"name": "Example",
"version": "0.0.1",
"author": "Pyves",
"content_scripts": [
{
"all_frames": true,
"matches": [
"<all_urls>"
],
"js": [
"content.js"
]
}
],
"manifest_version": 2
}

这是相关的 content.js :
console.log("Content script loaded");

问题
Content script loaded使用 Chrome 和 Opera 时,无论访问的页面如何,都会系统地记录。然而,在使用 Firefox 时,内容脚本似乎无法加载到某些页面中,例如原始 GitHub 页面,如下所示:
https://raw.githubusercontent.com/badges/shields/master/README.md

Firefox 控制台中没有错误消息说明为什么未在该特定页面上执行内容脚本。

问题
  • 为什么 Firefox 扩展无法将内容脚本加载到某些页面?
  • 需要进行哪些更改才能使扩展程序在所有浏览器上一致运行?
  • 最佳答案

    我终于弄清楚为什么在使用 Firefox 时扩展的内容脚本没有加载到某些页面中。

    使用网络开发者工具分析请求后,发现在获取 GitHub 原始页面时返回以下 header :

    Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; sandbox

    根据 MDN Web Docs , sandbox CSP 指令具有以下作用:

    enables a sandbox for the requested resource [...]. It applies restrictions to a page's actions including preventing popups, preventing the execution of plugins and scripts, and enforcing a same-origin policy.



    因此,Firefox 使用沙箱 CSP 阻止扩展程序在页面中执行内容脚本,而其他浏览器(例如 Chrome 和 Opera)确实允许这种行为。 Mozilla 的 Bugzilla 中的相关错误报告( 12670271411641 )强调:

    CSP 'sandbox' directive prevents content scripts from matching, due to unique origin



    此问题已得到确认,并有望在 future 的 Firefox 版本中得到修复。

    关于Firefox 内容脚本未在某些页面中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47622435/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com