Chrome 扩展 BackgroundScript 路径
Chrome Extension BackgroundScript Path
我正在尝试从 cdn 路径加载后台脚本('https://cdn.xyz.com/scripts/background.js') in manifest.json in my Chrome Extension, but it's throwing me error - Could not load background script 'https://cdn.xyz.com/scripts/background.js'. I have already added https://cdn.xyz.com in content_security_policy in manifest.json 文件。
Manifest.json
{
"name": "MyExtension",
"manifest_version": 2,
"background": {
"scripts": [
"https://cdn.xyz.com/scripts/background.js"
],
"persistent": false
},
"permissions": [
"http://*/*",
"https://*/*",
],
"content_security_policy": "script-src 'self' 'unsafe-eval' https://cdn.xyz.com; object-src 'self'"
}
那个CDN使用HTTP/1.1 301 Moved Permanently
重定向到cdn.xyz
,试试:
"content_security_policy": "script-src 'self' 'unsafe-eval' https://cdn.xyz https://cdn.xyz.com; object-src 'self'"`
如果可行,您还可以添加通配符域并保留旧 cdn.xyz.com
域以实现向后兼容性。
您不能直接在清单中添加远程脚本。
调整 CSP 允许您动态加载脚本 - 通过在您的(本地)后台代码中创建一个具有适当 src
的 <script>
节点。
// Local background script
let script = document.createElement('script');
script.src = "https://cdn.xyz.com/scripts/background.js";
document.head.appendChild(script); // Executes in background document
请注意,Google 非常反对这样的代码加载(大部分逻辑托管在别处并独立更新,在特权上下文中执行)并且 Mozilla 完全禁止列出的 WebExtensions。
我正在尝试从 cdn 路径加载后台脚本('https://cdn.xyz.com/scripts/background.js') in manifest.json in my Chrome Extension, but it's throwing me error - Could not load background script 'https://cdn.xyz.com/scripts/background.js'. I have already added https://cdn.xyz.com in content_security_policy in manifest.json 文件。
Manifest.json
{
"name": "MyExtension",
"manifest_version": 2,
"background": {
"scripts": [
"https://cdn.xyz.com/scripts/background.js"
],
"persistent": false
},
"permissions": [
"http://*/*",
"https://*/*",
],
"content_security_policy": "script-src 'self' 'unsafe-eval' https://cdn.xyz.com; object-src 'self'"
}
那个CDN使用HTTP/1.1 301 Moved Permanently
重定向到cdn.xyz
,试试:
"content_security_policy": "script-src 'self' 'unsafe-eval' https://cdn.xyz https://cdn.xyz.com; object-src 'self'"`
如果可行,您还可以添加通配符域并保留旧 cdn.xyz.com
域以实现向后兼容性。
您不能直接在清单中添加远程脚本。
调整 CSP 允许您动态加载脚本 - 通过在您的(本地)后台代码中创建一个具有适当 src
的 <script>
节点。
// Local background script
let script = document.createElement('script');
script.src = "https://cdn.xyz.com/scripts/background.js";
document.head.appendChild(script); // Executes in background document
请注意,Google 非常反对这样的代码加载(大部分逻辑托管在别处并独立更新,在特权上下文中执行)并且 Mozilla 完全禁止列出的 WebExtensions。