React构建Chrome扩展后Clerk脚本加载失败问题求助
问题描述
我用React的build命令打包了一款Chrome扩展,但启动时出现「Refused to load script for clerk」错误。我用Clerk实现身份验证,问题就出在这里。开发环境下运行正常,但扩展无法正常工作,希望启动扩展后能正常显示UI。我知道需要在manifest.json v3中添加以下URL,但不清楚正确的配置方式:https://excited-trout-38.clerk.accounts.dev/npm/@clerk/clerk-js@4/dist/clerk.browser.js
当前manifest.json配置如下:
{ "manifest_version": 3, "name": "uvault", "description": "save your important blogs here.", "version": "1.0", "icons": { "64": "icons-64.png", "128": "icons-128.png" }, "action": { "default_popup": "index.html" }, "content_security_policy": { "extension_pages": "script-src 'self'; object-src 'self';", "sandbox": "sandbox allow-scripts; script-src 'self' https://excited-trout-38.clerk.accounts.dev/npm/@clerk/clerk-js@4/dist/clerk.browser.js" }, "host_permissions": ["https://www.developer.chrome.com/*"], "optional_host_permissions": ["https://*/*", "http://*/*"], "permissions": ["storage"] }
解决方案
问题原因
你的弹窗页面属于extension_pages范畴,但当前extension_pages的内容安全策略(CSP)仅允许加载本地脚本,外部Clerk脚本被拦截。你之前把Clerk的URL配置到sandbox中,但弹窗并不在沙箱环境内,所以该配置无效。
修正后的manifest.json配置
{ "manifest_version": 3, "name": "uvault", "description": "save your important blogs here.", "version": "1.0", "icons": { "64": "icons-64.png", "128": "icons-128.png" }, "action": { "default_popup": "index.html" }, "content_security_policy": { "extension_pages": "script-src 'self' https://excited-trout-38.clerk.accounts.dev/npm/@clerk/clerk-js@4/dist/clerk.browser.js; object-src 'self';", "sandbox": "sandbox allow-scripts; script-src 'self'" }, "host_permissions": [ "https://www.developer.chrome.com/*", "https://excited-trout-38.clerk.accounts.dev/*" ], "optional_host_permissions": ["https://*/*", "http://*/*"], "permissions": ["storage"] }
关键修改点
- 将Clerk的脚本URL移到
extension_pages的script-src中,允许扩展页面加载该外部脚本 - 在
host_permissions中添加Clerk的域名,确保扩展可以正常请求该域名下的资源 - 清空
sandbox配置里的Clerk URL,沙箱环境与你的弹窗无关
修改后重新打包并加载扩展,Clerk脚本即可正常加载,UI也能正常显示。
内容的提问来源于stack exchange,提问作者S-111 0
相关产品推荐
相关产品推荐

