Ale*_*lls 8 javascript google-chrome-extension angular5
重新加载Chrome扩展程序时出现此错误:
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' blob: filesystem: chrome-extension-resource:".
at new Function (<anonymous>)
at evalExpression (compiler.js:33919)
at jitStatements (compiler.js:33937)
at JitCompiler.webpackJsonp.../../../compiler/esm5/compiler.js.JitCompiler._interpretOrJit (compiler.js:34520)
at JitCompiler.webpackJsonp.../../../compiler/esm5/compiler.js.JitCompiler._compileTemplate (compiler.js:34448)
at compiler.js:34347
at Set.forEach (<anonymous>)
at JitCompiler.webpackJsonp.../../../compiler/esm5/compiler.js.JitCompiler._compileComponents (compiler.js:34347)
at compiler.js:34217
at Object.then (compiler.js:474)
Run Code Online (Sandbox Code Playgroud)
我在manifest.json中有这个:
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
Run Code Online (Sandbox Code Playgroud)
我可以使用任何其他设置来避免该错误吗?
小智 37
花了我几个小时,但你可能想要做的是改变webpack使用的源映射的风格.默认情况下,它使用eval.
https://webpack.js.org/configuration/devtool/
我把它添加到我的webpack.config.js:
devtool: 'cheap-module-source-map'
这方面的诀窍是弄清楚为什么webpack --mode development
有错误而webpack --mode production
没有.
我也使用React而不是Polymer,但我很确定这仍然适用.
Jia*_*hen 10
感谢@Randy 的回答。但是,对于 Vue CLI 生成的 vue 项目,没有webpack.config.js
,因此解决方案将添加以下配置到vue.config.js
:
configureWebpack: {
devtool: 'cheap-module-source-map'
}
Run Code Online (Sandbox Code Playgroud)
不允许使用chrome扩展名unsafe-eval
,或者eval
实际上根本不允许使用。
https://developer.mozilla.org/zh-CN/Add-ons/WebExtensions/Content_Security_Policy
制作Chrome扩展程序时,请了解它受到内容安全政策的严格限制。确保阅读并理解WebExtensions内容安全策略。如果您想要一个内联脚本,例如:
<script>
alert('hello')
</script>
Run Code Online (Sandbox Code Playgroud)
您将必须将脚本标签的内容计算为其SHA256值,并将其添加到清单中,以便允许执行它。
通过清单克服有趣的阅读
https://developer.chrome.com/extensions/contentSecurityPolicy
评估JavaScript
可以通过在策略中添加“ unsafe-eval”来放宽针对eval()及其诸如setTimeout(String),setInterval(String)和new Function(String)的亲属的策略:
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
Run Code Online (Sandbox Code Playgroud)
但是,强烈建议您不要这样做。这些功能是臭名昭著的XSS攻击媒介。
归档时间: |
|
查看次数: |
12133 次 |
最近记录: |