forked from acsant/react-native-recaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageWebView.js
56 lines (50 loc) · 1.47 KB
/
MessageWebView.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
54
55
56
import React from 'react';
import { WebView, View } from 'react-native';
// 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;
})})();`;
export default class MessageWebView extends React.Component {
constructor(props) {
super(props);
this.postMessage = this.postMessage.bind(this);
}
postMessage(action) {
this.WebView.postMessage(JSON.stringify(action));
}
getWebViewHandle = () => {
return this.webview;
};
getToken = () => {
this.webview.postMessage('get token');
};
render() {
const { html, source, url, onMessage, ...props } = this.props;
return (
<View style={props.containerStyle}>
<WebView
{...props}
style={props.containerStyle}
javaScriptEnabled
automaticallyAdjustContentInsets
injectedJavaScript={patchPostMessageJsCode}
source={source ? source : html ? { html } : url}
ref={x => {
this.webview = x;
}}
onMessage={e => onMessage(e.nativeEvent.data)}
/>
</View>
);
}
}