4

I want to listen a click event triggered inside a webapp , this webapp loads in react-native webview. So need to listen the click event and then run an async function in react-native.

My use-case is to download a file once save button clicked (This save button is inside webapp).

But nothing gets triggered or listened inside handleNavigationStateChange and onMessage. please find the below code.

  render() {
    const url = 'https://www.takeselfie.com/#';
    return (
      <WebView
      useWebKit
      originWhitelist={['*']}
      allowsInlineMediaPlayback
        bounces={true}
        source={{
          uri: url,
        }}
        startInLoadingState={true}
        scalesPageToFit
        javaScriptEnabledAndroid={true}
        javaScriptEnabled={true}
        injectedJavaScript={`document.body.addEventListener('click', function(e){
          window.postMessage("Message from WebView","*");
            console.log(e)
        })`}
        onMessage={e => console.log("message==", e)}
         mixedContentMode={'always'}
        allowUniversalAccessFromFileURLs={true}
        onNavigationStateChange={this.handleNavigationStateChange}
        style={{ flex: 1 }}
      />
    );
  }
Vasant
  • 101
  • 2
  • 10
  • You could read this article https://medium.com/react-native-training/improving-communication-between-react-native-webviews-and-the-webpages-they-render-792c8f7db3e5 – ridoansaleh Nov 11 '20 at 09:28
  • did you get a solution.. please let me know i am also trying to this same – sherkhan May 23 '22 at 07:15

0 Answers0