允许在 Electron 中进行评估?

Allowing eval in Electron?

我想使用 Two.js 编写一个小型 Electron 应用程序,但我意识到 Electron 不喜欢任何人使用 eval

如果我在 renderer.js 中这样做:

let u = eval('1 + 1');

我遇到了这个问题:

Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self'".

当然我在webPreferences中启用了allowRunningInsecureContent: true

Electron 中是否有任何使用 eval 的 运行 包的方法?

您只需将其添加到已加载 html

<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self'">

问题不是来自电子,但我假设您是通过本地网络服务器 运行 电子应用程序,在这种情况下,您还需要为 CSP 提供正确的配置。