Browse Source

Merge pull request #40 from calvinkei/master

Add: DOM change detection
iou90 6 years ago
parent
commit
77a02cfa10
No account linked to committer's email address
2 changed files with 34 additions and 6 deletions
  1. 19
    3
      autoHeightWebView/index.android.js
  2. 15
    3
      autoHeightWebView/index.ios.js

+ 19
- 3
autoHeightWebView/index.android.js View File

@@ -23,7 +23,7 @@ import Immutable from "immutable";
23 23
 const RCTAutoHeightWebView = requireNativeComponent(
24 24
   "RCTAutoHeightWebView",
25 25
   AutoHeightWebView,
26
-  { nativeOnly: 
26
+  { nativeOnly:
27 27
     {
28 28
       nativeOnly: {
29 29
         onLoadingStart: true,
@@ -343,12 +343,28 @@ const BaseScript = IsBelowKitKat
343 343
         AutoHeightWebView.onMessage = function (message) {
344 344
             AutoHeightWebView.send(String(document.body.offsetHeight));
345 345
         };
346
-    } ()); 
346
+        MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
347
+        var observer = new MutationObserver(function() {
348
+            AutoHeightWebView.send(String(document.body.offsetHeight));
349
+        });
350
+        observer.observe(document, {
351
+            subtree: true,
352
+            attributes: true
353
+        });
354
+    } ());
347 355
     `
348 356
   : `
349 357
     ; (function () {
350 358
         document.addEventListener('message', function (e) {
351 359
             window.postMessage(String(document.body.offsetHeight));
352 360
         });
353
-    } ()); 
361
+        MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
362
+        var observer = new MutationObserver(function() {
363
+            window.postMessage(String(document.body.offsetHeight));
364
+        });
365
+        observer.observe(document, {
366
+            subtree: true,
367
+            attributes: true
368
+        });
369
+    } ());
354 370
     `;

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

@@ -175,7 +175,7 @@ const Styles = StyleSheet.create({
175 175
 
176 176
 const BaseScript =
177 177
     `
178
-    ; 
178
+    ;
179 179
     (function () {
180 180
         var i = 0;
181 181
         var height = 0;
@@ -195,12 +195,18 @@ const BaseScript =
195 195
         updateHeight();
196 196
         window.addEventListener('load', updateHeight);
197 197
         window.addEventListener('resize', updateHeight);
198
+        MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
199
+        var observer = new MutationObserver(updateHeight);
200
+        observer.observe(document, {
201
+            subtree: true,
202
+            attributes: true
203
+        });
198 204
     } ());
199 205
     `;
200 206
 
201 207
 const IframeBaseScript =
202 208
     `
203
-    ; 
209
+    ;
204 210
     (function () {
205 211
         var i = 0;
206 212
         var height = 0;
@@ -214,5 +220,11 @@ const IframeBaseScript =
214 220
         updateHeight();
215 221
         window.addEventListener('load', updateHeight);
216 222
         window.addEventListener('resize', updateHeight);
223
+        MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
224
+        var observer = new MutationObserver(updateHeight);
225
+        observer.observe(document, {
226
+            subtree: true,
227
+            attributes: true
228
+        });
217 229
     } ());
218
-    `;
230
+    `;