| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // This Firefox add-on exposes the Javascript console contents to Javascript | 5 // This Firefox add-on exposes the Javascript console contents to Javascript |
| 6 // running in the browser. Once this is installed there will be a new | 6 // running in the browser. Once this is installed there will be a new |
| 7 // window.ConsoleCollector object with read() and clear() functions. | 7 // window.ConsoleCollector object with read() and clear() functions. |
| 8 | 8 |
| 9 var ConsoleCollector = {}; | 9 var ConsoleCollector = {}; |
| 10 | 10 |
| 11 (function() { | 11 (function() { |
| 12 // An array for collecting the messages. | 12 // An array for collecting the messages. |
| 13 var messages = []; | 13 var messages = []; |
| 14 | 14 |
| 15 // Add a console message to the collection. | 15 // Add a console message to the collection. |
| 16 this.add = function(message) { | 16 this.add = function(message) { |
| 17 messages.push(message); | 17 messages.push(message); |
| 18 }; | 18 }; |
| 19 | 19 |
| 20 // Read the message collection. As a side effect we clear the message list. | 20 // Read the message collection. As a side effect we clear the message list. |
| 21 this.read = function(type) { | 21 this.read = function(type) { |
| 22 var rtn = []; | 22 var rtn = []; |
| 23 for (var i = 0; i < messages.length; i++) { | 23 for (var i = 0; i < messages.length; i++) { |
| 24 var message = messages[i]; | 24 var message = messages[i]; |
| 25 rtn.push({ 'time' : message.timeStamp, | 25 if (message.errorMessage) { |
| 26 rtn.push({ 'time' : message.timeStamp, |
| 26 'source' : message.sourceName, | 27 'source' : message.sourceName, |
| 27 'line': message.lineNumber, | 28 'line': message.lineNumber, |
| 28 'column': message.columnNumber, | 29 'column': message.columnNumber, |
| 29 'category': message.category, | 30 'category': message.category, |
| 30 'message' : message.errorMessage }); | 31 'message' : message.errorMessage }); |
| 32 } |
| 31 } | 33 } |
| 32 messages = []; | 34 messages = []; |
| 33 return rtn; | 35 return rtn; |
| 34 }; | 36 }; |
| 35 | 37 |
| 36 // Clear the message list. | 38 // Clear the message list. |
| 37 this.clear = function() { | 39 this.clear = function() { |
| 38 messages = []; | 40 messages = []; |
| 39 }; | 41 }; |
| 40 }).apply(ConsoleCollector); | 42 }).apply(ConsoleCollector); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 65 } | 67 } |
| 66 }; | 68 }; |
| 67 | 69 |
| 68 // Start collecting console messages. | 70 // Start collecting console messages. |
| 69 function initialize(event) { | 71 function initialize(event) { |
| 70 consoleService = Components.classes['@mozilla.org/consoleservice;1'] | 72 consoleService = Components.classes['@mozilla.org/consoleservice;1'] |
| 71 .getService(Components.interfaces.nsIConsoleService); | 73 .getService(Components.interfaces.nsIConsoleService); |
| 72 if (consoleService) { | 74 if (consoleService) { |
| 73 consoleService.registerListener(consoleListener); | 75 consoleService.registerListener(consoleListener); |
| 74 } | 76 } |
| 77 // Add the handler for hooking in to each page's DOM. This handler |
| 78 // is for each "gBrowser", representing a tab/window. |
| 79 window.getBrowser().addEventListener("load", onPageLoad, true); |
| 75 } | 80 } |
| 76 | 81 |
| 77 // Stop collecting console messages. | 82 // Stop collecting console messages. |
| 78 function shutdown(event) { | 83 function shutdown(event) { |
| 84 window.getBrowser().removeEventListener("load", onPageLoad); |
| 79 consoleService.unregisterListener(consoleListener); | 85 consoleService.unregisterListener(consoleListener); |
| 86 ConsoleCollector.clear(); |
| 80 } | 87 } |
| 81 | 88 |
| 82 // Hook the ConsoleCollector into the DOM as window.ConsoleCollector. | 89 // Hook the ConsoleCollector into the DOM as window.ConsoleCollector. |
| 83 var onPageLoad = function(e) { | 90 var onPageLoad = function(e) { |
| 84 var win = e.originalTarget.defaultView; | 91 var win = e.originalTarget.defaultView; |
| 85 if (win) { | 92 if (win) { |
| 86 win.wrappedJSObject.ConsoleCollector = ConsoleCollector; | 93 win.wrappedJSObject.ConsoleCollector = ConsoleCollector; |
| 87 } | 94 } |
| 88 }; | 95 }; |
| 89 | 96 |
| 90 // Add the handler for hooking in to each page's DOM. This handler | |
| 91 // is for each "gBrowser", representing a tab/window. | |
| 92 window.getBrowser().addEventListener("load", onPageLoad, true); | |
| 93 // Add the handlers to initialize the add-on and shut it down. | 97 // Add the handlers to initialize the add-on and shut it down. |
| 94 // These handlers are for the application as a whole. | 98 // These handlers are for the application as a whole. |
| 95 window.addEventListener('load', initialize, false); | 99 window.addEventListener('load', initialize, false); |
| 96 window.addEventListener('unload', shutdown, false); | 100 window.addEventListener('unload', shutdown, false); |
| 97 }()); | 101 }()); |
| 98 | 102 |
| 99 | 103 |
| 100 | 104 |
| OLD | NEW |