iou90 5 years ago
parent
commit
68c1dcff7b
4 changed files with 25 additions and 15 deletions
  1. 4
    0
      autoHeightWebView/index.android.js
  2. 17
    15
      autoHeightWebView/index.ios.js
  3. 3
    0
      index.js
  4. 1
    0
      package.json

+ 4
- 0
autoHeightWebView/index.android.js View File

@@ -217,6 +217,10 @@ export default class AutoHeightWebView extends PureComponent {
217 217
     if (!e.nativeEvent) {
218 218
       return;
219 219
     }
220
+    
221
+    // Sometimes the message is invalid JSON, so we ignore that case
222
+    try { JSON.parse(isBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data) } catch(e) { return }
223
+
220 224
     const { height, width } = JSON.parse(isBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data);
221 225
     const { height: oldHeight, width: oldWidth } = this.state;
222 226
     if (isSizeChanged(height, oldHeight, width, oldWidth)) {

+ 17
- 15
autoHeightWebView/index.ios.js View File

@@ -205,24 +205,26 @@ function getBaseScript(style) {
205 205
     ;
206 206
     ${getCurrentSize}
207 207
     (function () {
208
-        var height = 0;
209
-        var width = ${getWidth(style)};
210
-        var wrapper = document.createElement('div');
211
-        wrapper.id = 'rnahw-wrapper';
212
-        while (document.body.firstChild instanceof Node) {
213
-            wrapper.appendChild(document.body.firstChild);
214
-        }
215
-        document.body.appendChild(wrapper);
216
-        function updateSize() {
217
-            if(document.body.offsetHeight !== height || document.body.offsetWidth !== width) {
218
-                var size = getSize(wrapper);
219
-                height = size.height;
220
-                width = size.width;
221
-                document.title = height.toString() + ',' + width.toString();
208
+      if (!document.getElementById("rnahw-wrapper")) { 
209
+          var height = 0;
210
+          var width = ${getWidth(style)};
211
+          var wrapper = document.createElement('div');
212
+          wrapper.id = 'rnahw-wrapper';
213
+          while (document.body.firstChild instanceof Node) {
214
+              wrapper.appendChild(document.body.firstChild);
215
+          }
216
+          document.body.appendChild(wrapper);
217
+          function updateSize() {
218
+            if (document.body.offsetHeight !== height || document.body.offsetWidth !== width) {
219
+               var size = getSize(wrapper);
220
+               height = size.height;
221
+               width = size.width;
222
+               document.title = height.toString() + ',' + width.toString();
222 223
             }
223
-        }
224
+          }
224 225
         ${commonScript}
225 226
         ${domMutationObserveScript}
227
+        }
226 228
     } ());
227 229
     `;
228 230
 }

+ 3
- 0
index.js View File

@@ -0,0 +1,3 @@
1
+var ReactNative = require('react-native')
2
+module.exports = ReactNative.NativeModules.AutoHeightWebView
3
+

+ 1
- 0
package.json View File

@@ -4,6 +4,7 @@
4 4
   "description": "An auto height webview for React Native",
5 5
   "main": "autoHeightWebView",
6 6
   "files": [
7
+    "index.js",
7 8
     "android",
8 9
     "autoHeightWebView"
9 10
   ],