Ace 编辑器中的 JSHint 工作器在静态 HTML 页面上被阻止

问题描述

我尝试从计算机上的 HTML 文件加载 Ace editor(使用 file:/// 协议)来编辑 JavaScript:

ace.edit("editor",{
    "mode": "ace/mode/javascript",})

然而,CSP 会阻止 JSHint 使用的工作线程(Ace 认实现)。控制台会记录以下错误

Content Security Policy: The page’s settings blocked the loading of a resource at blob:null/[...] (“worker-src”).

当我将 loadWorkerFromBlob 设置为 false 时:

ace.config.set("loadWorkerFromBlob",false)

产生了不同的错误

Security Error: Content at file:///[...]/index.html may not load data from file:///[...]/ace/worker-javascript.js.

我尝试使用 <Meta http-equiv="Content-Security-Policy"> 绕过 CSP 但无济于事,搜索此问题没有结果。

如何从本地 HTML 页面在 Ace 编辑器中使用 JSHint?

编辑:忘记说明这只是 Firefox 的问题。第一个示例在 Chromium 上对我来说完全正常。

解决方法

file:// URL 产生一个 null 来源,如您在错误消息中所见:

The page’s settings blocked the loading of a resource at blob:null/[...]

因此 CORS 不支持本地 file:// 访问,请参阅 herehere
还有Chrome doesn't let you load web workers when running scripts from a local file

上面的链接包含一些解决方法,但我认为最好的方法是运行本地服务器以通过网络方案(http://localhost/...)加载工作人员。