Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Accept confirm in Webview React Native

I’ve a React Native app (with expo) that has a Webview that I used with injectedJavascript to go through a login an do some stuff.

The thing is in one part, there’s a confirm dialog, that I need to accept and then continue with the JS.

I’ve something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const sendDataFromReactNativeToWebView = async () => {
    await new Promise(() => setTimeout(() => createAlert(), 4000));
    let injectedData = `document.querySelector("#login-user").value= "test";`;
    viewRef.injectJavaScript(injectedData);
    await new Promise((resolve) => setTimeout(resolve, 500));
    injectedData = `document.querySelector("#login-password").value= "test123";`;
    viewRef.injectJavaScript(injectedData);
    await new Promise((resolve) => setTimeout(resolve, 500));
    injectedData = `document.getElementById("pc-login-btn").click();`;
    viewRef.injectJavaScript(injectedData);
    //here I've a confirm that I need to accept
    await new Promise((resolve) => setTimeout(resolve, 15000));
    injectedData = `document.querySelector("#menuTree > li:nth-child(2) > a").click();`;
    viewRef.injectJavaScript(injectedData);  
  };

return (
    <>
      <View style={styles.container}>
        <Image source={logo} />
        <Text style={styles.baseText}>
          Test <Text style={styles.innerText}>
          </Text>
        </Text>
        <View style={{ margin: 10 }}></View>
        <Button
          title="Test me"
          onPress={() => sendDataFromReactNativeToWebView()}
        />  
        <StatusBar style="auto" />
      </View>
      <View style={styles.web}>
        <SafeAreaView style={{ flex: 1 }}>
          <TouchableOpacity onPress={() => setSeeWebView(false)}>
            <Text>Close x</Text>
          </TouchableOpacity>
          <WebView
            style={{ flex: 1 }}
            source={{ uri: "http://www.someurl.com" }} 
            javaScriptEnabledAndroid={true}
            ref={(webView) => (viewRef = webView)}
          />
        </SafeAreaView>
      </View>
    </>
  );
}

There is a onMessagein webview that maybe I can use but don’t know how.

Any idea?

>Solution :

Nice question.
I think that you can use

function confirm() { 
return true;
} 

At least it will work if you execute it before each confirm() happens.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading