简体   繁体   中英

Javascript injection into React Native Webview is not working

I am trying to inject javascript into React Native Web view and code I have checked with Google Chrome Inspect does not affect my web view. Even alert function is not displaying any changes in web view. I am checking on iOS simulator.

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>
);

You can try another way to inject the javascript using the webView ref like this

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)
  }}
/>

you can use onLoadStart if you want the script to be executed first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM