- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / examples / api / devtools / inspectedWindow / chrome-preprocessor / Panel / PreprocessorPanel.js
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 (function() {
6
7 // This function is converted to a string and becomes the preprocessor
8 function preprocessor(source, url, listenerName) {
9   url = url ? url : '(eval)';
10   url += listenerName ? '_' + listenerName : '';
11   var prefix = 'window.__preprocessed = window.__preprocessed || [];\n';
12   prefix += 'window.__preprocessed.push(\'' + url +'\');\n';
13   var postfix = '\n//# sourceURL=' + url + '.js\n';
14   return prefix + source + postfix;
15 }
16
17 function extractPreprocessedFiles(onExtracted) {
18   var expr = 'window.__preprocessed';
19   function onEval(files, isException) {
20     if (isException)
21       throw new Error('Eval failed for ' + expr, isException.value);
22     onExtracted(files);
23   }
24   chrome.devtools.inspectedWindow.eval(expr, onEval);
25 }
26
27 function reloadWithPreprocessor(injectedScript) {
28   var options = {
29     ignoreCache: true,
30     userAgent: undefined,
31     injectedScript: '(' + injectedScript  + ')()',
32     preprocessingScript: '(' + preprocessor + ')'
33   };
34   chrome.devtools.inspectedWindow.reload(options);
35 }
36
37 function demoPreprocessor() {
38   function onLoaded() {
39     extractPreprocessedFiles(updateUI);
40   }
41   var loadMonitor = new InspectedWindow.LoadMonitor(onLoaded);
42   reloadWithPreprocessor(loadMonitor.injectedScript);
43 }
44
45 function listen() {
46   var reloadButton = document.querySelector('.reload-button');
47   reloadButton.addEventListener('click', demoPreprocessor);
48 }
49
50 window.addEventListener('load', listen);
51
52 function createRow(url) {
53   var li = document.createElement('li');
54   li.textContent = url;
55   return li;
56 }
57
58 function updateUI(preprocessedFiles) {
59   var rowContainer = document.querySelector('.js-preprocessed-urls');
60   rowContainer.innerHTML = '';
61   preprocessedFiles.forEach(function(url) {
62     rowContainer.appendChild(createRow(url));
63   });
64 }
65
66 })();
67
68