-
Notifications
You must be signed in to change notification settings - Fork 48
/
MessageWebView.js
48 lines (42 loc) · 1.48 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
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;
}
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>
)
}
}