summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/chromeos/chromevox/chromevox/background/injected_script_loader.js
blob: f36f9907693ba3e0a33714ee1449142d262f7bae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Responsible for loading scripts into the inject context.
 */

goog.provide('cvox.InjectedScriptLoader');




/** @constructor */
cvox.InjectedScriptLoader = function() { };


/**
 * Loads a dictionary of file contents for Javascript files.
 * @param {Array.<string>} files A list of file names.
 * @param {function(Object.<string,string>)} done A function called when all
 *     the files have been loaded. Called with the code map as the first
 *     parameter.
 */
cvox.InjectedScriptLoader.fetchCode = function(files, done) {
  var code = {};
  var waiting = files.length;
  var startTime = new Date();
  var loadScriptAsCode = function(src) {
      // Load the script by fetching its source and running 'eval' on it
      // directly, with a magic comment that makes Chrome treat it like it
      // loaded normally. Wait until it's fetched before loading the
      // next script.
      var xhr = new XMLHttpRequest();
      var url = chrome.extension.getURL(src) + '?' + new Date().getTime();
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
          var scriptText = xhr.responseText;
          // Add a magic comment to the bottom of the file so that
          // Chrome knows the name of the script in the JavaScript debugger.
          var debugSrc = src.replace('closure/../', '');
          // The 'chromevox' id is only used in the DevTools instead of a long
          // extension id.
          scriptText += '\n//# sourceURL= chrome-extension://chromevox/' +
              debugSrc + '\n';
          code[src] = scriptText;
          waiting--;
          if (waiting == 0) {
            done(code);
          }
        }
      };
      xhr.open('GET', url);
      xhr.send(null);
  }

  files.forEach(function(f) { loadScriptAsCode(f); });
};