diff --git a/index.js b/index.js index 3c52a68..06f4bcf 100644 --- a/index.js +++ b/index.js @@ -15,23 +15,40 @@ import React, { Component } from 'react'; import { View, Dimensions, - WebView, Platform, } from 'react-native'; +import { WebView } from 'react-native-webview'; -const injectedScript = function() { - function waitForBridge() { - if (window.postMessage.length !== 1){ - setTimeout(waitForBridge, 200); - } - else { - postMessage( - Math.max(document.documentElement.clientHeight, document.documentElement.scrollHeight, document.body.clientHeight, document.body.scrollHeight) - ) - } + +const injectedScript = `function() { + function postSize() { + //https://stackoverflow.com/questions/1145850/how-to-get-height-of-entire-document-with-javascript + var body = document.body, html = document.documentElement; + + var maxHeight = Math.max( body.scrollHeight, body.offsetHeight, html.offsetHeight ); + + console.log('postSize maxHeight', maxHeight) + window.ReactNativeWebView.postMessage(maxHeight); + } + + var postSizeTimeout; + function debouncedPostSize() { + clearTimeout(postSizeTimeout); + postSizeTimeout = setTimeout(function () { + postSize() + }, 500) } - waitForBridge(); -}; + + debouncedPostSize(); + //trigger when DOM changes + var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; + var observer = new MutationObserver(debouncedPostSize); + observer.observe(document, { + subtree: true, + attributes: true + }); + window.addEventListener('resize', debouncedPostSize); +}` export default class MyWebView extends Component { state = { @@ -40,6 +57,7 @@ export default class MyWebView extends Component { static defaultProps = { autoHeight: true, + noWidth: false, } constructor (props: Object) { @@ -52,9 +70,12 @@ export default class MyWebView extends Component { } _onMessage(e) { - this.setState({ - webViewHeight: parseInt(e.nativeEvent.data) - }); + const maxHeight = this.props.maxHeight; + let webViewHeight = parseInt(e.nativeEvent.data); + if (maxHeight && webViewHeight > maxHeight) { + webViewHeight = maxHeight + } + this.setState({ webViewHeight }); } stopLoading() { @@ -66,21 +87,25 @@ export default class MyWebView extends Component { } render () { + const _w = this.props.width || Dimensions.get('window').width; const _h = this.props.autoHeight ? this.state.webViewHeight : this.props.defaultHeight; - const androidScript = 'window.postMessage = String(Object.hasOwnProperty).replace(\'hasOwnProperty\', \'postMessage\');' + - '(' + String(injectedScript) + ')();'; - const iosScript = '(' + String(injectedScript) + ')();' + 'window.postMessage = String(Object.hasOwnProperty).replace(\'hasOwnProperty\', \'postMessage\');'; + + const injectedJavaScript = '(' + String(injectedScript) + ')();'; + + let style = !!this.props.noWidth ? []: [{width: _w}] + style = style.concat([this.props.style, {height: _h}]); + return ( { this.webview = ref; }} - injectedJavaScript={Platform.OS === 'ios' ? iosScript : androidScript} + injectedJavaScript={injectedJavaScript} scrollEnabled={this.props.scrollEnabled || false} onMessage={this._onMessage} javaScriptEnabled={true} automaticallyAdjustContentInsets={true} {...this.props} - style={[{width: _w}, this.props.style, {height: _h}]} + style={style} /> ) } diff --git a/package.json b/package.json index 5d795fb..2e6422a 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,11 @@ "auto", "height" ], + "peerDependencies": { + "react": ">= 16.8.0", + "react-native": ">= 0.60.0", + "react-native-webview": ">= 10.9.0" + }, "author": "Elton Jain", "license": "ISC", "bugs": {