| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // This is the background window. It can access the necessary APIs to get |
| 6 // at the console messages. It can only communicate with the content |
| 7 // window through message passing. |
| 8 // |
| 9 // There is no way to query the console messages, as such, but we can |
| 10 // set up a handler that is called when there are console messages. This |
| 11 // will be called with any console messages already present, so it can be set |
| 12 // up after the fact. However, if there are no messages it won't be called. |
| 13 // To handle the end of the messages (or no messages) we have to use a timer |
| 14 // that can fire when the handler has been idle for a while. |
| 15 |
| 16 var version = "1.0"; |
| 17 var messages = []; // An array that we can put messages in. |
| 18 var debuggee; // An object that identifies the browser tab we are talking to. |
| 19 var callback; // For passing back the response to the content window. |
| 20 var timer; // To time out if no messages are available. |
| 21 |
| 22 function allDone() { |
| 23 callback(messages); // Send back the messages we obtained. |
| 24 // Turn off console listening and debugging. |
| 25 chrome.debugger.sendCommand(debuggee, "Console.disable", {}, function() { }); |
| 26 chrome.debugger.detach(debuggee, function() {}); |
| 27 } |
| 28 |
| 29 function onEvent(debuggeeId, method, params) { |
| 30 clearTimeout(timer); // Reset the timeout. |
| 31 var tabId = debuggeeId.tabId; |
| 32 if (method == "Console.messageAdded") { |
| 33 var msg = params.message; |
| 34 // More fields are available if we want them later. See |
| 35 // https://developers.google.com/chrome-developer-tools/docs/\ |
| 36 // protocol/1.0/console#type-ConsoleMessage |
| 37 messages.push({"source":msg.url, "line": msg.line, |
| 38 "category":msg.source, "message":msg.text }); |
| 39 } |
| 40 timer = setTimeout(allDone, 1000); |
| 41 } |
| 42 |
| 43 // Set up the general handler for debug events. |
| 44 chrome.debugger.onEvent.addListener(onEvent); |
| 45 |
| 46 // Handle requests sent by the content script. |
| 47 function onRequest(request, sender, sendResponse) { |
| 48 if (request.command == "getMessages") { |
| 49 callback = sendResponse; // Save the callback for later. |
| 50 // Get the window and tab we are talking to. |
| 51 chrome.windows.getCurrent(function(win) { |
| 52 chrome.tabs.getSelected(win.id, function(tab) { |
| 53 debuggeeId = {tabId:tab.id}; |
| 54 // Attach the debugger to the tab. |
| 55 chrome.debugger.attach(debuggeeId, version, function() { |
| 56 if (chrome.extension.lastError) { |
| 57 // Attach failed; send an empty response. |
| 58 callback([]); |
| 59 } else { |
| 60 // Turn on console message event handling. |
| 61 chrome.debugger.sendCommand(debuggeeId, "Console.enable", {}, |
| 62 function() {}); |
| 63 timer = setTimeout(allDone, 1000); |
| 64 } |
| 65 }); |
| 66 }); |
| 67 }); |
| 68 } |
| 69 } |
| 70 |
| 71 // Listen for the content script to send a message to the background page. |
| 72 chrome.extension.onRequest.addListener(onRequest); |
| 73 |
| OLD | NEW |