微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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/...)加载工作人员。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。