forked from acsant/react-native-recaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageWebView.js
More file actions
40 lines (35 loc) · 1.43 KB
/
MessageWebView.js
File metadata and controls
40 lines (35 loc) · 1.43 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
import React, { Component } 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 () {
const originalPostMessage = window.postMessage;
const patchedPostMessage = (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 Component {
postMessage = (action) => {
this.WebView.postMessage(JSON.stringify(action));
};
getWebViewHandle = () => {
return this.webview;
};
render() {
const { containerStyle, html, source, url, onMessage, ...props } = this.props;
return <View style={containerStyle}>
<WebView {...props}
style={containerStyle}
javaScriptEnabled={true}
automaticallyAdjustContentInsets={true}
injectedJavaScript={patchPostMessageJsCode}
source={source ? source : html ? { html } : url}
ref={(x) => this.webview = x}
onMessage={e => onMessage(e.nativeEvent.data)}/>
</View>;
}
}