Chromium Code Reviews| 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 | |
|
Emily Fortuna
2012/10/04 21:13:48
Can you add an overall explanatory comment at the
| |
| 5 var ConsoleCollector = {}; | |
| 6 | |
| 7 (function() { | |
| 8 // An arry for collecting the messages. | |
|
Emily Fortuna
2012/10/04 21:13:48
typo: array
| |
| 9 var messages = []; | |
| 10 | |
| 11 // Add a console message to the collection. | |
| 12 this.add = function(message) { | |
| 13 messages.push(message); | |
| 14 }; | |
| 15 | |
| 16 // Read the message collection. As a side effect we clear the message list. | |
| 17 this.read = function(type) { | |
| 18 var rtn = []; | |
| 19 for (var i = 0; i < messages.length; i++) { | |
| 20 var message = messages[i]; | |
| 21 rtn.push({ 'time' : message.timeStamp, | |
| 22 'source' : message.sourceName, | |
| 23 'line': message.lineNumber, | |
| 24 'column': message.columnNumber, | |
| 25 'category': message.category, | |
| 26 'message' : message.errorMessage }); | |
| 27 } | |
| 28 messages = []; | |
| 29 return rtn; | |
| 30 }; | |
| 31 | |
| 32 // Clear the message list. | |
| 33 this.clear = function() { | |
| 34 messages = []; | |
| 35 }; | |
| 36 }).apply(ConsoleCollector); | |
| 37 | |
| 38 // A Console Listener. | |
| 39 // See https://developer.mozilla.org/en-US/docs/Console_service for | |
| 40 // details. | |
| 41 (function() { | |
| 42 | |
| 43 var consoleService; | |
| 44 | |
| 45 var consoleListener = { | |
| 46 observe: function(e) { | |
| 47 try { | |
| 48 var message = e.QueryInterface(Components.interfaces.nsIScriptError); | |
| 49 ConsoleCollector.add(message); | |
| 50 } catch (exception) { | |
| 51 ConsoleCollector.add(e); | |
| 52 } | |
| 53 }, | |
| 54 | |
| 55 QueryInterface: function (iid) { | |
| 56 if (!iid.equals(Components.interfaces.nsIConsoleListener) && | |
| 57 !iid.equals(Components.interfaces.nsISupports)) { | |
| 58 throw Components.results.NS_ERROR_NO_INTERFACE; | |
| 59 } | |
| 60 return this; | |
| 61 } | |
| 62 }; | |
| 63 | |
| 64 // Start collecting console messages. | |
| 65 function initialize(event) { | |
| 66 consoleService = Components.classes['@mozilla.org/consoleservice;1'] | |
| 67 .getService(Components.interfaces.nsIConsoleService); | |
| 68 if (consoleService) { | |
| 69 consoleService.registerListener(consoleListener); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 // Stop collecting console messages. | |
| 74 function shutdown(event) { | |
| 75 consoleService.unregisterListener(consoleListener); | |
| 76 } | |
| 77 | |
| 78 // Hook the ConsoleCollector into the DOM as window.ConsoleCollector. | |
| 79 var onPageLoad = function(e) { | |
| 80 var win = e.originalTarget.defaultView; | |
| 81 if (win) { | |
| 82 win.wrappedJSObject.ConsoleCollector = ConsoleCollector; | |
| 83 } | |
| 84 }; | |
| 85 | |
| 86 // Add the handler for hooking in to each page's DOM. This handler | |
| 87 // is for each "gBrowser", representing a tab/window. | |
| 88 window.getBrowser().addEventListener("load", onPageLoad, true); | |
| 89 // Add the handlers to initialize the add-on and shut it down. | |
| 90 // These handlers are for the application as a whole. | |
| 91 window.addEventListener('load', initialize, false); | |
| 92 window.addEventListener('unload', shutdown, false); | |
| 93 }()); | |
| 94 | |
| 95 | |
| 96 | |
| OLD | NEW |