Home | History | Annotate | Download | only in Panel
      1 // Copyright 2013 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 /** @fileoverview The chrome.devtools API does not support notifications from
      6  *  within a Web page nor does it report on events occuring in the page. For now
      7  *  we poll the page to determine a reasonable time to report the scripts.
      8  */
      9 
     10 (function() {
     11   /*
     12    * @param {function} Called  after the 'load' event on the inspected window
     13    * @return {function} A function to be injected into the inspected window.
     14    */
     15   function LoadMonitor(onLoadedCallback) {
     16 
     17     function checkForLoad() {
     18       var expr = 'window.__inspectedWindowLoaded';
     19       function onEval(isLoaded, isException) {
     20         if (isException)
     21           throw new Error('Eval failed for ' + expr, isException.value);
     22         if (isLoaded)
     23           onLoadedCallback();
     24         else
     25           pollForLoad();
     26       }
     27       chrome.devtools.inspectedWindow.eval(expr, onEval);
     28     }
     29 
     30     function pollForLoad() {
     31       setTimeout(checkForLoad, 200);
     32     }
     33 
     34     pollForLoad();
     35   }
     36 
     37   LoadMonitor.prototype = {
     38     // This function should be converted to a string and run in the Web page
     39     injectedScript: function() {
     40       // Initialize a secret data structure.
     41       window.__inspectedWindowLoaded = false;
     42       window.addEventListener('load', function() {
     43         window.__inspectedWindowLoaded = true;
     44         console.log('loaded');
     45       });
     46     }
     47   };
     48 
     49   window.InspectedWindow = window.InspectedWindow || {};
     50   InspectedWindow.LoadMonitor = LoadMonitor;
     51 })();
     52 
     53 
     54