You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2013/06/28 16:15:29 UTC

[05/26] js commit: Combine XHR + script injection technqieues to load the plugins list (either .json or .js file).

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/2.9.x
Commit: 1fd26e16edb07ede87d3f02373ffa6c3909a3076
Parents: a945b9d
Author: Fil Maj <ma...@gmail.com>
Authored: Wed Jun 19 13:39:40 2013 -0700
Committer: Fil Maj <ma...@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));