michael@0: // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: var gConsole, gConsoleBundle, gTextBoxEval, gEvaluator, gCodeToEvaluate; michael@0: var gFilter; michael@0: michael@0: /* :::::::: Console Initialization ::::::::::::::: */ michael@0: michael@0: window.onload = function() michael@0: { michael@0: gConsole = document.getElementById("ConsoleBox"); michael@0: gConsoleBundle = document.getElementById("ConsoleBundle"); michael@0: gTextBoxEval = document.getElementById("TextboxEval"); michael@0: gEvaluator = document.getElementById("Evaluator"); michael@0: gFilter = document.getElementById("Filter"); michael@0: michael@0: updateSortCommand(gConsole.sortOrder); michael@0: updateModeCommand(gConsole.mode); michael@0: michael@0: gEvaluator.addEventListener("load", loadOrDisplayResult, true); michael@0: } michael@0: michael@0: /* :::::::: Console UI Functions ::::::::::::::: */ michael@0: michael@0: function changeFilter() michael@0: { michael@0: gConsole.filter = gFilter.value; michael@0: michael@0: document.persist("ConsoleBox", "filter"); michael@0: } michael@0: michael@0: function changeMode(aMode) michael@0: { michael@0: switch (aMode) { michael@0: case "Errors": michael@0: case "Warnings": michael@0: case "Messages": michael@0: gConsole.mode = aMode; michael@0: break; michael@0: case "All": michael@0: gConsole.mode = null; michael@0: } michael@0: michael@0: document.persist("ConsoleBox", "mode"); michael@0: } michael@0: michael@0: function clearConsole() michael@0: { michael@0: gConsole.clear(); michael@0: } michael@0: michael@0: function changeSortOrder(aOrder) michael@0: { michael@0: updateSortCommand(gConsole.sortOrder = aOrder); michael@0: } michael@0: michael@0: function updateSortCommand(aOrder) michael@0: { michael@0: var orderString = aOrder == 'reverse' ? "Descend" : "Ascend"; michael@0: var bc = document.getElementById("Console:sort"+orderString); michael@0: bc.setAttribute("checked", true); michael@0: michael@0: orderString = aOrder == 'reverse' ? "Ascend" : "Descend"; michael@0: bc = document.getElementById("Console:sort"+orderString); michael@0: bc.setAttribute("checked", false); michael@0: } michael@0: michael@0: function updateModeCommand(aMode) michael@0: { michael@0: /* aMode can end up invalid if it set by an extension that replaces */ michael@0: /* mode and then it is uninstalled or disabled */ michael@0: var bc = document.getElementById("Console:mode" + aMode) || michael@0: document.getElementById("Console:modeAll"); michael@0: bc.setAttribute("checked", true); michael@0: } michael@0: michael@0: function onEvalKeyPress(aEvent) michael@0: { michael@0: if (aEvent.keyCode == 13) michael@0: evaluateTypein(); michael@0: } michael@0: michael@0: function evaluateTypein() michael@0: { michael@0: gCodeToEvaluate = gTextBoxEval.value; michael@0: // reset the iframe first; the code will be evaluated in loadOrDisplayResult michael@0: // below, once about:blank has completed loading (see bug 385092) michael@0: gEvaluator.contentWindow.location = "about:blank"; michael@0: } michael@0: michael@0: function loadOrDisplayResult() michael@0: { michael@0: if (gCodeToEvaluate) { michael@0: gEvaluator.contentWindow.location = "javascript: " + michael@0: gCodeToEvaluate.replace(/%/g, "%25"); michael@0: gCodeToEvaluate = ""; michael@0: return; michael@0: } michael@0: michael@0: var resultRange = gEvaluator.contentDocument.createRange(); michael@0: resultRange.selectNode(gEvaluator.contentDocument.documentElement); michael@0: var result = resultRange.toString(); michael@0: if (result) michael@0: Services.console.logStringMessage(result); michael@0: // or could use appendMessage which doesn't persist michael@0: }