cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fil...@apache.org
Subject [3/3] js commit: Combine XHR + script injection technqieues to load the plugins list (either .json or .js file).
Date Wed, 19 Jun 2013 20:40:02 GMT
Combine XHR + script injection technqieues to load the plugins list (either .json or .js file).


Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/1fd26e16
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/1fd26e16
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/1fd26e16

Branch: refs/heads/pl
Commit: 1fd26e16edb07ede87d3f02373ffa6c3909a3076
Parents: a945b9d
Author: Fil Maj <maj.fil@gmail.com>
Authored: Wed Jun 19 13:39:40 2013 -0700
Committer: Fil Maj <maj.fil@gmail.com>
Committed: Wed Jun 19 13:39:40 2013 -0700

----------------------------------------------------------------------
 lib/scripts/plugin_loader.js | 58 +++++++++++++++++++++++++++++----------
 1 file changed, 43 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/1fd26e16/lib/scripts/plugin_loader.js
----------------------------------------------------------------------
diff --git a/lib/scripts/plugin_loader.js b/lib/scripts/plugin_loader.js
index b56f124..b27216b 100644
--- a/lib/scripts/plugin_loader.js
+++ b/lib/scripts/plugin_loader.js
@@ -119,24 +119,52 @@
         }
     }
 
+    var plugins_json = path + 'cordova_plugins.json';
     var plugins_js = path + 'cordova_plugins.js';
-    try {
-        
-        var script = document.createElement("script");
-        script.onload = function(){
-            var list = cordova.require("cordova/plugin_list");
-            handlePluginsObject(list,path);
-        };
-        script.onerror = function() {
-            // Error loading cordova_plugins.js, file not found or something
-            // this is an acceptable error, pre-3.0.0, so we just move on.
+    // Try to XHR the cordova_plugins.json file asynchronously.
+    var xhr = new XMLHttpRequest();
+    xhr.onload = function() {
+        // If the response is a JSON string which composes an array, call handlePluginsObject.
+        // If the request fails, or the response is not a JSON array, just call finishPluginLoading.
+        var obj;
+        try {
+            obj = (this.status == 0 || this.status == 200) && this.responseText &&
JSON.parse(this.responseText);
+        } catch (err) {
+            // obj will be undefined.
+        }
+        if (Array.isArray(obj) && obj.length > 0) {
+            handlePluginsObject(obj, path);
+        } else {
             finishPluginLoading();
-        };
-        script.src = plugins_js;
-        document.head.appendChild(script);
-
-    } catch(err){
+        }
+    };
+    xhr.onerror = function() {
         finishPluginLoading();
+    };
+    try { // we commented we were going to try, so let us actually try and catch
+        xhr.open('GET', plugins_json, true); // Async
+        xhr.send();
+    } catch(err){
+        // One some phones (Windows) this xhr.open throws an Access Denied exception
+        // So lets keep trying, but with a script tag injection technique instead of XHR
+        try {
+            
+            var script = document.createElement("script");
+            script.onload = function(){
+                var list = cordova.require("cordova/plugin_list");
+                handlePluginsObject(list,path);
+            };
+            script.onerror = function() {
+                // Error loading cordova_plugins.js, file not found or something
+                // this is an acceptable error, pre-3.0.0, so we just move on.
+                finishPluginLoading();
+            };
+            script.src = plugins_js;
+            document.head.appendChild(script);
+
+        } catch(err){
+            finishPluginLoading();
+        }
     }
 }(window));
 


Mime
View raw message