diff options
author | johnjbarton@chromium.org <johnjbarton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-16 16:26:15 +0000 |
---|---|---|
committer | johnjbarton@chromium.org <johnjbarton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-16 16:26:15 +0000 |
commit | b170c0ca7e7057175b3ef12e56e0037475b28c83 (patch) | |
tree | a0f2cd4a402572fa0b5b4db2f23db67e4275f55c | |
parent | 8739b30fd5ddc6259426d942763110e0115e99a5 (diff) | |
download | chromium_src-b170c0ca7e7057175b3ef12e56e0037475b28c83.zip chromium_src-b170c0ca7e7057175b3ef12e56e0037475b28c83.tar.gz chromium_src-b170c0ca7e7057175b3ef12e56e0037475b28c83.tar.bz2 |
Sample Extension for chrome.devtools.inspectedWindow.reload preprocessor option
BUG=
Review URL: https://chromiumcodereview.appspot.com/23892003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@223348 0039d316-1c4b-4281-b951-d872f2087c98
7 files changed, 184 insertions, 0 deletions
diff --git a/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/Panel/InspectedWindowLoadMonitor.js b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/Panel/InspectedWindowLoadMonitor.js new file mode 100644 index 0000000..fd0e987 --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/Panel/InspectedWindowLoadMonitor.js @@ -0,0 +1,53 @@ +// Copyright 2013 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 The chrome.devtools API does not support notifications from + * within a Web page nor does it report on events occuring in the page. For now + * we poll the page to determine a reasonable time to report the scripts. + */ + +(function() { + /* + * @param {function} Called after the 'load' event on the inspected window + * @return {function} A function to be injected into the inspected window. + */ + function LoadMonitor(onLoadedCallback) { + + function checkForLoad() { + var expr = 'window.__inspectedWindowLoaded'; + function onEval(isLoaded, isException) { + if (isException) + throw new Error('Eval failed for ' + expr, isException.value); + if (isLoaded) + onLoadedCallback(); + else + pollForLoad(); + } + chrome.devtools.inspectedWindow.eval(expr, onEval); + } + + function pollForLoad() { + setTimeout(checkForLoad, 200); + } + + pollForLoad(); + } + + LoadMonitor.prototype = { + // This function should be converted to a string and run in the Web page + injectedScript: function() { + // Initialize a secret data structure. + window.__inspectedWindowLoaded = false; + window.addEventListener('load', function() { + window.__inspectedWindowLoaded = true; + console.log('loaded'); + }); + } + }; + + window.InspectedWindow = window.InspectedWindow || {}; + InspectedWindow.LoadMonitor = LoadMonitor; +})(); + + diff --git a/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/Panel/PreprocessorPanel.css b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/Panel/PreprocessorPanel.css new file mode 100644 index 0000000..a4074d9 --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/Panel/PreprocessorPanel.css @@ -0,0 +1,22 @@ +/* Copyright 2013 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. */ + +.newspaper { + font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif; +} + +ol.newspaper { + border: 1px solid #69c; + font-size: 12px; + margin: 20px; + text-align: left; + width: 480px; +} + +ol.newspaper li { + border-top: 1px dashed #fff; + color: #669; + padding: 5px; + vertical-align: top; +} diff --git a/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/Panel/PreprocessorPanel.html b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/Panel/PreprocessorPanel.html new file mode 100644 index 0000000..393c50a --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/Panel/PreprocessorPanel.html @@ -0,0 +1,14 @@ +<html> +<head> + <link rel='stylesheet' href='PreprocessorPanel.css'> +</head> +<body> + <button class='reload-button'>Reload</button> + <h2 class='newspaper' align="bottom">JavaScript files seen by devtools preprocessor. </h2> + <ol class='newspaper js-preprocessed-urls'> + <li>Reload to preprocess sources</li> + </ol> + <script src="InspectedWindowLoadMonitor.js"></script> + <script src="PreprocessorPanel.js"></script> +</body> +</html>
\ No newline at end of file diff --git a/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/Panel/PreprocessorPanel.js b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/Panel/PreprocessorPanel.js new file mode 100644 index 0000000..2cdf41a --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/Panel/PreprocessorPanel.js @@ -0,0 +1,68 @@ +// Copyright 2013 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. + +(function() { + +// This function is converted to a string and becomes the preprocessor +function preprocessor(source, url, listenerName) { + url = url ? url : '(eval)'; + url += listenerName ? '_' + listenerName : ''; + var prefix = 'window.__preprocessed = window.__preprocessed || [];\n'; + prefix += 'window.__preprocessed.push(\'' + url +'\');\n'; + var postfix = '\n//# sourceURL=' + url + '.js\n'; + return prefix + source + postfix; +} + +function extractPreprocessedFiles(onExtracted) { + var expr = 'window.__preprocessed'; + function onEval(files, isException) { + if (isException) + throw new Error('Eval failed for ' + expr, isException.value); + onExtracted(files); + } + chrome.devtools.inspectedWindow.eval(expr, onEval); +} + +function reloadWithPreprocessor(injectedScript) { + var options = { + ignoreCache: true, + userAgent: undefined, + injectedScript: '(' + injectedScript + ')()', + preprocessingScript: '(' + preprocessor + ')' + }; + chrome.devtools.inspectedWindow.reload(options); +} + +function demoPreprocessor() { + function onLoaded() { + extractPreprocessedFiles(updateUI); + } + var loadMonitor = new InspectedWindow.LoadMonitor(onLoaded); + reloadWithPreprocessor(loadMonitor.injectedScript); +} + +function listen() { + var reloadButton = document.querySelector('.reload-button'); + reloadButton.addEventListener('click', demoPreprocessor); +} + +window.addEventListener('load', listen); + +function createRow(url) { + var li = document.createElement('li'); + li.textContent = url; + return li; +} + +function updateUI(preprocessedFiles) { + var rowContainer = document.querySelector('.js-preprocessed-urls'); + rowContainer.innerHTML = ''; + preprocessedFiles.forEach(function(url) { + rowContainer.appendChild(createRow(url)); + }); +} + +})(); + + diff --git a/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/chrome-preprocessor.html b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/chrome-preprocessor.html new file mode 100644 index 0000000..31325a1 --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/chrome-preprocessor.html @@ -0,0 +1,5 @@ +<html> +<body> +<script src="chrome-preprocessor.js"></script> +</body> +</html>
\ No newline at end of file diff --git a/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/chrome-preprocessor.js b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/chrome-preprocessor.js new file mode 100644 index 0000000..98cd749 --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/chrome-preprocessor.js @@ -0,0 +1,10 @@ +// Copyright 2013 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. + +chrome.devtools.panels.create( + 'Preprocessor', + null, // No icon path + 'Panel/PreprocessorPanel.html', + null // no callback needed +); diff --git a/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/manifest.json b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/manifest.json new file mode 100644 index 0000000..c074625 --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/devtools/inspectedWindow/chrome-preprocessor/manifest.json @@ -0,0 +1,12 @@ +{ + "name": "Chrome Preprocessor Example", + "version": "0.1", + "description": "Simple hello-world example for chrome.devtools.inspectedWindow.reload() using preprocessor.", + "devtools_page": "chrome-preprocessor.html", + "manifest_version": 2, + "content_security_policy": "default-src 'self' chrome-extension-resource: ; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-eval'; connect-src *; frame-src *;", + "web_accessible_resources": + [ + "*" + ] +}
\ No newline at end of file |