解决办法:在tauriconfig中配置
"csp": "default-src 'self' asset: 'unsafe-inline' customprotocol://* http://localhost:* ws:localhost:* 'unsafe-eval' ipc: http://ipc.localhost; script-src 'unsafe-eval' 'self' https://www.googletagmanager.com 'unsafe-inline'; connect-src 'self' http://localhost:* ws:localhost:* ipc://localhost https://api.github.com https://www.google-analytics.com https://google-analytics.com https://pakeplus.com http://ipc.localhost customprotocol://* https://www.pakeplus.com; img-src 'self' https://avatars.githubusercontent.com https://www.googletagmanager.com asset: http://asset.localhost blob: data: http://asset.localhost; font-src 'self' asset: http://localhost data:;",
这个错误是由于浏览器的内容安全策略(CSP)阻止了字符串形式的JavaScript代码执行。让我为您分析并提供解决方案。
问题分析
从错误信息可以看出:
-
您的CSP策略禁止使用
unsafe-eval
-
代码试图通过
Function()
构造函数或eval()
执行字符串形式的JavaScript -
错误发生在Tauri应用程序环境中
在cocos游戏打包为移动端web的时候,需要这个场景配置。
还需要注意就是在通过参数传递到index..html后面的时候,在rust中使用base64解析,然后再传递,可以有效防止特殊字符编码问题。然后在js中通过下面的方式获取解析参数:
// 获取url中的查询参数
const urlSearch = window.location.search
console.log('urlSearch', urlSearch)
const urlParams = new URLSearchParams(urlSearch)
console.log('game on urlParams', urlParams)
const token = urlParams.get('ppargs')
console.log('game on load', token)
再使用base64解析获取到的参数即可。