jsloader.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview A utility to load JavaScript files via DOM script tags.
  16. * Refactored from goog.net.Jsonp. Works cross-domain.
  17. *
  18. */
  19. goog.provide('goog.net.jsloader');
  20. goog.provide('goog.net.jsloader.Error');
  21. goog.provide('goog.net.jsloader.ErrorCode');
  22. goog.provide('goog.net.jsloader.Options');
  23. goog.require('goog.array');
  24. goog.require('goog.async.Deferred');
  25. goog.require('goog.debug.Error');
  26. goog.require('goog.dom');
  27. goog.require('goog.dom.TagName');
  28. goog.require('goog.dom.safe');
  29. goog.require('goog.html.TrustedResourceUrl');
  30. goog.require('goog.html.legacyconversions');
  31. goog.require('goog.object');
  32. /**
  33. * The name of the property of goog.global under which the JavaScript
  34. * verification object is stored by the loaded script.
  35. * @private {string}
  36. */
  37. goog.net.jsloader.GLOBAL_VERIFY_OBJS_ = 'closure_verification';
  38. /**
  39. * The default length of time, in milliseconds, we are prepared to wait for a
  40. * load request to complete.
  41. * @type {number}
  42. */
  43. goog.net.jsloader.DEFAULT_TIMEOUT = 5000;
  44. /**
  45. * Optional parameters for goog.net.jsloader.send.
  46. * timeout: The length of time, in milliseconds, we are prepared to wait
  47. * for a load request to complete, or 0 or negative for no timeout. Default
  48. * is 5 seconds.
  49. * document: The HTML document under which to load the JavaScript. Default is
  50. * the current document.
  51. * cleanupWhenDone: If true clean up the script tag after script completes to
  52. * load. This is important if you just want to read data from the JavaScript
  53. * and then throw it away. Default is false.
  54. * attributes: Additional attributes to set on the script tag.
  55. *
  56. * @typedef {{
  57. * timeout: (number|undefined),
  58. * document: (HTMLDocument|undefined),
  59. * cleanupWhenDone: (boolean|undefined),
  60. * attributes: (!Object<string, string>|undefined)
  61. * }}
  62. */
  63. goog.net.jsloader.Options;
  64. /**
  65. * Scripts (URIs) waiting to be loaded.
  66. * @private {!Array<!goog.html.TrustedResourceUrl>}
  67. */
  68. goog.net.jsloader.scriptsToLoad_ = [];
  69. /**
  70. * The deferred result of loading the URIs in scriptsToLoad_.
  71. * We need to return this to a caller that wants to load URIs while
  72. * a deferred is already working on them.
  73. * @private {!goog.async.Deferred<null>}
  74. */
  75. goog.net.jsloader.scriptLoadingDeferred_;
  76. /**
  77. * This is deprecated, please use safeLoadMany.
  78. *
  79. * @param {Array<string>} uris The URIs to load.
  80. * @param {goog.net.jsloader.Options=} opt_options Optional parameters. See
  81. * goog.net.jsloader.options documentation for details.
  82. * @return {!goog.async.Deferred} The deferred result, that may be used to add
  83. * callbacks
  84. * @deprecated
  85. */
  86. goog.net.jsloader.loadMany = function(uris, opt_options) {
  87. var trustedUris = goog.array.map(
  88. uris, goog.html.legacyconversions.trustedResourceUrlFromString);
  89. return goog.net.jsloader.safeLoadMany(trustedUris, opt_options);
  90. };
  91. /**
  92. * Loads and evaluates the JavaScript files at the specified URIs, guaranteeing
  93. * the order of script loads.
  94. *
  95. * Because we have to load the scripts in serial (load script 1, exec script 1,
  96. * load script 2, exec script 2, and so on), this will be slower than doing
  97. * the network fetches in parallel.
  98. *
  99. * If you need to load a large number of scripts but dependency order doesn't
  100. * matter, you should just call goog.net.jsloader.load N times.
  101. *
  102. * If you need to load a large number of scripts on the same domain,
  103. * you may want to use goog.module.ModuleLoader.
  104. *
  105. * @param {Array<!goog.html.TrustedResourceUrl>} trustedUris The URIs to load.
  106. * @param {goog.net.jsloader.Options=} opt_options Optional parameters. See
  107. * goog.net.jsloader.options documentation for details.
  108. * @return {!goog.async.Deferred} The deferred result, that may be used to add
  109. * callbacks
  110. */
  111. goog.net.jsloader.safeLoadMany = function(trustedUris, opt_options) {
  112. // Loading the scripts in serial introduces asynchronosity into the flow.
  113. // Therefore, there are race conditions where client A can kick off the load
  114. // sequence for client B, even though client A's scripts haven't all been
  115. // loaded yet.
  116. //
  117. // To work around this issue, all module loads share a queue.
  118. if (!trustedUris.length) {
  119. return goog.async.Deferred.succeed(null);
  120. }
  121. var isAnotherModuleLoading = goog.net.jsloader.scriptsToLoad_.length;
  122. goog.array.extend(goog.net.jsloader.scriptsToLoad_, trustedUris);
  123. if (isAnotherModuleLoading) {
  124. // jsloader is still loading some other scripts.
  125. // In order to prevent the race condition noted above, we just add
  126. // these URIs to the end of the scripts' queue and return the deferred
  127. // result of the ongoing script load, so the caller knows when they
  128. // finish loading.
  129. return goog.net.jsloader.scriptLoadingDeferred_;
  130. }
  131. trustedUris = goog.net.jsloader.scriptsToLoad_;
  132. var popAndLoadNextScript = function() {
  133. var trustedUri = trustedUris.shift();
  134. var deferred = goog.net.jsloader.safeLoad(trustedUri, opt_options);
  135. if (trustedUris.length) {
  136. deferred.addBoth(popAndLoadNextScript);
  137. }
  138. return deferred;
  139. };
  140. goog.net.jsloader.scriptLoadingDeferred_ = popAndLoadNextScript();
  141. return goog.net.jsloader.scriptLoadingDeferred_;
  142. };
  143. /**
  144. * This is deprecated, please use safeLoad instead.
  145. *
  146. * @param {string} uri The URI of the JavaScript.
  147. * @param {goog.net.jsloader.Options=} opt_options Optional parameters. See
  148. * goog.net.jsloader.Options documentation for details.
  149. * @return {!goog.async.Deferred} The deferred result, that may be used to add
  150. * callbacks and/or cancel the transmission.
  151. * The error callback will be called with a single goog.net.jsloader.Error
  152. * parameter.
  153. * @deprecated Use safeLoad instead.
  154. */
  155. goog.net.jsloader.load = function(uri, opt_options) {
  156. var trustedUri =
  157. goog.html.legacyconversions.trustedResourceUrlFromString(uri);
  158. return goog.net.jsloader.safeLoad(trustedUri, opt_options);
  159. };
  160. /**
  161. * Loads and evaluates a JavaScript file.
  162. * When the script loads, a user callback is called.
  163. * It is the client's responsibility to verify that the script ran successfully.
  164. *
  165. * @param {!goog.html.TrustedResourceUrl} trustedUri The URI of the JavaScript.
  166. * @param {goog.net.jsloader.Options=} opt_options Optional parameters. See
  167. * goog.net.jsloader.Options documentation for details.
  168. * @return {!goog.async.Deferred} The deferred result, that may be used to add
  169. * callbacks and/or cancel the transmission.
  170. * The error callback will be called with a single goog.net.jsloader.Error
  171. * parameter.
  172. */
  173. goog.net.jsloader.safeLoad = function(trustedUri, opt_options) {
  174. var options = opt_options || {};
  175. var doc = options.document || document;
  176. var uri = goog.html.TrustedResourceUrl.unwrap(trustedUri);
  177. var script = goog.dom.createElement(goog.dom.TagName.SCRIPT);
  178. var request = {script_: script, timeout_: undefined};
  179. var deferred = new goog.async.Deferred(goog.net.jsloader.cancel_, request);
  180. // Set a timeout.
  181. var timeout = null;
  182. var timeoutDuration = goog.isDefAndNotNull(options.timeout) ?
  183. options.timeout :
  184. goog.net.jsloader.DEFAULT_TIMEOUT;
  185. if (timeoutDuration > 0) {
  186. timeout = window.setTimeout(function() {
  187. goog.net.jsloader.cleanup_(script, true);
  188. deferred.errback(
  189. new goog.net.jsloader.Error(
  190. goog.net.jsloader.ErrorCode.TIMEOUT,
  191. 'Timeout reached for loading script ' + uri));
  192. }, timeoutDuration);
  193. request.timeout_ = timeout;
  194. }
  195. // Hang the user callback to be called when the script completes to load.
  196. // NOTE(user): This callback will be called in IE even upon error. In any
  197. // case it is the client's responsibility to verify that the script ran
  198. // successfully.
  199. script.onload = script.onreadystatechange = function() {
  200. if (!script.readyState || script.readyState == 'loaded' ||
  201. script.readyState == 'complete') {
  202. var removeScriptNode = options.cleanupWhenDone || false;
  203. goog.net.jsloader.cleanup_(script, removeScriptNode, timeout);
  204. deferred.callback(null);
  205. }
  206. };
  207. // Add an error callback.
  208. // NOTE(user): Not supported in IE.
  209. script.onerror = function() {
  210. goog.net.jsloader.cleanup_(script, true, timeout);
  211. deferred.errback(
  212. new goog.net.jsloader.Error(
  213. goog.net.jsloader.ErrorCode.LOAD_ERROR,
  214. 'Error while loading script ' + uri));
  215. };
  216. var properties = options.attributes || {};
  217. goog.object.extend(
  218. properties, {'type': 'text/javascript', 'charset': 'UTF-8'});
  219. goog.dom.setProperties(script, properties);
  220. // NOTE(user): Safari never loads the script if we don't set the src
  221. // attribute before appending.
  222. goog.dom.safe.setScriptSrc(script, trustedUri);
  223. var scriptParent = goog.net.jsloader.getScriptParentElement_(doc);
  224. scriptParent.appendChild(script);
  225. return deferred;
  226. };
  227. /**
  228. * This function is deprecated, please use safeLoadAndVerify instead.
  229. *
  230. * @param {string} uri The URI of the JavaScript.
  231. * @param {string} verificationObjName The name of the verification object that
  232. * the loaded script should set.
  233. * @param {goog.net.jsloader.Options} options Optional parameters. See
  234. * goog.net.jsloader.Options documentation for details.
  235. * @return {!goog.async.Deferred} The deferred result, that may be used to add
  236. * callbacks and/or cancel the transmission.
  237. * The success callback will be called with a single parameter containing
  238. * the value of the verification object.
  239. * The error callback will be called with a single goog.net.jsloader.Error
  240. * parameter.
  241. * @deprecated
  242. */
  243. goog.net.jsloader.loadAndVerify = function(uri, verificationObjName, options) {
  244. var trustedUri =
  245. goog.html.legacyconversions.trustedResourceUrlFromString(uri);
  246. return goog.net.jsloader.safeLoadAndVerify(
  247. trustedUri, verificationObjName, options);
  248. };
  249. /**
  250. * Loads a JavaScript file and verifies it was evaluated successfully, using a
  251. * verification object.
  252. * The verification object is set by the loaded JavaScript at the end of the
  253. * script.
  254. * We verify this object was set and return its value in the success callback.
  255. * If the object is not defined we trigger an error callback.
  256. *
  257. * @param {!goog.html.TrustedResourceUrl} trustedUri The URI of the JavaScript.
  258. * @param {string} verificationObjName The name of the verification object that
  259. * the loaded script should set.
  260. * @param {goog.net.jsloader.Options} options Optional parameters. See
  261. * goog.net.jsloader.Options documentation for details.
  262. * @return {!goog.async.Deferred} The deferred result, that may be used to add
  263. * callbacks and/or cancel the transmission.
  264. * The success callback will be called with a single parameter containing
  265. * the value of the verification object.
  266. * The error callback will be called with a single goog.net.jsloader.Error
  267. * parameter.
  268. */
  269. goog.net.jsloader.safeLoadAndVerify = function(
  270. trustedUri, verificationObjName, options) {
  271. // Define the global objects variable.
  272. if (!goog.global[goog.net.jsloader.GLOBAL_VERIFY_OBJS_]) {
  273. goog.global[goog.net.jsloader.GLOBAL_VERIFY_OBJS_] = {};
  274. }
  275. var verifyObjs = goog.global[goog.net.jsloader.GLOBAL_VERIFY_OBJS_];
  276. var uri = goog.html.TrustedResourceUrl.unwrap(trustedUri);
  277. // Verify that the expected object does not exist yet.
  278. if (goog.isDef(verifyObjs[verificationObjName])) {
  279. // TODO(user): Error or reset variable?
  280. return goog.async.Deferred.fail(
  281. new goog.net.jsloader.Error(
  282. goog.net.jsloader.ErrorCode.VERIFY_OBJECT_ALREADY_EXISTS,
  283. 'Verification object ' + verificationObjName +
  284. ' already defined.'));
  285. }
  286. // Send request to load the JavaScript.
  287. var sendDeferred = goog.net.jsloader.safeLoad(trustedUri, options);
  288. // Create a deferred object wrapping the send result.
  289. var deferred =
  290. new goog.async.Deferred(goog.bind(sendDeferred.cancel, sendDeferred));
  291. // Call user back with object that was set by the script.
  292. sendDeferred.addCallback(function() {
  293. var result = verifyObjs[verificationObjName];
  294. if (goog.isDef(result)) {
  295. deferred.callback(result);
  296. delete verifyObjs[verificationObjName];
  297. } else {
  298. // Error: script was not loaded properly.
  299. deferred.errback(
  300. new goog.net.jsloader.Error(
  301. goog.net.jsloader.ErrorCode.VERIFY_ERROR, 'Script ' + uri +
  302. ' loaded, but verification object ' + verificationObjName +
  303. ' was not defined.'));
  304. }
  305. });
  306. // Pass error to new deferred object.
  307. sendDeferred.addErrback(function(error) {
  308. if (goog.isDef(verifyObjs[verificationObjName])) {
  309. delete verifyObjs[verificationObjName];
  310. }
  311. deferred.errback(error);
  312. });
  313. return deferred;
  314. };
  315. /**
  316. * Gets the DOM element under which we should add new script elements.
  317. * How? Take the first head element, and if not found take doc.documentElement,
  318. * which always exists.
  319. *
  320. * @param {!HTMLDocument} doc The relevant document.
  321. * @return {!Element} The script parent element.
  322. * @private
  323. */
  324. goog.net.jsloader.getScriptParentElement_ = function(doc) {
  325. var headElements = goog.dom.getElementsByTagName(goog.dom.TagName.HEAD, doc);
  326. if (!headElements || goog.array.isEmpty(headElements)) {
  327. return doc.documentElement;
  328. } else {
  329. return headElements[0];
  330. }
  331. };
  332. /**
  333. * Cancels a given request.
  334. * @this {{script_: Element, timeout_: number}} The request context.
  335. * @private
  336. */
  337. goog.net.jsloader.cancel_ = function() {
  338. var request = this;
  339. if (request && request.script_) {
  340. var scriptNode = request.script_;
  341. if (scriptNode && scriptNode.tagName == goog.dom.TagName.SCRIPT) {
  342. goog.net.jsloader.cleanup_(scriptNode, true, request.timeout_);
  343. }
  344. }
  345. };
  346. /**
  347. * Removes the script node and the timeout.
  348. *
  349. * @param {Node} scriptNode The node to be cleaned up.
  350. * @param {boolean} removeScriptNode If true completely remove the script node.
  351. * @param {?number=} opt_timeout The timeout handler to cleanup.
  352. * @private
  353. */
  354. goog.net.jsloader.cleanup_ = function(
  355. scriptNode, removeScriptNode, opt_timeout) {
  356. if (goog.isDefAndNotNull(opt_timeout)) {
  357. goog.global.clearTimeout(opt_timeout);
  358. }
  359. scriptNode.onload = goog.nullFunction;
  360. scriptNode.onerror = goog.nullFunction;
  361. scriptNode.onreadystatechange = goog.nullFunction;
  362. // Do this after a delay (removing the script node of a running script can
  363. // confuse older IEs).
  364. if (removeScriptNode) {
  365. window.setTimeout(function() { goog.dom.removeNode(scriptNode); }, 0);
  366. }
  367. };
  368. /**
  369. * Possible error codes for jsloader.
  370. * @enum {number}
  371. */
  372. goog.net.jsloader.ErrorCode = {
  373. LOAD_ERROR: 0,
  374. TIMEOUT: 1,
  375. VERIFY_ERROR: 2,
  376. VERIFY_OBJECT_ALREADY_EXISTS: 3
  377. };
  378. /**
  379. * A jsloader error.
  380. *
  381. * @param {goog.net.jsloader.ErrorCode} code The error code.
  382. * @param {string=} opt_message Additional message.
  383. * @constructor
  384. * @extends {goog.debug.Error}
  385. * @final
  386. */
  387. goog.net.jsloader.Error = function(code, opt_message) {
  388. var msg = 'Jsloader error (code #' + code + ')';
  389. if (opt_message) {
  390. msg += ': ' + opt_message;
  391. }
  392. goog.net.jsloader.Error.base(this, 'constructor', msg);
  393. /**
  394. * The code for this error.
  395. *
  396. * @type {goog.net.jsloader.ErrorCode}
  397. */
  398. this.code = code;
  399. };
  400. goog.inherits(goog.net.jsloader.Error, goog.debug.Error);