使用源参数html中的脚本进行React原生webview在iOS上有效,但在android上无效



下面显示了这个react原生web视图设置。它在iOS上运行良好,但在Android上显示一个空白页面。

我使用的是RN 0.63.3和RNWebview 11.15.0

如何解决此问题?

<WebView
onShouldStartLoadWithRequest={onRouteChange}
javaScriptEnabled={true}
injectedJavaScript={`(${ReportJS})()`}
injectedJavaScriptForMainFrameOnly={true}
onMessage={onMessage}
onLoadStart={onLoadStart}
style={styles.webview}
ref={webViewRef}
originWhitelist={['*']}
source={{
html: `
<body>
<script>${startupJS}</script>
</body>
`,
}}

/>

这是ReportJS:内部的功能

[window, document].forEach((el) =>
el.addEventListener('load', (event) => {
let data = 'success';
if (location.href.startsWith('<redacted url>')) {
const inputElement = document.querySelector(
"form input[name='response']",
);
data = inputElement?.value;
}
if (data === undefined) {
data = 'Hello!';
}
window.ReactNativeWebView.postMessage(data);
}),
);

在页面中运行文件集作为injectedJavascript变量而不是使用外部文件解决了我的问题

最新更新