-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReCaptchaView.js
More file actions
71 lines (65 loc) · 2.28 KB
/
ReCaptchaView.js
File metadata and controls
71 lines (65 loc) · 2.28 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React from 'react';
import { WebView } from 'react-native';
import PropTypes from 'prop-types';
// fix https://github.com/facebook/react-native/issues/10865
const patchPostMessageJsCode = `(${String(function() {
var originalPostMessage = window.postMessage
var patchedPostMessage = function(message, targetOrigin, transfer) {
originalPostMessage(message, targetOrigin, transfer)
}
patchedPostMessage.toString = function() {
return String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage')
}
window.postMessage = patchedPostMessage
})})();`
const getWebviewContent = (siteKey) => {
const originalForm = '<!DOCTYPE html><html><head>' +
'<style> .text-xs-center { text-align: center; } .g-recaptcha { display: inline-block; } </style>' +
'<script src="https://www.google.com/recaptcha/api.js"></script>' +
'<script type="text/javascript"> var onloadCallback = function() { }; ' +
'var onDataCallback = function(response) { window.postMessage(response); }; ' +
'var onDataExpiredCallback = function(error) { window.postMessage("expired"); }; ' +
'var onDataErrorCallback = function(error) { window.postMessage("error"); } </script>' +
'</head><body>' +
'<div class="text-xs-center"><div class="g-recaptcha" ' +
'data-sitekey="' + siteKey + '" data-callback="onDataCallback" ' +
'data-expired-callback="onDataExpiredCallback" ' +
'data-error-callback="onDataErrorCallback"></div></div></body></html>';
return originalForm;
}
const ReCaptchaView = ({
onMessage,
siteKey,
containStyle,
title,
description,
url
}) => (
<WebView
ref={(ref) => { this.webview = ref; }}
mixedContentMode={'always'}
onMessage={onMessage}
javaScriptEnabled
injectedJavaScript={patchPostMessageJsCode}
automaticallyAdjustContentInsets
style={[{ backgroundColor: 'transparent', height: 500 }]}
source={{
html: getWebviewContent(siteKey),
baseUrl: url
}}
/>
);
ReCaptchaView.propTypes = {
onMessage: PropTypes.func,
siteKey: PropTypes.string.isRequired,
containStyle: PropTypes.any,
url: PropTypes.string
};
ReCaptchaView.defaultProps = {
autoHeight: true,
onMessage: (event) => null,
title: '',
description: '',
url: ''
};
export default ReCaptchaView;