Home | History | Annotate | Download | only in closure
      1 // Copyright 2010 Google Inc.
      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 /**
     16  * @fileoverview Code to execute before Closure's base.js.
     17  *
     18  * @author dmazzoni (a] google.com (Dominic Mazzoni)
     19  */
     20 
     21 /**
     22  * Tell Closure to load JavaScript code from the extension root directory.
     23  * @type {boolean}
     24  */
     25 window.CLOSURE_BASE_PATH = chrome.extension.getURL('/closure/');
     26 
     27 /**
     28  * Tell Closure not to load deps.js; it's included by manifest.json already.
     29  * @type {boolean}
     30  */
     31 window.CLOSURE_NO_DEPS = true;
     32 
     33 /**
     34  * Array of urls that should be included next, in order.
     35  * @type {Array}
     36  * @private
     37  */
     38 window.queue_ = [];
     39 
     40 /**
     41  * Custom function for importing ChromeVox scripts.
     42  * @param {string} src The JS file to import.
     43  * @return {boolean} Whether the script was imported.
     44  */
     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]) {
     51       return false;
     52     }
     53     goog.dependencies_.written[src] = true;
     54     function loadNextScript() {
     55       if (goog.global.queue_.length == 0)
     56         return;
     57 
     58       var src = goog.global.queue_[0];
     59 
     60       if (window.CLOSURE_USE_EXT_MESSAGES) {
     61         var relativeSrc = src.substr(src.indexOf('closure/..') + 11);
     62         chrome.extension.sendMessage(
     63             {'srcFile': relativeSrc},
     64             function(response) {
     65               try {
     66                 eval(response['code']);
     67               } catch (e) {
     68                 console.error('Script error: ' + e + ' in ' + src);
     69               }
     70               goog.global.queue_ = goog.global.queue_.slice(1);
     71               loadNextScript();
     72             });
     73         return;
     74       }
     75       window.console.log('Using XHR');
     76 
     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
     80       // next script.
     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';
     89           eval(scriptText);
     90           goog.global.queue_ = goog.global.queue_.slice(1);
     91           loadNextScript();
     92         }
     93       };
     94       xhr.open('GET', url, false);
     95       xhr.send(null);
     96     }
     97     goog.global.queue_.push(src);
     98     if (goog.global.queue_.length == 1) {
     99       loadNextScript();
    100     }
    101     return true;
    102   } else {
    103     return goog.writeScriptTag_(src);
    104   }
    105 };
    106