-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathReCaptchaComponent.js
53 lines (53 loc) · 2.43 KB
/
ReCaptchaComponent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("./constants");
const React = require("react");
const react_native_1 = require("react-native");
const react_native_webview_1 = require("react-native-webview");
const patchPostMessageJsCode = `(${String(function () {
const originalPostMessage = window.postMessage;
const patchedPostMessage = (message, targetOrigin, transfer) => {
originalPostMessage(message, targetOrigin, transfer);
};
patchedPostMessage.toString = () => String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage');
window.postMessage = patchedPostMessage;
})})();`;
const getExecutionFunction = (siteKey, action) => {
return `window.grecaptcha.execute('${siteKey}', { action: '${action}' }).then(
function(args) {
window.ReactNativeWebView.postMessage(args);
}
)`;
};
const getInvisibleRecaptchaContent = (siteKey, action) => {
return `<!DOCTYPE html><html><head>
<script src="https://www.google.com/recaptcha/api.js?render=${siteKey}"></script>
<script>window.grecaptcha.ready(function() { ${getExecutionFunction(siteKey, action)} });</script>
</head></html>`;
};
class ReCaptchaComponent extends React.PureComponent {
constructor() {
super(...arguments);
this._webViewRef = null;
}
refreshToken() {
if (constants_1.platform.isIOS && this._webViewRef) {
this._webViewRef.injectJavaScript(getExecutionFunction(this.props.siteKey, this.props.action));
}
else if (constants_1.platform.isAndroid && this._webViewRef) {
this._webViewRef.reload();
}
}
render() {
return React.createElement(react_native_1.View, { style: { width: 1, height: 1, opacity: 0 } },
React.createElement(react_native_webview_1.WebView, { ref: (ref) => {
this._webViewRef = ref;
}, javaScriptEnabled: true, androidHardwareAccelerationDisabled: true, originWhitelist: ['*'], automaticallyAdjustContentInsets: true, mixedContentMode: 'always', injectedJavaScript: patchPostMessageJsCode, source: {
html: getInvisibleRecaptchaContent(this.props.siteKey, this.props.action),
baseUrl: this.props.captchaDomain
}, onMessage: (e) => {
this.props.onReceiveToken(e.nativeEvent.data);
} }));
}
}
exports.default = ReCaptchaComponent;