某些页面未加载 Firefox 内容脚本
Firefox content script not loading in some pages
上下文
我目前正在开发一个浏览器扩展程序,它在 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");
问题
使用 Chrome 和 Opera 时,无论访问的页面如何,都会系统地记录 Content script loaded
。然而,在使用 Firefox 时,内容脚本似乎不会加载到某些页面中,例如原始 GitHub 页面,如下所示:
https://raw.githubusercontent.com/badges/shields/master/README.md
Firefox 控制台中没有错误消息说明内容脚本未在该特定页面上执行的原因。
问题
为什么 Firefox 扩展无法将内容脚本加载到某些页面?
需要进行哪些更改才能使扩展在所有浏览器上一致地工作?
我终于弄明白为什么在使用 Firefox 时扩展的内容脚本没有加载到某些页面中。
使用网络开发者工具分析请求后,发现在获取 GitHub 个原始页面时返回以下 headers:
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 (1267027 and 1411641) 中的相关错误报告强调:
CSP 'sandbox' directive prevents content scripts from matching, due to unique origin
此问题已得到确认,有望在未来的 Firefox 版本中得到解决。
上下文
我目前正在开发一个浏览器扩展程序,它在 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");
问题
使用 Chrome 和 Opera 时,无论访问的页面如何,都会系统地记录Content script loaded
。然而,在使用 Firefox 时,内容脚本似乎不会加载到某些页面中,例如原始 GitHub 页面,如下所示:
https://raw.githubusercontent.com/badges/shields/master/README.md
Firefox 控制台中没有错误消息说明内容脚本未在该特定页面上执行的原因。
问题
为什么 Firefox 扩展无法将内容脚本加载到某些页面?
需要进行哪些更改才能使扩展在所有浏览器上一致地工作?
我终于弄明白为什么在使用 Firefox 时扩展的内容脚本没有加载到某些页面中。
使用网络开发者工具分析请求后,发现在获取 GitHub 个原始页面时返回以下 headers:
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 (1267027 and 1411641) 中的相关错误报告强调:
CSP 'sandbox' directive prevents content scripts from matching, due to unique origin
此问题已得到确认,有望在未来的 Firefox 版本中得到解决。