[All][CB-4016] plugin loading uses script injection to load cordova_plugins.js (cherry picked from commit 0ce47184000f9187654e1532265b9c9ae0c93330)
Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/af01151c Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/af01151c Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/af01151c Branch: refs/heads/2.9.x Commit: af01151cba7824d974e0277cb48dd7d1d241a962 Parents: 931e771 Author: Jesse MacFadyen <[email protected]> Authored: Tue Jul 9 18:43:26 2013 -0700 Committer: Andrew Grieve <[email protected]> Committed: Tue Oct 22 11:35:13 2013 -0400 ---------------------------------------------------------------------- lib/common/pluginloader.js | 38 +++++--------------------------------- 1 file changed, 5 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-js/blob/af01151c/lib/common/pluginloader.js ---------------------------------------------------------------------- diff --git a/lib/common/pluginloader.js b/lib/common/pluginloader.js index f160312..4b5955d 100644 --- a/lib/common/pluginloader.js +++ b/lib/common/pluginloader.js @@ -33,11 +33,12 @@ function scriptLoadedCallback() { } // Helper function to inject a <script> tag. -function injectScript(url, onload) { +function injectScript(url, onload, onerror) { scriptCounter++; var script = document.createElement("script"); // onload fires even when script fails loads with an error. script.onload = onload; + script.onerror = onerror; script.src = url; document.head.appendChild(script); } @@ -104,7 +105,7 @@ function injectPluginScript(pathPrefix) { // this is an acceptable error, pre-3.0.0, so we just move on. finishPluginLoading(); } - }); + },finishPluginLoading); // also, add script load error handler for file not found } function findCordovaPath() { @@ -127,38 +128,9 @@ function findCordovaPath() { exports.load = function() { var pathPrefix = findCordovaPath(); if (pathPrefix === null) { - console.warn('Could not find cordova.js script tag. Plugin loading may fail.'); + console.log('Could not find cordova.js script tag. Plugin loading may fail.'); pathPrefix = ''; } - - // 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) { - moduleList = obj; - handlePluginsObject(pathPrefix); - } else { - finishPluginLoading(); - } - }; - xhr.onerror = function() { - // 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 - injectPluginScript(pathPrefix); - }; - try { - xhr.open('GET', pathPrefix + 'cordova_plugins.json', true); // Async - xhr.send(); - } catch(err){ - injectPluginScript(pathPrefix); - } + injectPluginScript(pathPrefix); };
