Inject inline script in cordova wkwebview IOS



好的,所以我已经看到了所有的线程,但似乎无法让它工作。

<meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; media-src *; img-src *">

如果我直接添加:

<script type="text/javascript">
alert('Hello world');
</script>

这行得通。

..但。。如果我从本地存储注入它,它会使应用程序崩溃。不过,我可以很好地注入 html 和 css。

我安装了这些 NPM:

<plugin name="cordova-plugin-wkwebview-engine" spec="git+https://github.com/apache/cordova-plugin-wkwebview-engine.git#1.0.0" />
<plugin name="cordova-plugin-wkwebview-file-xhr" spec="^1.0.0" />

我使用这个小函数来加载它。适用于UIwebview

Nate.createScript = function(input){
var head = document.getElementsByTagName('head')[0]
var el = document.createElement('script')
el.setAttribute('type', 'text/javascript')
el.innerText = input
head.appendChild(el)
}

本地存储如下所示:

localStorage.js = "alert('hello world')";
Nate.createScript(localStorage.js)