forked from scazzy/react-native-webview-autoheight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (109 loc) · 2.93 KB
/
index.js
File metadata and controls
120 lines (109 loc) · 2.93 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Custom WebView with autoHeight feature
*
* @prop source: Same as WebView
* @prop autoHeight: true|false
* @prop defaultHeight: 100
* @prop width: device Width
* @prop ...props
*
* @author Elton Jain
* @version v1.0.2
*/
import React, { Component } from "react";
import { Platform, Dimensions, WebView } from "react-native";
const injectedIOSScript = function() {
function waitForBridge() {
if (window.postMessage.length !== 1) {
setTimeout(waitForBridge, 200);
} else {
let height = 0;
if (document.documentElement.clientHeight > document.body.clientHeight) {
height = document.documentElement.clientHeight;
} else {
height = document.body.clientHeight;
}
postMessage(height);
}
}
waitForBridge();
};
const injectedAndroidScript = function() {
// Modification start
const originalPostMessage = window.postMessage;
const patchedPostMessage = function(message, targetOrigin, transfer) {
originalPostMessage(message, targetOrigin, transfer);
};
patchedPostMessage.toString = function() {
return String(Object.hasOwnProperty).replace(
"hasOwnProperty",
"postMessage"
);
};
window.postMessage = patchedPostMessage;
// Modification end
function waitForBridge() {
if (window.postMessage.length !== 1) {
setTimeout(waitForBridge, 200);
} else {
let height = 0;
if (document.documentElement.clientHeight > document.body.clientHeight) {
height = document.documentElement.clientHeight;
} else {
height = document.body.clientHeight;
}
postMessage(height);
}
}
waitForBridge();
};
export default class MyWebView extends Component {
state = {
webViewHeight: Number
};
static defaultProps = {
autoHeight: true
};
constructor(props: Object) {
super(props);
this.state = {
webViewHeight: this.props.defaultHeight
};
this._onMessage = this._onMessage.bind(this);
}
_onMessage(e) {
this.setState({
webViewHeight: parseInt(e.nativeEvent.data)
});
}
stopLoading() {
this.webview.stopLoading();
}
render() {
const _w = this.props.width || Dimensions.get("window").width;
const _h = this.props.autoHeight
? this.state.webViewHeight
: this.props.defaultHeight;
return (
<WebView
ref={ref => {
this.webview = ref;
}}
injectedJavaScript={
"(" +
String(
Platform.OS === "ios" ? injectedIOSScript : injectedAndroidScript
) +
")();" +
"window.postMessage = String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage');"
}
scrollEnabled={this.props.scrollEnabled || false}
onMessage={this._onMessage}
javaScriptEnabled={true}
automaticallyAdjustContentInsets={true}
{...this.props}
style={[{ width: _w }, this.props.style, { height: _h }]}
/>
);
}
}