forked from acsant/react-native-recaptcha
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MessageWebView.js
49 lines (43 loc) · 1.51 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
import React from 'react'
import { View } from 'react-native'
import { WebView } from 'react-native-webview'
// 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;
}
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>
)
}
}