Browse Source

Added code scoping fix. General codestyle fixes and optimizations

Vladimir Panov 4 years ago
parent
commit
6730392446
1 changed files with 25 additions and 22 deletions
  1. 25
    22
      autoHeightWebView/utils.js

+ 25
- 22
autoHeightWebView/utils.js View File

@@ -13,9 +13,6 @@ observer.observe(document, {
13 13
 
14 14
 const updateSizeWithMessage = element =>
15 15
   `
16
-  var updateSizeInterval = null;
17
-  var height = 0;
18
-
19 16
   var lastHeight = 0;
20 17
   var heightTheSameTimes = 0;
21 18
   var maxHeightTheSameTimes = 5;
@@ -23,13 +20,17 @@ const updateSizeWithMessage = element =>
23 20
   var forceRefreshTimeout;
24 21
 
25 22
   function updateSize(event) {
26
-    if (!window.hasOwnProperty('ReactNativeWebView') || !window.ReactNativeWebView.hasOwnProperty('postMessage')) {
27
-      !updateSizeInterval && (updateSizeInterval = setInterval(updateSize, 200));
23
+    if (
24
+      !window.hasOwnProperty('ReactNativeWebView') || 
25
+      !window.ReactNativeWebView.hasOwnProperty('postMessage')
26
+    ) {
27
+      setTimeout(updateSize, 200);
28 28
       return;
29 29
     }
30
-    clearInterval(updateSizeInterval)
31
-    height = ${element}.offsetHeight || window.innerHeight;
32
-    width = ${element}.offsetWidth || window.innerWidth;
30
+    
31
+    var height = ${element}.offsetHeight || window.innerHeight;
32
+    var width = ${element}.offsetWidth || window.innerWidth;
33
+
33 34
     window.ReactNativeWebView.postMessage(JSON.stringify({ width: width, height: height }));
34 35
 
35 36
     // Make additional height checks (required to fix issues wit twitter embeds)
@@ -71,7 +72,6 @@ const getBaseScript = ({ style, zoomable }) =>
71 72
     }
72 73
     document.body.appendChild(wrapper);
73 74
   }
74
-  var width = ${getWidth(style)};
75 75
   ${updateSizeWithMessage('wrapper')}
76 76
   window.addEventListener('load', updateSize);
77 77
   window.addEventListener('resize', updateSize);
@@ -84,13 +84,13 @@ const appendFilesToHead = ({ files, script }) =>
84 84
   files.reduceRight((combinedScript, file) => {
85 85
     const { rel, type, href } = file;
86 86
     return `
87
-          var link  = document.createElement('link');
88
-          link.rel  = '${rel}';
89
-          link.type = '${type}';
90
-          link.href = '${href}';
91
-          document.head.appendChild(link);
92
-          ${combinedScript}
93
-        `;
87
+      var link  = document.createElement('link');
88
+      link.rel  = '${rel}';
89
+      link.type = '${type}';
90
+      link.href = '${href}';
91
+      document.head.appendChild(link);
92
+      ${combinedScript}
93
+    `;
94 94
   }, script);
95 95
 
96 96
 const screenWidth = Dimensions.get('window').width;
@@ -107,17 +107,20 @@ const appendStylesToHead = ({ style, script }) => {
107 107
   // Escape any single quotes or newlines in the CSS with .replace()
108 108
   const escaped = currentStyles.replace(/\'/g, "\\'").replace(/\n/g, '\\n');
109 109
   return `
110
-          var styleElement = document.createElement('style');
111
-          styleElement.innerHTML = '${escaped}';
112
-          document.head.appendChild(styleElement);
113
-          ${script}
114
-        `;
110
+    var styleElement = document.createElement('style');
111
+    styleElement.innerHTML = '${escaped}';
112
+    document.head.appendChild(styleElement);
113
+    ${script}
114
+  `;
115 115
 };
116 116
 
117 117
 const getInjectedSource = ({ html, script }) => `
118 118
 ${html}
119 119
 <script>
120
-${script}
120
+// prevents code colissions with global scope
121
+(() => {
122
+  ${script}
123
+})();
121 124
 </script>
122 125
 `;
123 126