Chromium Code Reviews| Index: tools/testing/extensions/chrome/ConsoleCollector/background.js |
| =================================================================== |
| --- tools/testing/extensions/chrome/ConsoleCollector/background.js (revision 0) |
| +++ tools/testing/extensions/chrome/ConsoleCollector/background.js (revision 0) |
| @@ -0,0 +1,73 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +// This is the background window. It can access the necessary APIs to get |
| +// at the console messages. It can only communicate with the content |
| +// window through message passing. |
| +// |
| +// There is no way to query the console messages, as such, but we can |
| +// set up a handler that is called when there are console messages. This |
| +// will be called with any console messages already present, so it can be set |
| +// up after the fact. However, if there are no messages it won't be called. |
| +// To handle the end of the messages (or no messages) we have to use a timer |
| +// that can fire when the handler has been idle for a while. |
| + |
| +var version = "1.0"; |
| +var messages = []; // An array that we can put messages in. |
| +var debuggeeId; // An object that identifies the browser tab we are talking to. |
| +var callback; // For passing back the response to the content window. |
| +var timer; // To time out if no messages are available. |
| + |
| +function allDone() { |
| + callback(messages); // Send back the messages we obtained. |
|
Emily Fortuna
2012/10/09 18:15:22
do we want to do the google style javascript comme
|
| + // Turn off console listening and debugging. |
| + chrome.debugger.sendCommand(debuggeeId, "Console.disable", {}, function() { }); |
|
Emily Fortuna
2012/10/09 18:15:22
80 char
gram
2012/10/09 18:57:14
Done.
|
| + chrome.debugger.detach(debuggeeId, function() {}); |
| +} |
| + |
| +function onEvent(debuggeeId, method, params) { |
| + clearTimeout(timer); // Reset the timeout. |
| + var tabId = debuggeeId.tabId; |
| + if (method == "Console.messageAdded") { |
| + var msg = params.message; |
| + // More fields are available if we want them later. See |
| + // https://developers.google.com/chrome-developer-tools/docs/\ |
| + // protocol/1.0/console#type-ConsoleMessage |
|
ahe
2012/10/09 06:01:04
I don't think you need to wrap URIs.
gram
2012/10/09 18:57:14
Done.
|
| + messages.push({"source":msg.url, "line": msg.line, |
| + "category":msg.source, "message":msg.text }); |
| + } |
| + timer = setTimeout(allDone, 1000); |
| +} |
| + |
| +// Set up the general handler for debug events. |
| +chrome.debugger.onEvent.addListener(onEvent); |
| + |
| +// Handle requests sent by the content script. |
| +function onRequest(request, sender, sendResponse) { |
| + if (request.command == "getMessages") { |
| + callback = sendResponse; // Save the callback for later. |
| + // Get the window and tab we are talking to. |
| + chrome.windows.getCurrent(function(win) { |
| + chrome.tabs.getSelected(win.id, function(tab) { |
| + debuggeeId = {tabId:tab.id}; |
| + // Attach the debugger to the tab. |
| + chrome.debugger.attach(debuggeeId, version, function() { |
| + if (chrome.extension.lastError) { |
| + // Attach failed; send an empty response. |
| + callback([]); |
| + } else { |
| + // Turn on console message event handling. |
| + chrome.debugger.sendCommand(debuggeeId, "Console.enable", {}, |
| + function() {}); |
| + timer = setTimeout(allDone, 1000); |
| + } |
| + }); |
| + }); |
| + }); |
| + } |
| +} |
| + |
| +// Listen for the content script to send a message to the background page. |
| +chrome.extension.onRequest.addListener(onRequest); |
| + |
|
Emily Fortuna
2012/10/09 18:15:22
nits: extra newlines at the bottoms of this file a
gram
2012/10/09 18:57:14
Done.
|