Deze vertaling is niet volledig. Help dit artikel te vertalen vanuit het Engels.
De Browser Console is zoals de Web Console, maar toegepast op de hele browser in plaats van alleen het huidige tabblad.
Het logt dus dezelfde soort informatie als de Web Console - netwerkverzoeken, JavaScript, CSS, en beveiligingsfouten -en waarschuwingen, en logberichten van JavaScript code. Het logt deze informatie echter niet van een enkel tabblad, het logt informatie van alle tabbladen, van add-ons, en van de browser zelf.
Als u het andere webontwikkelaarsgereedschap van de gewone Web Toolbox wil gebruiken met add-ons of browser code, gebruik dan de Browser Toolbox.
U kunt ook JavaScript expressies uitvoeren met de Browser Console. Maar terwijl de Web Console de code uitvoert in het bereik van een paginavenster, zal de Browser Console de code uitvoeren in het bereik van de browser's chrome venster. Dit betekent dat u met alle browsertabbladen kunt werken met de gBrowser
global, en zelfs met de XUL waarin de browser's gebruikersinterface is gespecifieerd.
NB: Vanaf Firefox 30 is de Browser Console opdrachtregel (om JavaScript expressies uit te voeren) standaard uitgeschakeld. Om hem in te schakelen, zat dan de devtools.chrome.enabled
voorkeur op true
in about:config, of zet de "Enable chrome debugging" optie in de developer tool settings.
De Browser Console openen
U kan de Browser Console op twee manieren openen:
- from the menu: select "Browser Console" from the Web Developer submenu in the Firefox Menu (or Tools menu if you display the menu bar or are on OS X)
- from the keyboard: press Ctrl+Shift+J (or Cmd+Shift+J on a Mac).
Note that until Firefox 38, if the Browser Console has become hidden by a normal Firefox window and you select it again, either from the menu or from the keyboard, then it will be closed. From Firefox 38 onwards, this instead has the effect of switching the focus back to the Browser Console, which is more likely to be what you wanted.
You can also start the Browser Console by launching Firefox from the command line and passing the -jsconsole
argument:
/Applications/FirefoxAurora.app/Contents/MacOS/firefox-bin -jsconsole
The Browser Console looks like this:
You can see that the Browser Console looks and behaves very much like the Web Console:
- most of the window is occupied by a pane that display messages
- at the top, a toolbar enables you to filter the messages that appear
- at the bottom, a command line interpreter enables you to evaluate JavaScript expressions.
Browser Console logging
The Browser console logs the same sorts of messages as the Web Console:
- HTTP requests
- Warnings and errors (including JavaScript, CSS, security warnings and errors, and messages explicitly logged by JavaScript code using the console API)
- Input/output messages: commands send to the browser via the command line, and the result of executing them.
However, it displays such messages from:
- web content hosted by all browser tabs
- the browser's own code
- add-ons.
Messages from add-ons
The Browser Console displays messages logged by all Firefox add-ons.
Console.jsm
To use the console API from a traditional or bootstrapped add-on, get it from the Console module.
One exported symbol from Console.jsm is "console". Below is an example of how to acess it, which adds a message to the Browser Console.
Components.utils.import("resource://gre/modules/devtools/Console.jsm"); console.log("Hello from Firefox code"); //output messages to the console
Learn more:
HUDService
There is also the HUDService which allows access to the Browse Console. The module is available at Mozilla Cross-Reference. We see we can not only access the Browser Console but also Web Console.
Here is an example on how to clear the contents of the Browser console:
Components.utils.import("resource://gre/modules/devtools/Loader.jsm"); var HUDService = devtools.require("devtools/webconsole/hudservice"); var hud = HUDService.getBrowserConsole(); hud.jsterm.clearOutput(true);
If you would like to access the content document of the Browser Console this can be done with the HUDService. This example here makes it so that when you mouse over the "Clear" button it will clear the Browser Console:
Components.utils.import("resource://gre/modules/devtools/Loader.jsm"); var HUDService = devtools.require("devtools/webconsole/hudservice"); var hud = HUDService.getBrowserConsole(); var clearBtn = hud.chromeWindow.document.querySelector('.webconsole-clear-console-button'); clearBtn.addEventListener('mouseover', function() { hud.jsterm.clearOutput(true); }, false);
Bonus Features Available
For Add-on SDK add-ons, the console API is available automatically. Here's an example add-on that just logs an error when the user clicks a widget:
widget = require("sdk/widget").Widget({ id: "an-error-happened", label: "Error!", width: 40, content: "Error!", onClick: logError }); function logError() { console.error("something went wrong!"); }
If you build this as an XPI file, then open the Browser Console, then open the XPI file in Firefox and install it, you'll see a widget labeled "Error!" in the Add-on bar:
Click the icon. You'll see output like this in the Browser Console:
For Add-on SDK-based add-ons only, the message is prefixed with the name of the add-on ("log-error"), making it easy to find all messages from this add-on using the "Filter output" search box. By default, only error messages are logged to the console, although you can change this in the browser's preferences.
Browser Console command line
From Firefox 30, the Browser Console command line is disabled by default. To enable it set the devtools.chrome.enabled
preference to true
in about:config, or set the "Enable chrome debugging" option in the developer tool settings.
Like the Web Console, the command line interpreter enables you to evaluate JavaScript expressions in real time:Also like the Web Console's command line interpreter, this command line supports autocomplete, history, and various keyboard shortcuts and helper commands. If the result of a command is an object, you can click on the object to see its details.
But while the Web Console executes code in the scope of the content window it's attached to, the browser console executes code in the scope of the chrome window of the browser. You can confirm this by evaluating window
:
This means you can control the browser: opening, closing tabs and windows and changing the content that they host, and modify the browser's UI by creating, changing and removing XUL elements.
Controlling the browser
The command line interpreter gets access to the tabbrowser
object, through the gBrowser
global, and that enables you to control the browser through the command line. Try running this code in the Browser Console's command line (remember that to send multiple lines to the Browser Console, use Shift+Enter):
var newTabBrowser = gBrowser.getBrowserForTab(gBrowser.selectedTab); newTabBrowser.addEventListener("load", function() { newTabBrowser.contentDocument.body.innerHTML = "<h1>this page has been eaten</h1>"; }, true); newTabBrowser.contentDocument.location.href = "https://mozilla.org/";
It adds a listener to the currently selected tab's load
event that will eat the new page, then loads a new page.
Modifying the browser UI
Since the global window
object is the browser's chrome window, you can also modify the browser's user interface. On Windows, the following code will add a new item to the browser's main menu:
var parent = window.document.getElementById("appmenuPrimaryPane"); var makeTheTea = gBrowser.ownerDocument.defaultView.document.createElementNS("https://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menuitem"); makeTheTea.setAttribute("label", "A nice cup of tea?"); parent.appendChild(makeTheTea);
On OS X, this similar code will add a new item to the "Tools" menu:
var parent = window.document.getElementById("menu_ToolsPopup"); var makeTheTea = gBrowser.ownerDocument.defaultView.document.createElementNS("https://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menuitem"); makeTheTea.setAttribute("label", "A nice cup of tea?"); parent.appendChild(makeTheTea);