在使用Electron封裝一些模塊的時(shí)候,出現(xiàn)以下錯(cuò)誤:
Refused to load the script ‘https://unpkg.com/xxxx.js’ because it violates the following Content Security Policy directive: “script-src ‘self’ ‘unsafe-eval’ ‘unsafe-inline’ data:”. Note that ‘script-src-elem’ was not explicitly set, so ‘script-src’ is used as a fallback.
這是由于Electron為了防止出現(xiàn)XSS攻擊,阻止了該網(wǎng)站資源的加載,我們?cè)O(shè)置允許加載即可。
解決方法
例如,我們當(dāng)前被禁止的xxxx.js
來(lái)自https://unpkg.com
那么我們可以在electron程序的主進(jìn)程中添加如下代碼段文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-698538.html
app.on('ready', () => {
createWindow()
session.defaultSession.webRequest.onHeadersReceived((details: Electron.OnHeadersReceivedListenerDetails, callback: (headersReceivedResponse: Electron.HeadersReceivedResponse) => void) => {
callback({
responseHeaders: {
...details.responseHeaders,
'Content-Security-Policy': ['default-src \'self\' \'unsafe-inline\' \'unsafe-eval\' https://unpkg.com'] // 注意這里的https://unpkg.com
}
})
})
});
官方詳解:CSP解決方案文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-698538.html
到了這里,關(guān)于Refused to load the script ‘xxxx.js‘ because it violates the following Content Security Policy ...的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!