Javascript注入到React Native Webview不工作



我正试图将javascript注入到React Native Web视图和代码中,我已经用Google Chrome检查过了,不影响我的Web视图。甚至警报功能也不显示web视图中的任何更改。我正在检查iOS模拟器。

const runFirst = `
let footer = document.getElementsByTagName("#footer")[0];
footer.style.display='none';
true;
`;
return (
<View style={styles.container}>
<WebView
source={{ uri: 'https://mobex.az/' }}
style={{ flex: 1, marginTop: 40 }}
bounces={false}
automaticallyAdjustContentInsets={false}
javaScriptEnabled={true}
domStorageEnabled={true}
startInLoadingState={true}
injectedJavaScript={runFirst}
/>
</View>
);

你可以尝试另一种方式来注入javascript使用webView ref像这样

const webViewRef = useRef(null)
<WebView
source={{ uri: 'https://mobex.az/' }}
style={{ flex: 1, marginTop: 40 }}
bounces={false}
automaticallyAdjustContentInsets={false}
javaScriptEnabled={true}
domStorageEnabled={true}
startInLoadingState={true}
// instead of injectedJavaScript={runFirst}
onLoadEnd={() => {
webViewRef?.current?.injectJavaScript(runFirst)
}}
/>

你可以使用onLoadStart如果你想让脚本先执行。

最新更新