1 // Copyright 2010 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 * @fileoverview Code to execute before Closure's base.js.
18 * @author dmazzoni@google.com (Dominic Mazzoni)
22 * Tell Closure to load JavaScript code from the extension root directory.
25 window
.CLOSURE_BASE_PATH
= chrome
.extension
.getURL('/closure/');
28 * Tell Closure not to load deps.js; it's included by manifest.json already.
31 window
.CLOSURE_NO_DEPS
= true;
34 * Array of urls that should be included next, in order.
41 * Custom function for importing ChromeVox scripts.
42 * @param {string} src The JS file to import.
43 * @return {boolean} Whether the script was imported.
45 window
.CLOSURE_IMPORT_SCRIPT = function(src
) {
46 // Only run our version of the import script
47 // when trying to inject ChromeVox scripts.
48 if (src
.indexOf('chrome-extension://') == 0) {
49 if (!goog
.inHtmlDocument_() ||
50 goog
.dependencies_
.written
[src
]) {
53 goog
.dependencies_
.written
[src
] = true;
54 function loadNextScript() {
55 if (goog
.global
.queue_
.length
== 0)
58 var src
= goog
.global
.queue_
[0];
60 if (window
.CLOSURE_USE_EXT_MESSAGES
) {
61 var relativeSrc
= src
.substr(src
.indexOf('closure/..') + 11);
62 chrome
.extension
.sendMessage(
63 {'srcFile': relativeSrc
},
66 eval(response
['code']);
68 console
.error('Script error: ' + e
+ ' in ' + src
);
70 goog
.global
.queue_
= goog
.global
.queue_
.slice(1);
75 window
.console
.log('Using XHR');
77 // Load the script by fetching its source and running 'eval' on it
78 // directly, with a magic comment that makes Chrome treat it like it
79 // loaded normally. Wait until it's fetched before loading the
81 var xhr
= new XMLHttpRequest();
82 var url
= src
+ '?' + new Date().getTime();
83 xhr
.onreadystatechange = function() {
84 if (xhr
.readyState
== 4) {
85 var scriptText
= xhr
.responseText
;
86 // Add a magic comment to the bottom of the file so that
87 // Chrome knows the name of the script in the JavaScript debugger.
88 scriptText
+= '\n//# sourceURL=' + src
+ '\n';
90 goog
.global
.queue_
= goog
.global
.queue_
.slice(1);
94 xhr
.open('GET', url
, false);
97 goog
.global
.queue_
.push(src
);
98 if (goog
.global
.queue_
.length
== 1) {
103 return goog
.writeScriptTag_(src
);