• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * @fileoverview Responsible for loading scripts into the inject context.
7 */
8
9goog.provide('cvox.InjectedScriptLoader');
10
11
12
13
14/** @constructor */
15cvox.InjectedScriptLoader = function() { };
16
17
18/**
19 * Loads a dictionary of file contents for Javascript files.
20 * @param {Array.<string>} files A list of file names.
21 * @param {function(Object.<string,string>)} done A function called when all
22 *     the files have been loaded. Called with the code map as the first
23 *     parameter.
24 */
25cvox.InjectedScriptLoader.fetchCode = function(files, done) {
26  var code = {};
27  var waiting = files.length;
28  var startTime = new Date();
29  var loadScriptAsCode = function(src) {
30      // Load the script by fetching its source and running 'eval' on it
31      // directly, with a magic comment that makes Chrome treat it like it
32      // loaded normally. Wait until it's fetched before loading the
33      // next script.
34      var xhr = new XMLHttpRequest();
35      var url = chrome.extension.getURL(src) + '?' + new Date().getTime();
36      xhr.onreadystatechange = function() {
37        if (xhr.readyState == 4) {
38          var scriptText = xhr.responseText;
39          // Add a magic comment to the bottom of the file so that
40          // Chrome knows the name of the script in the JavaScript debugger.
41          var debugSrc = src.replace('closure/../', '');
42          // The 'chromevox' id is only used in the DevTools instead of a long
43          // extension id.
44          scriptText += '\n//# sourceURL= chrome-extension://chromevox/' +
45              debugSrc + '\n';
46          code[src] = scriptText;
47          waiting--;
48          if (waiting == 0) {
49            done(code);
50          }
51        }
52      };
53      xhr.open('GET', url);
54      xhr.send(null);
55  }
56
57  files.forEach(function(f) { loadScriptAsCode(f); });
58};
59