【问题标题】:Cannot start vfat.tools locally - Refused to execute inline script无法在本地启动 vfat.tools - 拒绝执行内联脚本
【发布时间】:2026-01-11 20:55:01
【问题描述】:

我按照 vfat.tools (https://github.com/vfat-tools/vfat-tools) 的 README 说明进行操作,即运行 npm install,最后运行 npm run dev。我在控制台上看到以下内容:

[Browsersync] 访问 URL:

   Local: http://localhost:3000
External: http://192.168.0.197:3000

      UI: http://localhost:3001

UI 外部:http://localhost:3001

[Browsersync] 提供来自:dist 的文件 [Browsersync] 正在观看文件...

但是,当我打开 localhost:3000 访问 UI 时,我在 Chrome 的控制台上看到以下错误:

Refused to execute inline script because it violates the following Content Security Policy 
directive: "default-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-
ACotEtBlkqjCUAsddlA/3p2h7Q0iHuDXxk577uNsXwA='), or a nonce ('nonce-...') is required to enable 
inline execution. Note also that 'script-src' was not explicitly set, so 'default-src' 
is used as a fallback.

解决此问题的选项包括在代码中的某处添加unsafe-inline(例如 -> Script causes “Refused to execute inline script: Either the 'unsafe-inline' keyword, a hash… or a nonce is required to enable inline execution”),但我也觉得这不是一个好习惯。

如何让网页正常加载?

【问题讨论】:

    标签: ethereum content-security-policy cryptocurrency


    【解决方案1】:

    内联脚本有 3 种:<script>...</script><a href='javascript:void(0)'<a onclick='eventHandler()'
    第一个可以使用'nonce-value''hash-value' 解决,后两个需要使用强制'unsafe-inline'(或代码重构)。

    因此,要消除此错误,您需要知道哪种内联脚本导致了违规。

    我可以看到vfat.tools 使用document.getElementById("theme-button").onclick = setTheme(也许在其他地方使用类似的东西)。因此你必须使用'unsafe-inline'或者重写这部分代码使用addEventListener()函数绑定一个事件监听器。

    【讨论】: