Skip to content

Commit

Permalink
feat: expose reset into even object to allow retry #62
Browse files Browse the repository at this point in the history
  • Loading branch information
CAMOBAP committed Sep 29, 2024
1 parent 250d209 commit 48ac268
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
4 changes: 3 additions & 1 deletion Example.App.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ export default class App extends React.Component {
if (['cancel'].includes(event.nativeEvent.data)) {
this.captchaForm.hide();
this.setState({ code: event.nativeEvent.data});
} else if (['error', 'expired'].includes(event.nativeEvent.data)) {
} else if (['error'].includes(event.nativeEvent.data)) {
this.captchaForm.hide();
this.setState({ code: event.nativeEvent.data});
} else if (event.nativeEvent.data === 'expired') {
event.reset();
} else if (event.nativeEvent.data === 'open') {
console.log('Visual challenge opened');
} else {
Expand Down
18 changes: 15 additions & 3 deletions Hcaptcha.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo, useCallback } from 'react';
import React, { useMemo, useCallback, useRef } from 'react';
import WebView from 'react-native-webview';
import { Linking, StyleSheet, View, ActivityIndicator } from 'react-native';
import ReactNativeVersion from 'react-native/Libraries/Core/ReactNativeVersion';
Expand Down Expand Up @@ -153,7 +153,7 @@ const Hcaptcha = ({
console.log("challenge opened");
};
var onDataExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("expired"); };
var onChalExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("cancel"); };
var onChalExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("expired"); };
var onDataErrorCallback = function(error) {
console.log("challenge error callback fired");
window.ReactNativeWebView.postMessage("error");
Expand Down Expand Up @@ -201,8 +201,17 @@ const Hcaptcha = ({
[loadingIndicatorColor]
);

const webViewRef = useRef(null);

const reset = () => {
if (webViewRef.current) {
webViewRef.current.injectJavaScript('onloadCallback();');
}
};

return (
<WebView
ref={webViewRef}
originWhitelist={['*']}
onShouldStartLoadWithRequest={(event) => {
if (event.url.slice(0, 24) === 'https://www.hcaptcha.com') {
Expand All @@ -212,7 +221,10 @@ const Hcaptcha = ({
return true;
}}
mixedContentMode={'always'}
onMessage={onMessage}
onMessage={(e) => {
e.reset = reset;
onMessage(e);
}}
javaScriptEnabled
injectedJavaScript={patchPostMessageJsCode}
automaticallyAdjustContentInsets
Expand Down
6 changes: 4 additions & 2 deletions __tests__/__snapshots__/ConfirmHcaptcha.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ exports[`ConfirmHcaptcha snapshot tests renders ConfirmHcaptcha with all props 1
})();"
javaScriptEnabled={true}
mixedContentMode="always"
onMessage={[Function]}
onShouldStartLoadWithRequest={[Function]}
originWhitelist={
[
Expand Down Expand Up @@ -103,7 +104,7 @@ exports[`ConfirmHcaptcha snapshot tests renders ConfirmHcaptcha with all props 1
console.log("challenge opened");
};
var onDataExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("expired"); };
var onChalExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("cancel"); };
var onChalExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("expired"); };
var onDataErrorCallback = function(error) {
console.log("challenge error callback fired");
window.ReactNativeWebView.postMessage("error");
Expand Down Expand Up @@ -208,6 +209,7 @@ exports[`ConfirmHcaptcha snapshot tests renders ConfirmHcaptcha with minimum pro
})();"
javaScriptEnabled={true}
mixedContentMode="always"
onMessage={[Function]}
onShouldStartLoadWithRequest={[Function]}
originWhitelist={
[
Expand Down Expand Up @@ -258,7 +260,7 @@ exports[`ConfirmHcaptcha snapshot tests renders ConfirmHcaptcha with minimum pro
console.log("challenge opened");
};
var onDataExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("expired"); };
var onChalExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("cancel"); };
var onChalExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("expired"); };
var onDataErrorCallback = function(error) {
console.log("challenge error callback fired");
window.ReactNativeWebView.postMessage("error");
Expand Down
9 changes: 6 additions & 3 deletions __tests__/__snapshots__/Hcaptcha.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exports[`Hcaptcha snapshot tests renders Hcaptcha with all props 1`] = `
})();"
javaScriptEnabled={true}
mixedContentMode="always"
onMessage={[Function]}
onShouldStartLoadWithRequest={[Function]}
originWhitelist={
[
Expand Down Expand Up @@ -65,7 +66,7 @@ exports[`Hcaptcha snapshot tests renders Hcaptcha with all props 1`] = `
console.log("challenge opened");
};
var onDataExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("expired"); };
var onChalExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("cancel"); };
var onChalExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("expired"); };
var onDataErrorCallback = function(error) {
console.log("challenge error callback fired");
window.ReactNativeWebView.postMessage("error");
Expand Down Expand Up @@ -130,6 +131,7 @@ exports[`Hcaptcha snapshot tests renders Hcaptcha with minimum props 1`] = `
})();"
javaScriptEnabled={true}
mixedContentMode="always"
onMessage={[Function]}
onShouldStartLoadWithRequest={[Function]}
originWhitelist={
[
Expand Down Expand Up @@ -180,7 +182,7 @@ exports[`Hcaptcha snapshot tests renders Hcaptcha with minimum props 1`] = `
console.log("challenge opened");
};
var onDataExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("expired"); };
var onChalExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("cancel"); };
var onChalExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("expired"); };
var onDataErrorCallback = function(error) {
console.log("challenge error callback fired");
window.ReactNativeWebView.postMessage("error");
Expand Down Expand Up @@ -244,6 +246,7 @@ exports[`Hcaptcha snapshot tests test debug 1`] = `
})();"
javaScriptEnabled={true}
mixedContentMode="always"
onMessage={[Function]}
onShouldStartLoadWithRequest={[Function]}
originWhitelist={
[
Expand Down Expand Up @@ -294,7 +297,7 @@ exports[`Hcaptcha snapshot tests test debug 1`] = `
console.log("challenge opened");
};
var onDataExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("expired"); };
var onChalExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("cancel"); };
var onChalExpiredCallback = function(error) { window.ReactNativeWebView.postMessage("expired"); };
var onDataErrorCallback = function(error) {
console.log("challenge error callback fired");
window.ReactNativeWebView.postMessage("error");
Expand Down

0 comments on commit 48ac268

Please sign in to comment.