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:
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 onMessage
in webview
that maybe I can use but don't know how.
Any idea?
CodePudding user response:
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.