2011-04-20 12:18:00 +04:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 et:
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-04-20 12:18:00 +04:00
|
|
|
|
2011-05-09 18:51:52 +04:00
|
|
|
/*
|
|
|
|
* Original version history can be found here:
|
|
|
|
* https://github.com/mozilla/workspace
|
|
|
|
*
|
|
|
|
* Copied and relicensed from the Public Domain.
|
|
|
|
* See bug 653934 for details.
|
|
|
|
* https://bugzilla.mozilla.org/show_bug.cgi?id=653934
|
|
|
|
*/
|
|
|
|
|
2011-08-12 01:35:10 +04:00
|
|
|
"use strict";
|
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
2013-07-23 20:58:27 +04:00
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
const SCRATCHPAD_CONTEXT_CONTENT = 1;
|
|
|
|
const SCRATCHPAD_CONTEXT_BROWSER = 2;
|
|
|
|
const BUTTON_POSITION_SAVE = 0;
|
|
|
|
const BUTTON_POSITION_CANCEL = 1;
|
|
|
|
const BUTTON_POSITION_DONT_SAVE = 2;
|
|
|
|
const BUTTON_POSITION_REVERT = 0;
|
2013-11-20 03:17:40 +04:00
|
|
|
const EVAL_FUNCTION_TIMEOUT = 1000; // milliseconds
|
2013-09-20 22:34:10 +04:00
|
|
|
|
2014-04-02 19:17:21 +04:00
|
|
|
const MAXIMUM_FONT_SIZE = 96;
|
|
|
|
const MINIMUM_FONT_SIZE = 6;
|
|
|
|
const NORMAL_FONT_SIZE = 12;
|
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
const SCRATCHPAD_L10N = "chrome://browser/locale/devtools/scratchpad.properties";
|
|
|
|
const DEVTOOLS_CHROME_ENABLED = "devtools.chrome.enabled";
|
|
|
|
const PREF_RECENT_FILES_MAX = "devtools.scratchpad.recentFilesMax";
|
2014-01-15 02:12:39 +04:00
|
|
|
const SHOW_TRAILING_SPACE = "devtools.scratchpad.showTrailingSpace";
|
2014-01-16 04:23:32 +04:00
|
|
|
const ENABLE_CODE_FOLDING = "devtools.scratchpad.enableCodeFolding";
|
2013-09-20 22:34:10 +04:00
|
|
|
|
|
|
|
const VARIABLES_VIEW_URL = "chrome://browser/content/devtools/widgets/VariablesView.xul";
|
|
|
|
|
|
|
|
const require = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools.require;
|
2013-12-03 02:09:11 +04:00
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
const Telemetry = require("devtools/shared/telemetry");
|
|
|
|
const Editor = require("devtools/sourceeditor/editor");
|
|
|
|
const TargetFactory = require("devtools/framework/target").TargetFactory;
|
2011-04-20 12:18:00 +04:00
|
|
|
|
2013-12-03 02:09:11 +04:00
|
|
|
const { Promise: promise } = Cu.import("resource://gre/modules/Promise.jsm", {});
|
2011-04-20 12:18:00 +04:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/NetUtil.jsm");
|
2011-11-02 23:32:55 +04:00
|
|
|
Cu.import("resource:///modules/devtools/scratchpad-manager.jsm");
|
2012-05-01 19:17:32 +04:00
|
|
|
Cu.import("resource://gre/modules/jsdebugger.jsm");
|
2012-11-30 12:07:59 +04:00
|
|
|
Cu.import("resource:///modules/devtools/gDevTools.jsm");
|
2013-03-15 05:08:19 +04:00
|
|
|
Cu.import("resource://gre/modules/osfile.jsm");
|
2013-07-23 20:58:27 +04:00
|
|
|
Cu.import("resource:///modules/devtools/ViewHelpers.jsm");
|
2013-09-04 06:22:08 +04:00
|
|
|
Cu.import("resource://gre/modules/reflect.jsm");
|
|
|
|
Cu.import("resource://gre/modules/devtools/DevToolsUtils.jsm");
|
2011-11-02 23:32:55 +04:00
|
|
|
|
2013-05-07 03:11:44 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "VariablesView",
|
2013-07-23 20:58:27 +04:00
|
|
|
"resource:///modules/devtools/VariablesView.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "VariablesViewController",
|
|
|
|
"resource:///modules/devtools/VariablesViewController.jsm");
|
|
|
|
|
2013-09-25 20:03:17 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "EnvironmentClient",
|
|
|
|
"resource://gre/modules/devtools/dbg-client.jsm");
|
|
|
|
|
2013-08-13 20:08:30 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "ObjectClient",
|
2013-07-23 20:58:27 +04:00
|
|
|
"resource://gre/modules/devtools/dbg-client.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "WebConsoleUtils",
|
|
|
|
"resource://gre/modules/devtools/WebConsoleUtils.jsm");
|
2013-05-07 03:11:44 +04:00
|
|
|
|
2013-07-23 20:58:27 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "DebuggerServer",
|
|
|
|
"resource://gre/modules/devtools/dbg-server.jsm");
|
2013-05-07 03:11:44 +04:00
|
|
|
|
2013-07-23 20:58:27 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "DebuggerClient",
|
|
|
|
"resource://gre/modules/devtools/dbg-client.jsm");
|
2013-05-24 14:26:17 +04:00
|
|
|
|
2013-07-23 20:58:27 +04:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "REMOTE_TIMEOUT", () =>
|
2013-09-20 22:34:10 +04:00
|
|
|
Services.prefs.getIntPref("devtools.debugger.remote-timeout"));
|
2013-05-07 03:11:44 +04:00
|
|
|
|
2013-10-21 00:50:37 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "ShortcutUtils",
|
|
|
|
"resource://gre/modules/ShortcutUtils.jsm");
|
|
|
|
|
2013-11-20 03:17:40 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Reflect",
|
|
|
|
"resource://gre/modules/reflect.jsm");
|
|
|
|
|
2013-05-24 14:26:17 +04:00
|
|
|
// Because we have no constructor / destructor where we can log metrics we need
|
|
|
|
// to do so here.
|
|
|
|
let telemetry = new Telemetry();
|
|
|
|
telemetry.toolOpened("scratchpad");
|
2011-04-20 12:18:00 +04:00
|
|
|
|
|
|
|
/**
|
2011-05-09 18:51:52 +04:00
|
|
|
* The scratchpad object handles the Scratchpad window functionality.
|
2011-04-20 12:18:00 +04:00
|
|
|
*/
|
2011-05-09 18:51:52 +04:00
|
|
|
var Scratchpad = {
|
2012-09-15 00:12:57 +04:00
|
|
|
_instanceId: null,
|
2012-02-17 21:11:17 +04:00
|
|
|
_initialWindowTitle: document.title,
|
2013-09-20 22:34:10 +04:00
|
|
|
_dirty: false,
|
2012-02-17 21:11:17 +04:00
|
|
|
|
2013-01-19 04:59:47 +04:00
|
|
|
/**
|
|
|
|
* Check if provided string is a mode-line and, if it is, return an
|
|
|
|
* object with its values.
|
|
|
|
*
|
|
|
|
* @param string aLine
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
_scanModeLine: function SP__scanModeLine(aLine="")
|
|
|
|
{
|
|
|
|
aLine = aLine.trim();
|
|
|
|
|
|
|
|
let obj = {};
|
|
|
|
let ch1 = aLine.charAt(0);
|
|
|
|
let ch2 = aLine.charAt(1);
|
|
|
|
|
|
|
|
if (ch1 !== "/" || (ch2 !== "*" && ch2 !== "/")) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
aLine = aLine
|
|
|
|
.replace(/^\/\//, "")
|
|
|
|
.replace(/^\/\*/, "")
|
|
|
|
.replace(/\*\/$/, "");
|
|
|
|
|
2013-04-25 22:30:00 +04:00
|
|
|
aLine.split(",").forEach(pair => {
|
2013-01-19 04:59:47 +04:00
|
|
|
let [key, val] = pair.split(":");
|
|
|
|
|
|
|
|
if (key && val) {
|
|
|
|
obj[key.trim()] = val.trim();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
|
2014-03-10 17:00:31 +04:00
|
|
|
/**
|
|
|
|
* Add the event listeners for popupshowing events.
|
|
|
|
*/
|
|
|
|
_setupPopupShowingListeners: function SP_setupPopupShowing() {
|
|
|
|
let elementIDs = ['sp-menu_editpopup', 'scratchpad-text-popup'];
|
|
|
|
|
|
|
|
for (let elementID of elementIDs) {
|
|
|
|
let elem = document.getElementById(elementID);
|
|
|
|
if (elem) {
|
|
|
|
elem.addEventListener("popupshowing", function () {
|
|
|
|
goUpdateGlobalEditMenuItems();
|
|
|
|
let commands = ['cmd_undo', 'cmd_redo', 'cmd_delete', 'cmd_findAgain'];
|
|
|
|
commands.forEach(goUpdateCommand);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the event event listeners for command events.
|
|
|
|
*/
|
|
|
|
_setupCommandListeners: function SP_setupCommands() {
|
|
|
|
let commands = {
|
|
|
|
"cmd_gotoLine": () => {
|
|
|
|
goDoCommand('cmd_gotoLine');
|
|
|
|
},
|
|
|
|
"sp-cmd-newWindow": () => {
|
|
|
|
Scratchpad.openScratchpad();
|
|
|
|
},
|
|
|
|
"sp-cmd-openFile": () => {
|
|
|
|
Scratchpad.openFile();
|
|
|
|
},
|
|
|
|
"sp-cmd-clearRecentFiles": () => {
|
|
|
|
Scratchpad.clearRecentFiles();
|
|
|
|
},
|
|
|
|
"sp-cmd-save": () => {
|
|
|
|
Scratchpad.saveFile();
|
|
|
|
},
|
|
|
|
"sp-cmd-saveas": () => {
|
|
|
|
Scratchpad.saveFileAs();
|
|
|
|
},
|
|
|
|
"sp-cmd-revert": () => {
|
|
|
|
Scratchpad.promptRevert();
|
|
|
|
},
|
|
|
|
"sp-cmd-close": () => {
|
|
|
|
Scratchpad.close();
|
|
|
|
},
|
|
|
|
"sp-cmd-run": () => {
|
|
|
|
Scratchpad.run();
|
|
|
|
},
|
|
|
|
"sp-cmd-inspect": () => {
|
|
|
|
Scratchpad.inspect();
|
|
|
|
},
|
|
|
|
"sp-cmd-display": () => {
|
|
|
|
Scratchpad.display();
|
|
|
|
},
|
|
|
|
"sp-cmd-pprint": () => {
|
|
|
|
Scratchpad.prettyPrint();
|
|
|
|
},
|
|
|
|
"sp-cmd-contentContext": () => {
|
|
|
|
Scratchpad.setContentContext();
|
|
|
|
},
|
|
|
|
"sp-cmd-browserContext": () => {
|
|
|
|
Scratchpad.setBrowserContext();
|
|
|
|
},
|
|
|
|
"sp-cmd-reloadAndRun": () => {
|
|
|
|
Scratchpad.reloadAndRun();
|
|
|
|
},
|
|
|
|
"sp-cmd-evalFunction": () => {
|
|
|
|
Scratchpad.evalTopLevelFunction();
|
|
|
|
},
|
|
|
|
"sp-cmd-errorConsole": () => {
|
|
|
|
Scratchpad.openErrorConsole();
|
|
|
|
},
|
|
|
|
"sp-cmd-webConsole": () => {
|
|
|
|
Scratchpad.openWebConsole();
|
|
|
|
},
|
|
|
|
"sp-cmd-documentationLink": () => {
|
|
|
|
Scratchpad.openDocumentationPage();
|
|
|
|
},
|
|
|
|
"sp-cmd-hideSidebar": () => {
|
|
|
|
Scratchpad.sidebar.hide();
|
2014-04-02 19:17:21 +04:00
|
|
|
},
|
|
|
|
"sp-cmd-line-numbers": () => {
|
|
|
|
Scratchpad.toggleEditorOption('lineNumbers');
|
|
|
|
},
|
|
|
|
"sp-cmd-wrap-text": () => {
|
|
|
|
Scratchpad.toggleEditorOption('lineWrapping');
|
|
|
|
},
|
|
|
|
"sp-cmd-highlight-trailing-space": () => {
|
|
|
|
Scratchpad.toggleEditorOption('showTrailingSpace');
|
|
|
|
},
|
|
|
|
"sp-cmd-larger-font": () => {
|
|
|
|
Scratchpad.increaseFontSize();
|
|
|
|
},
|
|
|
|
"sp-cmd-smaller-font": () => {
|
|
|
|
Scratchpad.decreaseFontSize();
|
|
|
|
},
|
|
|
|
"sp-cmd-normal-font": () => {
|
|
|
|
Scratchpad.normalFontSize();
|
|
|
|
},
|
2014-03-10 17:00:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let command in commands) {
|
|
|
|
let elem = document.getElementById(command);
|
|
|
|
if (elem) {
|
|
|
|
elem.addEventListener("command", commands[command]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-04-20 12:18:00 +04:00
|
|
|
/**
|
2011-05-09 18:51:52 +04:00
|
|
|
* The script execution context. This tells Scratchpad in which context the
|
2011-04-20 12:18:00 +04:00
|
|
|
* script shall execute.
|
|
|
|
*
|
|
|
|
* Possible values:
|
2011-05-09 18:51:52 +04:00
|
|
|
* - SCRATCHPAD_CONTEXT_CONTENT to execute code in the context of the current
|
2011-04-20 12:18:00 +04:00
|
|
|
* tab content window object.
|
2011-05-20 01:10:41 +04:00
|
|
|
* - SCRATCHPAD_CONTEXT_BROWSER to execute code in the context of the
|
2011-04-20 12:18:00 +04:00
|
|
|
* currently active chrome window object.
|
|
|
|
*/
|
2011-05-09 18:51:52 +04:00
|
|
|
executionContext: SCRATCHPAD_CONTEXT_CONTENT,
|
2011-04-20 12:18:00 +04:00
|
|
|
|
2011-12-10 21:03:57 +04:00
|
|
|
/**
|
|
|
|
* Tells if this Scratchpad is initialized and ready for use.
|
|
|
|
* @boolean
|
|
|
|
* @see addObserver
|
|
|
|
*/
|
|
|
|
initialized: false,
|
|
|
|
|
2011-04-20 12:18:00 +04:00
|
|
|
/**
|
2013-09-20 22:34:10 +04:00
|
|
|
* Returns the 'dirty' state of this Scratchpad.
|
2011-04-20 12:18:00 +04:00
|
|
|
*/
|
2013-09-20 22:34:10 +04:00
|
|
|
get dirty()
|
2013-08-03 02:03:34 +04:00
|
|
|
{
|
2013-09-20 22:34:10 +04:00
|
|
|
let clean = this.editor && this.editor.isClean();
|
|
|
|
return this._dirty || !clean;
|
2013-08-03 02:03:34 +04:00
|
|
|
},
|
2011-04-20 12:18:00 +04:00
|
|
|
|
|
|
|
/**
|
2013-09-20 22:34:10 +04:00
|
|
|
* Sets the 'dirty' state of this Scratchpad.
|
2011-04-20 12:18:00 +04:00
|
|
|
*/
|
2013-09-20 22:34:10 +04:00
|
|
|
set dirty(aValue)
|
2013-08-03 02:03:34 +04:00
|
|
|
{
|
2013-09-20 22:34:10 +04:00
|
|
|
this._dirty = aValue;
|
|
|
|
if (!aValue && this.editor)
|
2013-11-05 16:07:37 +04:00
|
|
|
this.editor.setClean();
|
2013-09-20 22:34:10 +04:00
|
|
|
this._updateTitle();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the xul:notificationbox DOM element. It notifies the user when
|
|
|
|
* the current code execution context is SCRATCHPAD_CONTEXT_BROWSER.
|
|
|
|
*/
|
|
|
|
get notificationBox()
|
|
|
|
{
|
|
|
|
return document.getElementById("scratchpad-notificationbox");
|
2013-08-03 02:03:34 +04:00
|
|
|
},
|
2011-08-12 01:35:10 +04:00
|
|
|
|
2013-10-04 23:44:09 +04:00
|
|
|
/**
|
|
|
|
* Hide the menu bar.
|
|
|
|
*/
|
|
|
|
hideMenu: function SP_hideMenu()
|
|
|
|
{
|
|
|
|
document.getElementById("sp-menubar").style.display = "none";
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the menu bar.
|
|
|
|
*/
|
|
|
|
showMenu: function SP_showMenu()
|
|
|
|
{
|
|
|
|
document.getElementById("sp-menubar").style.display = "";
|
|
|
|
},
|
|
|
|
|
2011-08-12 01:35:10 +04:00
|
|
|
/**
|
|
|
|
* Get the editor content, in the given range. If no range is given you get
|
|
|
|
* the entire editor content.
|
|
|
|
*
|
|
|
|
* @param number [aStart=0]
|
|
|
|
* Optional, start from the given offset.
|
|
|
|
* @param number [aEnd=content char count]
|
|
|
|
* Optional, end offset for the text you want. If this parameter is not
|
|
|
|
* given, then the text returned goes until the end of the editor
|
|
|
|
* content.
|
|
|
|
* @return string
|
|
|
|
* The text in the given range.
|
|
|
|
*/
|
|
|
|
getText: function SP_getText(aStart, aEnd)
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
2013-09-20 22:34:10 +04:00
|
|
|
var value = this.editor.getText();
|
|
|
|
return value.slice(aStart || 0, aEnd || value.length);
|
2011-04-20 12:18:00 +04:00
|
|
|
},
|
|
|
|
|
2011-11-02 23:32:55 +04:00
|
|
|
/**
|
|
|
|
* Set the filename in the scratchpad UI and object
|
|
|
|
*
|
|
|
|
* @param string aFilename
|
|
|
|
* The new filename
|
|
|
|
*/
|
|
|
|
setFilename: function SP_setFilename(aFilename)
|
|
|
|
{
|
2012-02-17 21:11:17 +04:00
|
|
|
this.filename = aFilename;
|
|
|
|
this._updateTitle();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the Scratchpad window title based on the current state.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_updateTitle: function SP__updateTitle()
|
|
|
|
{
|
2012-10-23 02:53:23 +04:00
|
|
|
let title = this.filename || this._initialWindowTitle;
|
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
if (this.dirty)
|
2012-10-23 02:53:23 +04:00
|
|
|
title = "*" + title;
|
|
|
|
|
|
|
|
document.title = title;
|
2011-11-02 23:32:55 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current state of the scratchpad. Called by the
|
|
|
|
* Scratchpad Manager for session storing.
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
* An object with 3 properties: filename, text, and
|
|
|
|
* executionContext.
|
|
|
|
*/
|
|
|
|
getState: function SP_getState()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
filename: this.filename,
|
|
|
|
text: this.getText(),
|
2011-11-09 22:09:54 +04:00
|
|
|
executionContext: this.executionContext,
|
2013-09-20 22:34:10 +04:00
|
|
|
saved: !this.dirty
|
2011-11-02 23:32:55 +04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the filename and execution context using the given state. Called
|
|
|
|
* when scratchpad is being restored from a previous session.
|
|
|
|
*
|
|
|
|
* @param object aState
|
|
|
|
* An object with filename and executionContext properties.
|
|
|
|
*/
|
2012-07-03 17:40:12 +04:00
|
|
|
setState: function SP_setState(aState)
|
2011-11-02 23:32:55 +04:00
|
|
|
{
|
2013-09-20 22:34:10 +04:00
|
|
|
if (aState.filename)
|
2011-11-02 23:32:55 +04:00
|
|
|
this.setFilename(aState.filename);
|
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
this.dirty = !aState.saved;
|
|
|
|
|
|
|
|
if (aState.executionContext == SCRATCHPAD_CONTEXT_BROWSER)
|
2011-11-02 23:32:55 +04:00
|
|
|
this.setBrowserContext();
|
2013-09-20 22:34:10 +04:00
|
|
|
else
|
2011-11-02 23:32:55 +04:00
|
|
|
this.setContentContext();
|
|
|
|
},
|
|
|
|
|
2011-04-20 12:18:00 +04:00
|
|
|
/**
|
|
|
|
* Get the most recent chrome window of type navigator:browser.
|
|
|
|
*/
|
2013-08-03 02:03:34 +04:00
|
|
|
get browserWindow()
|
|
|
|
{
|
|
|
|
return Services.wm.getMostRecentWindow("navigator:browser");
|
|
|
|
},
|
2011-04-20 12:18:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the gBrowser object of the most recent browser window.
|
|
|
|
*/
|
|
|
|
get gBrowser()
|
|
|
|
{
|
|
|
|
let recentWin = this.browserWindow;
|
|
|
|
return recentWin ? recentWin.gBrowser : null;
|
|
|
|
},
|
|
|
|
|
2012-09-15 00:12:57 +04:00
|
|
|
/**
|
|
|
|
* Unique name for the current Scratchpad instance. Used to distinguish
|
|
|
|
* Scratchpad windows between each other. See bug 661762.
|
|
|
|
*/
|
|
|
|
get uniqueName()
|
|
|
|
{
|
|
|
|
return "Scratchpad/" + this._instanceId;
|
|
|
|
},
|
|
|
|
|
2013-05-07 03:11:44 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sidebar that contains the VariablesView for object inspection.
|
|
|
|
*/
|
|
|
|
get sidebar()
|
|
|
|
{
|
|
|
|
if (!this._sidebar) {
|
2013-06-18 01:55:25 +04:00
|
|
|
this._sidebar = new ScratchpadSidebar(this);
|
2013-05-07 03:11:44 +04:00
|
|
|
}
|
|
|
|
return this._sidebar;
|
|
|
|
},
|
|
|
|
|
2011-04-20 12:18:00 +04:00
|
|
|
/**
|
2013-09-20 22:34:10 +04:00
|
|
|
* Replaces context of an editor with provided value (a string).
|
|
|
|
* Note: this method is simply a shortcut to editor.setText.
|
2011-04-20 12:18:00 +04:00
|
|
|
*/
|
2013-09-20 22:34:10 +04:00
|
|
|
setText: function SP_setText(value)
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
2013-09-20 22:34:10 +04:00
|
|
|
return this.editor.setText(value);
|
2011-04-20 12:18:00 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluate a string in the currently desired context, that is either the
|
|
|
|
* chrome window or the tab content window object.
|
|
|
|
*
|
|
|
|
* @param string aString
|
|
|
|
* The script you want to evaluate.
|
2013-03-15 05:08:19 +04:00
|
|
|
* @return Promise
|
|
|
|
* The promise for the script evaluation result.
|
2011-04-20 12:18:00 +04:00
|
|
|
*/
|
2013-07-23 20:58:27 +04:00
|
|
|
evaluate: function SP_evaluate(aString)
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
2013-07-23 20:58:27 +04:00
|
|
|
let connection;
|
2013-10-04 23:44:09 +04:00
|
|
|
if (this.target) {
|
|
|
|
connection = ScratchpadTarget.consoleFor(this.target);
|
|
|
|
}
|
|
|
|
else if (this.executionContext == SCRATCHPAD_CONTEXT_CONTENT) {
|
2013-07-23 20:58:27 +04:00
|
|
|
connection = ScratchpadTab.consoleFor(this.gBrowser.selectedTab);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
connection = ScratchpadWindow.consoleFor(this.browserWindow);
|
|
|
|
}
|
2013-03-15 05:08:19 +04:00
|
|
|
|
2013-07-23 20:58:27 +04:00
|
|
|
let evalOptions = { url: this.uniqueName };
|
2013-03-15 05:08:19 +04:00
|
|
|
|
2013-07-23 20:58:27 +04:00
|
|
|
return connection.then(({ debuggerClient, webConsoleClient }) => {
|
|
|
|
let deferred = promise.defer();
|
|
|
|
|
|
|
|
webConsoleClient.evaluateJS(aString, aResponse => {
|
|
|
|
this.debuggerClient = debuggerClient;
|
|
|
|
this.webConsoleClient = webConsoleClient;
|
|
|
|
if (aResponse.error) {
|
|
|
|
deferred.reject(aResponse);
|
|
|
|
}
|
2013-11-01 20:03:30 +04:00
|
|
|
else if (aResponse.exception !== null) {
|
2013-07-23 20:58:27 +04:00
|
|
|
deferred.resolve([aString, aResponse]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
deferred.resolve([aString, undefined, aResponse.result]);
|
|
|
|
}
|
|
|
|
}, evalOptions);
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
});
|
|
|
|
},
|
2011-04-20 12:18:00 +04:00
|
|
|
|
|
|
|
/**
|
2011-08-12 01:35:10 +04:00
|
|
|
* Execute the selected text (if any) or the entire editor content in the
|
2011-04-20 12:18:00 +04:00
|
|
|
* current context.
|
2013-03-15 05:08:19 +04:00
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* The promise for the script evaluation result.
|
2011-04-20 12:18:00 +04:00
|
|
|
*/
|
2012-01-03 20:53:39 +04:00
|
|
|
execute: function SP_execute()
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
2013-09-20 22:34:10 +04:00
|
|
|
let selection = this.editor.getSelection() || this.getText();
|
2013-07-23 20:58:27 +04:00
|
|
|
return this.evaluate(selection);
|
2012-01-03 20:53:39 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the selected text (if any) or the entire editor content in the
|
|
|
|
* current context.
|
2013-03-15 05:08:19 +04:00
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* The promise for the script evaluation result.
|
2012-01-03 20:53:39 +04:00
|
|
|
*/
|
|
|
|
run: function SP_run()
|
|
|
|
{
|
2013-07-23 20:58:27 +04:00
|
|
|
let deferred = promise.defer();
|
|
|
|
let reject = aReason => deferred.reject(aReason);
|
|
|
|
|
|
|
|
this.execute().then(([aString, aError, aResult]) => {
|
|
|
|
let resolve = () => deferred.resolve([aString, aError, aResult]);
|
|
|
|
|
2013-03-15 05:08:19 +04:00
|
|
|
if (aError) {
|
2013-07-23 20:58:27 +04:00
|
|
|
this.writeAsErrorComment(aError.exception).then(resolve, reject);
|
2013-03-15 05:08:19 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-09-20 22:34:10 +04:00
|
|
|
this.editor.dropSelection();
|
2013-07-23 20:58:27 +04:00
|
|
|
resolve();
|
2013-03-15 05:08:19 +04:00
|
|
|
}
|
2013-07-23 20:58:27 +04:00
|
|
|
}, reject);
|
|
|
|
|
|
|
|
return deferred.promise;
|
2011-04-20 12:18:00 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-08-12 01:35:10 +04:00
|
|
|
* Execute the selected text (if any) or the entire editor content in the
|
2013-05-07 03:11:44 +04:00
|
|
|
* current context. If the result is primitive then it is written as a
|
|
|
|
* comment. Otherwise, the resulting object is inspected up in the sidebar.
|
2013-03-15 05:08:19 +04:00
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* The promise for the script evaluation result.
|
2011-04-20 12:18:00 +04:00
|
|
|
*/
|
2011-05-09 18:51:52 +04:00
|
|
|
inspect: function SP_inspect()
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
2013-07-11 11:12:20 +04:00
|
|
|
let deferred = promise.defer();
|
2013-05-07 03:11:44 +04:00
|
|
|
let reject = aReason => deferred.reject(aReason);
|
|
|
|
|
|
|
|
this.execute().then(([aString, aError, aResult]) => {
|
|
|
|
let resolve = () => deferred.resolve([aString, aError, aResult]);
|
|
|
|
|
2013-03-15 05:08:19 +04:00
|
|
|
if (aError) {
|
2013-07-23 20:58:27 +04:00
|
|
|
this.writeAsErrorComment(aError.exception).then(resolve, reject);
|
2013-05-07 03:11:44 +04:00
|
|
|
}
|
2013-07-23 20:58:27 +04:00
|
|
|
else if (VariablesView.isPrimitive({ value: aResult })) {
|
2013-07-31 03:27:44 +04:00
|
|
|
this._writePrimitiveAsComment(aResult).then(resolve, reject);
|
2013-03-15 05:08:19 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-09-20 22:34:10 +04:00
|
|
|
this.editor.dropSelection();
|
2013-05-07 03:11:44 +04:00
|
|
|
this.sidebar.open(aString, aResult).then(resolve, reject);
|
2013-03-15 05:08:19 +04:00
|
|
|
}
|
2013-05-07 03:11:44 +04:00
|
|
|
}, reject);
|
2013-06-18 01:55:25 +04:00
|
|
|
|
2013-05-07 03:11:44 +04:00
|
|
|
return deferred.promise;
|
2011-04-20 12:18:00 +04:00
|
|
|
},
|
|
|
|
|
2012-09-14 04:54:02 +04:00
|
|
|
/**
|
|
|
|
* Reload the current page and execute the entire editor content when
|
|
|
|
* the page finishes loading. Note that this operation should be available
|
|
|
|
* only in the content context.
|
2013-03-15 05:08:19 +04:00
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* The promise for the script evaluation result.
|
2012-09-14 04:54:02 +04:00
|
|
|
*/
|
|
|
|
reloadAndRun: function SP_reloadAndRun()
|
|
|
|
{
|
2013-07-11 11:12:20 +04:00
|
|
|
let deferred = promise.defer();
|
2013-03-15 05:08:19 +04:00
|
|
|
|
2012-09-14 04:54:02 +04:00
|
|
|
if (this.executionContext !== SCRATCHPAD_CONTEXT_CONTENT) {
|
|
|
|
Cu.reportError(this.strings.
|
|
|
|
GetStringFromName("scratchpadContext.invalid"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let browser = this.gBrowser.selectedBrowser;
|
|
|
|
|
2013-03-15 05:08:19 +04:00
|
|
|
this._reloadAndRunEvent = evt => {
|
2012-09-14 04:54:02 +04:00
|
|
|
if (evt.target !== browser.contentDocument) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
browser.removeEventListener("load", this._reloadAndRunEvent, true);
|
2013-03-15 05:08:19 +04:00
|
|
|
|
|
|
|
this.run().then(aResults => deferred.resolve(aResults));
|
|
|
|
};
|
2012-09-14 04:54:02 +04:00
|
|
|
|
|
|
|
browser.addEventListener("load", this._reloadAndRunEvent, true);
|
|
|
|
browser.contentWindow.location.reload();
|
2013-03-15 05:08:19 +04:00
|
|
|
|
|
|
|
return deferred.promise;
|
2012-09-14 04:54:02 +04:00
|
|
|
},
|
|
|
|
|
2011-04-20 12:18:00 +04:00
|
|
|
/**
|
2011-08-12 01:35:10 +04:00
|
|
|
* Execute the selected text (if any) or the entire editor content in the
|
|
|
|
* current context. The evaluation result is inserted into the editor after
|
|
|
|
* the selected text, or at the end of the editor content if there is no
|
2011-04-20 12:18:00 +04:00
|
|
|
* selected text.
|
2013-03-15 05:08:19 +04:00
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* The promise for the script evaluation result.
|
2011-04-20 12:18:00 +04:00
|
|
|
*/
|
2011-05-21 15:59:23 +04:00
|
|
|
display: function SP_display()
|
2012-01-03 20:53:39 +04:00
|
|
|
{
|
2013-07-23 20:58:27 +04:00
|
|
|
let deferred = promise.defer();
|
|
|
|
let reject = aReason => deferred.reject(aReason);
|
|
|
|
|
|
|
|
this.execute().then(([aString, aError, aResult]) => {
|
|
|
|
let resolve = () => deferred.resolve([aString, aError, aResult]);
|
|
|
|
|
2013-03-15 05:08:19 +04:00
|
|
|
if (aError) {
|
2013-07-23 20:58:27 +04:00
|
|
|
this.writeAsErrorComment(aError.exception).then(resolve, reject);
|
|
|
|
}
|
|
|
|
else if (VariablesView.isPrimitive({ value: aResult })) {
|
2013-07-31 03:27:44 +04:00
|
|
|
this._writePrimitiveAsComment(aResult).then(resolve, reject);
|
2013-03-15 05:08:19 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-08-13 20:08:30 +04:00
|
|
|
let objectClient = new ObjectClient(this.debuggerClient, aResult);
|
|
|
|
objectClient.getDisplayString(aResponse => {
|
2013-07-23 20:58:27 +04:00
|
|
|
if (aResponse.error) {
|
2013-07-31 03:27:44 +04:00
|
|
|
reportError("display", aResponse);
|
2013-07-23 20:58:27 +04:00
|
|
|
reject(aResponse);
|
|
|
|
}
|
|
|
|
else {
|
2013-11-19 01:24:54 +04:00
|
|
|
this.writeAsComment(aResponse.displayString);
|
2013-07-23 20:58:27 +04:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
2013-03-15 05:08:19 +04:00
|
|
|
}
|
2013-07-23 20:58:27 +04:00
|
|
|
}, reject);
|
|
|
|
|
|
|
|
return deferred.promise;
|
2012-01-03 20:53:39 +04:00
|
|
|
},
|
|
|
|
|
2013-11-06 10:49:22 +04:00
|
|
|
_prettyPrintWorker: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get or create the worker that handles pretty printing.
|
|
|
|
*/
|
|
|
|
get prettyPrintWorker() {
|
|
|
|
if (!this._prettyPrintWorker) {
|
|
|
|
this._prettyPrintWorker = new ChromeWorker(
|
|
|
|
"resource://gre/modules/devtools/server/actors/pretty-print-worker.js");
|
|
|
|
|
|
|
|
this._prettyPrintWorker.addEventListener("error", ({ message, filename, lineno }) => {
|
|
|
|
DevToolsUtils.reportException(message + " @ " + filename + ":" + lineno);
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
return this._prettyPrintWorker;
|
|
|
|
},
|
|
|
|
|
2013-09-04 06:22:08 +04:00
|
|
|
/**
|
|
|
|
* Pretty print the source text inside the scratchpad.
|
2013-11-06 10:49:22 +04:00
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* A promise resolved with the pretty printed code, or rejected with
|
|
|
|
* an error.
|
2013-09-04 06:22:08 +04:00
|
|
|
*/
|
|
|
|
prettyPrint: function SP_prettyPrint() {
|
|
|
|
const uglyText = this.getText();
|
2013-09-16 00:05:35 +04:00
|
|
|
const tabsize = Services.prefs.getIntPref("devtools.editor.tabsize");
|
2013-11-06 10:49:22 +04:00
|
|
|
const id = Math.random();
|
|
|
|
const deferred = promise.defer();
|
|
|
|
|
|
|
|
const onReply = ({ data }) => {
|
|
|
|
if (data.id !== id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.prettyPrintWorker.removeEventListener("message", onReply, false);
|
|
|
|
|
|
|
|
if (data.error) {
|
|
|
|
let errorString = DevToolsUtils.safeErrorString(data.error);
|
|
|
|
this.writeAsErrorComment(errorString);
|
|
|
|
deferred.reject(errorString);
|
|
|
|
} else {
|
|
|
|
this.editor.setText(data.code);
|
|
|
|
deferred.resolve(data.code);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.prettyPrintWorker.addEventListener("message", onReply, false);
|
|
|
|
this.prettyPrintWorker.postMessage({
|
|
|
|
id: id,
|
|
|
|
url: "(scratchpad)",
|
|
|
|
indent: tabsize,
|
|
|
|
source: uglyText
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
2013-09-04 06:22:08 +04:00
|
|
|
},
|
|
|
|
|
2013-11-20 03:17:40 +04:00
|
|
|
/**
|
|
|
|
* Parse the text and return an AST. If we can't parse it, write an error
|
|
|
|
* comment and return false.
|
|
|
|
*/
|
|
|
|
_parseText: function SP__parseText(aText) {
|
|
|
|
try {
|
|
|
|
return Reflect.parse(aText);
|
|
|
|
} catch (e) {
|
|
|
|
this.writeAsErrorComment(DevToolsUtils.safeErrorString(e));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the given AST node location contains the given cursor
|
|
|
|
* position.
|
|
|
|
*
|
|
|
|
* @returns Boolean
|
|
|
|
*/
|
|
|
|
_containsCursor: function (aLoc, aCursorPos) {
|
|
|
|
// Our line numbers are 1-based, while CodeMirror's are 0-based.
|
|
|
|
const lineNumber = aCursorPos.line + 1;
|
|
|
|
const columnNumber = aCursorPos.ch;
|
|
|
|
|
|
|
|
if (aLoc.start.line <= lineNumber && aLoc.end.line >= lineNumber) {
|
|
|
|
if (aLoc.start.line === aLoc.end.line) {
|
|
|
|
return aLoc.start.column <= columnNumber
|
|
|
|
&& aLoc.end.column >= columnNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aLoc.start.line == lineNumber) {
|
|
|
|
return columnNumber >= aLoc.start.column;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aLoc.end.line == lineNumber) {
|
|
|
|
return columnNumber <= aLoc.end.column;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the top level function AST node that the cursor is within.
|
|
|
|
*
|
|
|
|
* @returns Object|null
|
|
|
|
*/
|
|
|
|
_findTopLevelFunction: function SP__findTopLevelFunction(aAst, aCursorPos) {
|
|
|
|
for (let statement of aAst.body) {
|
|
|
|
switch (statement.type) {
|
|
|
|
case "FunctionDeclaration":
|
|
|
|
if (this._containsCursor(statement.loc, aCursorPos)) {
|
|
|
|
return statement;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "VariableDeclaration":
|
|
|
|
for (let decl of statement.declarations) {
|
|
|
|
if (!decl.init) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((decl.init.type == "FunctionExpression"
|
|
|
|
|| decl.init.type == "ArrowExpression")
|
|
|
|
&& this._containsCursor(decl.loc, aCursorPos)) {
|
|
|
|
return decl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the source text associated with the given function statement.
|
|
|
|
*
|
|
|
|
* @param Object aFunction
|
|
|
|
* @param String aFullText
|
|
|
|
* @returns String
|
|
|
|
*/
|
|
|
|
_getFunctionText: function SP__getFunctionText(aFunction, aFullText) {
|
|
|
|
let functionText = "";
|
|
|
|
// Initially set to 0, but incremented first thing in the loop below because
|
|
|
|
// line numbers are 1 based, not 0 based.
|
|
|
|
let lineNumber = 0;
|
|
|
|
const { start, end } = aFunction.loc;
|
|
|
|
const singleLine = start.line === end.line;
|
|
|
|
|
|
|
|
for (let line of aFullText.split(/\n/g)) {
|
|
|
|
lineNumber++;
|
|
|
|
|
|
|
|
if (singleLine && start.line === lineNumber) {
|
|
|
|
functionText = line.slice(start.column, end.column);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start.line === lineNumber) {
|
|
|
|
functionText += line.slice(start.column) + "\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end.line === lineNumber) {
|
|
|
|
functionText += line.slice(0, end.column);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start.line < lineNumber && end.line > lineNumber) {
|
|
|
|
functionText += line + "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return functionText;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluate the top level function that the cursor is resting in.
|
|
|
|
*
|
|
|
|
* @returns Promise [text, error, result]
|
|
|
|
*/
|
|
|
|
evalTopLevelFunction: function SP_evalTopLevelFunction() {
|
|
|
|
const text = this.getText();
|
|
|
|
const ast = this._parseText(text);
|
|
|
|
if (!ast) {
|
|
|
|
return promise.resolve([text, undefined, undefined]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const cursorPos = this.editor.getCursor();
|
|
|
|
const funcStatement = this._findTopLevelFunction(ast, cursorPos);
|
|
|
|
if (!funcStatement) {
|
|
|
|
return promise.resolve([text, undefined, undefined]);
|
|
|
|
}
|
|
|
|
|
|
|
|
let functionText = this._getFunctionText(funcStatement, text);
|
|
|
|
|
|
|
|
// TODO: This is a work around for bug 940086. It should be removed when
|
|
|
|
// that is fixed.
|
|
|
|
if (funcStatement.type == "FunctionDeclaration"
|
|
|
|
&& !functionText.startsWith("function ")) {
|
|
|
|
functionText = "function " + functionText;
|
|
|
|
funcStatement.loc.start.column -= 9;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The decrement by one is because our line numbers are 1-based, while
|
|
|
|
// CodeMirror's are 0-based.
|
|
|
|
const from = {
|
|
|
|
line: funcStatement.loc.start.line - 1,
|
|
|
|
ch: funcStatement.loc.start.column
|
|
|
|
};
|
|
|
|
const to = {
|
|
|
|
line: funcStatement.loc.end.line - 1,
|
|
|
|
ch: funcStatement.loc.end.column
|
|
|
|
};
|
|
|
|
|
2013-11-29 18:47:52 +04:00
|
|
|
const marker = this.editor.markText(from, to, "eval-text");
|
2013-11-20 03:17:40 +04:00
|
|
|
setTimeout(() => marker.clear(), EVAL_FUNCTION_TIMEOUT);
|
|
|
|
|
|
|
|
return this.evaluate(functionText);
|
|
|
|
},
|
|
|
|
|
2013-07-31 03:27:44 +04:00
|
|
|
/**
|
|
|
|
* Writes out a primitive value as a comment. This handles values which are
|
|
|
|
* to be printed directly (number, string) as well as grips to values
|
|
|
|
* (null, undefined, longString).
|
|
|
|
*
|
|
|
|
* @param any aValue
|
|
|
|
* The value to print.
|
|
|
|
* @return Promise
|
|
|
|
* The promise that resolves after the value has been printed.
|
|
|
|
*/
|
|
|
|
_writePrimitiveAsComment: function SP__writePrimitiveAsComment(aValue)
|
|
|
|
{
|
|
|
|
let deferred = promise.defer();
|
|
|
|
|
|
|
|
if (aValue.type == "longString") {
|
|
|
|
let client = this.webConsoleClient;
|
|
|
|
client.longString(aValue).substring(0, aValue.length, aResponse => {
|
|
|
|
if (aResponse.error) {
|
|
|
|
reportError("display", aResponse);
|
|
|
|
deferred.reject(aResponse);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
deferred.resolve(aResponse.substring);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
deferred.resolve(aValue.type || aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise.then(aComment => {
|
|
|
|
this.writeAsComment(aComment);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-01-03 20:53:39 +04:00
|
|
|
/**
|
2012-02-24 18:41:06 +04:00
|
|
|
* Write out a value at the next line from the current insertion point.
|
|
|
|
* The comment block will always be preceded by a newline character.
|
2012-01-03 20:53:39 +04:00
|
|
|
* @param object aValue
|
|
|
|
* The Object to write out as a string
|
|
|
|
*/
|
|
|
|
writeAsComment: function SP_writeAsComment(aValue)
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
2013-09-20 22:34:10 +04:00
|
|
|
let value = "\n/*\n" + aValue + "\n*/";
|
2012-09-12 14:39:51 +04:00
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
if (this.editor.somethingSelected()) {
|
|
|
|
let from = this.editor.getCursor("end");
|
|
|
|
this.editor.replaceSelection(this.editor.getSelection() + value);
|
|
|
|
let to = this.editor.getPosition(this.editor.getOffset(from) + value.length);
|
|
|
|
this.editor.setSelection(from, to);
|
|
|
|
return;
|
|
|
|
}
|
2012-09-12 14:39:51 +04:00
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
let text = this.editor.getText();
|
|
|
|
this.editor.setText(text + value);
|
2011-04-20 12:18:00 +04:00
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
let [ from, to ] = this.editor.getPosition(text.length, (text + value).length);
|
|
|
|
this.editor.setSelection(from, to);
|
2011-04-20 12:18:00 +04:00
|
|
|
},
|
|
|
|
|
2012-01-03 20:53:39 +04:00
|
|
|
/**
|
|
|
|
* Write out an error at the current insertion point as a block comment
|
|
|
|
* @param object aValue
|
|
|
|
* The Error object to write out the message and stack trace
|
2013-07-23 20:58:27 +04:00
|
|
|
* @return Promise
|
|
|
|
* The promise that indicates when writing the comment completes.
|
2012-01-03 20:53:39 +04:00
|
|
|
*/
|
|
|
|
writeAsErrorComment: function SP_writeAsErrorComment(aError)
|
|
|
|
{
|
2013-07-23 20:58:27 +04:00
|
|
|
let deferred = promise.defer();
|
|
|
|
|
|
|
|
if (VariablesView.isPrimitive({ value: aError })) {
|
2013-11-01 20:03:30 +04:00
|
|
|
let type = aError.type;
|
|
|
|
if (type == "undefined" ||
|
|
|
|
type == "null" ||
|
|
|
|
type == "Infinity" ||
|
|
|
|
type == "-Infinity" ||
|
|
|
|
type == "NaN" ||
|
|
|
|
type == "-0") {
|
|
|
|
deferred.resolve(type);
|
|
|
|
}
|
|
|
|
else if (type == "longString") {
|
|
|
|
deferred.resolve(aError.initial + "\u2026");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
deferred.resolve(aError);
|
|
|
|
}
|
2012-05-23 01:24:00 +04:00
|
|
|
}
|
2013-07-23 20:58:27 +04:00
|
|
|
else {
|
2013-08-13 20:08:30 +04:00
|
|
|
let objectClient = new ObjectClient(this.debuggerClient, aError);
|
2013-10-16 21:56:05 +04:00
|
|
|
objectClient.getPrototypeAndProperties(aResponse => {
|
|
|
|
if (aResponse.error) {
|
|
|
|
deferred.reject(aResponse);
|
|
|
|
return;
|
|
|
|
}
|
2013-07-23 20:58:27 +04:00
|
|
|
|
2013-10-16 21:56:05 +04:00
|
|
|
let { ownProperties, safeGetterValues } = aResponse;
|
|
|
|
let error = Object.create(null);
|
2013-07-23 20:58:27 +04:00
|
|
|
|
|
|
|
// Combine all the property descriptor/getter values into one object.
|
2013-10-16 21:56:05 +04:00
|
|
|
for (let key of Object.keys(safeGetterValues)) {
|
|
|
|
error[key] = safeGetterValues[key].getterValue;
|
2013-07-23 20:58:27 +04:00
|
|
|
}
|
2012-05-23 01:24:00 +04:00
|
|
|
|
2013-10-16 21:56:05 +04:00
|
|
|
for (let key of Object.keys(ownProperties)) {
|
|
|
|
error[key] = ownProperties[key].value;
|
2013-07-23 20:58:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble the best possible stack we can given the properties we have.
|
|
|
|
let stack;
|
2014-02-05 00:54:26 +04:00
|
|
|
if (typeof error.stack == "string" && error.stack) {
|
2013-07-23 20:58:27 +04:00
|
|
|
stack = error.stack;
|
|
|
|
}
|
2014-02-05 00:54:26 +04:00
|
|
|
else if (typeof error.fileName == "string") {
|
2013-07-23 20:58:27 +04:00
|
|
|
stack = "@" + error.fileName;
|
|
|
|
if (typeof error.lineNumber == "number") {
|
|
|
|
stack += ":" + error.lineNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (typeof error.lineNumber == "number") {
|
|
|
|
stack = "@" + error.lineNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
stack = stack ? "\n" + stack.replace(/\n$/, "") : "";
|
|
|
|
|
|
|
|
if (typeof error.message == "string") {
|
|
|
|
deferred.resolve(error.message + stack);
|
|
|
|
}
|
|
|
|
else {
|
2013-10-16 21:56:05 +04:00
|
|
|
objectClient.getDisplayString(aResponse => {
|
|
|
|
if (aResponse.error) {
|
|
|
|
deferred.reject(aResponse);
|
2013-07-23 20:58:27 +04:00
|
|
|
}
|
2013-10-16 21:56:05 +04:00
|
|
|
else if (typeof aResponse.displayString == "string") {
|
|
|
|
deferred.resolve(aResponse.displayString + stack);
|
2013-07-23 20:58:27 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-10-16 21:56:05 +04:00
|
|
|
deferred.resolve(stack);
|
2013-07-23 20:58:27 +04:00
|
|
|
}
|
2013-10-16 21:56:05 +04:00
|
|
|
});
|
2013-07-23 20:58:27 +04:00
|
|
|
}
|
2013-10-16 21:56:05 +04:00
|
|
|
});
|
2013-07-23 20:58:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise.then(aMessage => {
|
2013-10-16 21:56:05 +04:00
|
|
|
console.error(aMessage);
|
2013-07-23 20:58:27 +04:00
|
|
|
this.writeAsComment("Exception: " + aMessage);
|
|
|
|
});
|
2012-01-03 20:53:39 +04:00
|
|
|
},
|
|
|
|
|
2011-04-20 12:18:00 +04:00
|
|
|
// Menu Operations
|
|
|
|
|
|
|
|
/**
|
2011-05-09 18:51:52 +04:00
|
|
|
* Open a new Scratchpad window.
|
2011-11-15 12:41:37 +04:00
|
|
|
*
|
|
|
|
* @return nsIWindow
|
2011-04-20 12:18:00 +04:00
|
|
|
*/
|
2011-05-09 18:51:52 +04:00
|
|
|
openScratchpad: function SP_openScratchpad()
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
2011-11-15 12:41:37 +04:00
|
|
|
return ScratchpadManager.openScratchpad();
|
2011-04-20 12:18:00 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export the textbox content to a file.
|
|
|
|
*
|
|
|
|
* @param nsILocalFile aFile
|
|
|
|
* The file where you want to save the textbox content.
|
|
|
|
* @param boolean aNoConfirmation
|
|
|
|
* If the file already exists, ask for confirmation?
|
|
|
|
* @param boolean aSilentError
|
|
|
|
* True if you do not want to display an error when file save fails,
|
|
|
|
* false otherwise.
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file save completes. It will
|
|
|
|
* get the following arguments:
|
|
|
|
* 1) the nsresult status code for the export operation.
|
|
|
|
*/
|
2011-05-09 18:51:52 +04:00
|
|
|
exportToFile: function SP_exportToFile(aFile, aNoConfirmation, aSilentError,
|
2011-04-20 12:18:00 +04:00
|
|
|
aCallback)
|
|
|
|
{
|
|
|
|
if (!aNoConfirmation && aFile.exists() &&
|
|
|
|
!window.confirm(this.strings.
|
|
|
|
GetStringFromName("export.fileOverwriteConfirmation"))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-26 05:23:36 +04:00
|
|
|
let encoder = new TextEncoder();
|
|
|
|
let buffer = encoder.encode(this.getText());
|
2013-07-11 11:12:20 +04:00
|
|
|
let writePromise = OS.File.writeAtomic(aFile.path, buffer,{tmpPath: aFile.path + ".tmp"});
|
|
|
|
writePromise.then(value => {
|
2013-02-26 05:23:36 +04:00
|
|
|
if (aCallback) {
|
|
|
|
aCallback.call(this, Components.results.NS_OK);
|
|
|
|
}
|
2013-04-25 22:30:00 +04:00
|
|
|
}, reason => {
|
2013-02-26 05:23:36 +04:00
|
|
|
if (!aSilentError) {
|
|
|
|
window.alert(this.strings.GetStringFromName("saveFile.failed"));
|
2011-04-20 12:18:00 +04:00
|
|
|
}
|
|
|
|
if (aCallback) {
|
2013-02-26 05:23:36 +04:00
|
|
|
aCallback.call(this, Components.results.NS_ERROR_UNEXPECTED);
|
2011-04-20 12:18:00 +04:00
|
|
|
}
|
2013-04-25 22:30:00 +04:00
|
|
|
});
|
2013-02-26 05:23:36 +04:00
|
|
|
|
2011-04-20 12:18:00 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the content of a file and put it into the textbox.
|
|
|
|
*
|
|
|
|
* @param nsILocalFile aFile
|
|
|
|
* The file you want to save the textbox content into.
|
|
|
|
* @param boolean aSilentError
|
|
|
|
* True if you do not want to display an error when file load fails,
|
|
|
|
* false otherwise.
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file load completes. It will
|
|
|
|
* get the following arguments:
|
|
|
|
* 1) the nsresult status code for the import operation.
|
|
|
|
* 2) the data that was read from the file, if any.
|
|
|
|
*/
|
2011-05-09 18:51:52 +04:00
|
|
|
importFromFile: function SP_importFromFile(aFile, aSilentError, aCallback)
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
|
|
|
// Prevent file type detection.
|
|
|
|
let channel = NetUtil.newChannel(aFile);
|
|
|
|
channel.contentType = "application/javascript";
|
|
|
|
|
2013-04-25 22:30:00 +04:00
|
|
|
NetUtil.asyncFetch(channel, (aInputStream, aStatus) => {
|
2011-04-20 12:18:00 +04:00
|
|
|
let content = null;
|
|
|
|
|
|
|
|
if (Components.isSuccessCode(aStatus)) {
|
2011-12-22 21:55:49 +04:00
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
|
|
|
|
createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
converter.charset = "UTF-8";
|
2011-04-20 12:18:00 +04:00
|
|
|
content = NetUtil.readInputStreamToString(aInputStream,
|
|
|
|
aInputStream.available());
|
2011-12-22 21:55:49 +04:00
|
|
|
content = converter.ConvertToUnicode(content);
|
2013-01-19 04:59:47 +04:00
|
|
|
|
|
|
|
// Check to see if the first line is a mode-line comment.
|
|
|
|
let line = content.split("\n")[0];
|
2013-04-25 22:30:00 +04:00
|
|
|
let modeline = this._scanModeLine(line);
|
2013-01-24 02:04:18 +04:00
|
|
|
let chrome = Services.prefs.getBoolPref(DEVTOOLS_CHROME_ENABLED);
|
2013-01-19 04:59:47 +04:00
|
|
|
|
2013-01-24 02:04:18 +04:00
|
|
|
if (chrome && modeline["-sp-context"] === "browser") {
|
2013-04-25 22:30:00 +04:00
|
|
|
this.setBrowserContext();
|
2013-01-19 04:59:47 +04:00
|
|
|
}
|
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
this.editor.setText(content);
|
|
|
|
this.editor.clearHistory();
|
2014-01-22 22:23:21 +04:00
|
|
|
this.dirty = false;
|
2013-09-20 22:34:10 +04:00
|
|
|
document.getElementById("sp-cmd-revert").setAttribute("disabled", true);
|
2011-04-20 12:18:00 +04:00
|
|
|
}
|
|
|
|
else if (!aSilentError) {
|
2013-04-25 22:30:00 +04:00
|
|
|
window.alert(this.strings.GetStringFromName("openFile.failed"));
|
2011-04-20 12:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aCallback) {
|
2013-04-25 22:30:00 +04:00
|
|
|
aCallback.call(this, aStatus, content);
|
2011-04-20 12:18:00 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-05-09 18:51:52 +04:00
|
|
|
* Open a file to edit in the Scratchpad.
|
2012-07-03 17:40:12 +04:00
|
|
|
*
|
|
|
|
* @param integer aIndex
|
|
|
|
* Optional integer: clicked menuitem in the 'Open Recent'-menu.
|
2011-04-20 12:18:00 +04:00
|
|
|
*/
|
2012-07-03 17:40:12 +04:00
|
|
|
openFile: function SP_openFile(aIndex)
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
2013-04-25 22:30:00 +04:00
|
|
|
let promptCallback = aFile => {
|
|
|
|
this.promptSave((aCloseFile, aSaved, aStatus) => {
|
2012-07-03 17:40:12 +04:00
|
|
|
let shouldOpen = aCloseFile;
|
|
|
|
if (aSaved && !Components.isSuccessCode(aStatus)) {
|
|
|
|
shouldOpen = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldOpen) {
|
|
|
|
let file;
|
2012-09-20 03:21:38 +04:00
|
|
|
if (aFile) {
|
|
|
|
file = aFile;
|
2012-07-03 17:40:12 +04:00
|
|
|
} else {
|
|
|
|
file = Components.classes["@mozilla.org/file/local;1"].
|
|
|
|
createInstance(Components.interfaces.nsILocalFile);
|
|
|
|
let filePath = this.getRecentFiles()[aIndex];
|
|
|
|
file.initWithPath(filePath);
|
|
|
|
}
|
|
|
|
|
2012-11-06 22:26:00 +04:00
|
|
|
if (!file.exists()) {
|
|
|
|
this.notificationBox.appendNotification(
|
|
|
|
this.strings.GetStringFromName("fileNoLongerExists.notification"),
|
|
|
|
"file-no-longer-exists",
|
|
|
|
null,
|
|
|
|
this.notificationBox.PRIORITY_WARNING_HIGH,
|
|
|
|
null);
|
|
|
|
|
|
|
|
this.clearFiles(aIndex, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-03 17:40:12 +04:00
|
|
|
this.setFilename(file.path);
|
|
|
|
this.importFromFile(file, false);
|
|
|
|
this.setRecentFile(file);
|
|
|
|
}
|
2013-04-25 22:30:00 +04:00
|
|
|
});
|
|
|
|
};
|
2012-09-20 03:21:38 +04:00
|
|
|
|
|
|
|
if (aIndex > -1) {
|
|
|
|
promptCallback();
|
|
|
|
} else {
|
|
|
|
let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
|
|
|
|
fp.init(window, this.strings.GetStringFromName("openFile.title"),
|
|
|
|
Ci.nsIFilePicker.modeOpen);
|
|
|
|
fp.defaultString = "";
|
2013-09-30 22:59:24 +04:00
|
|
|
fp.appendFilter("JavaScript Files", "*.js; *.jsm; *.json");
|
|
|
|
fp.appendFilter("All Files", "*.*");
|
2013-04-25 22:30:00 +04:00
|
|
|
fp.open(aResult => {
|
|
|
|
if (aResult != Ci.nsIFilePicker.returnCancel) {
|
|
|
|
promptCallback(fp.file);
|
|
|
|
}
|
|
|
|
});
|
2012-07-03 17:40:12 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get recent files.
|
|
|
|
*
|
|
|
|
* @return Array
|
|
|
|
* File paths.
|
|
|
|
*/
|
|
|
|
getRecentFiles: function SP_getRecentFiles()
|
|
|
|
{
|
2012-10-31 20:23:51 +04:00
|
|
|
let branch = Services.prefs.getBranch("devtools.scratchpad.");
|
2012-10-31 18:27:37 +04:00
|
|
|
let filePaths = [];
|
2012-10-31 20:23:51 +04:00
|
|
|
|
|
|
|
// WARNING: Do not use getCharPref here, it doesn't play nicely with
|
|
|
|
// Unicode strings.
|
|
|
|
|
2012-07-03 17:40:12 +04:00
|
|
|
if (branch.prefHasUserValue("recentFilePaths")) {
|
2012-10-31 20:23:51 +04:00
|
|
|
let data = branch.getComplexValue("recentFilePaths",
|
|
|
|
Ci.nsISupportsString).data;
|
|
|
|
filePaths = JSON.parse(data);
|
2012-07-03 17:40:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return filePaths;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a recent file in a JSON parsable string.
|
|
|
|
*
|
|
|
|
* @param nsILocalFile aFile
|
|
|
|
* The nsILocalFile we want to save as a recent file.
|
|
|
|
*/
|
|
|
|
setRecentFile: function SP_setRecentFile(aFile)
|
|
|
|
{
|
|
|
|
let maxRecent = Services.prefs.getIntPref(PREF_RECENT_FILES_MAX);
|
|
|
|
if (maxRecent < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let filePaths = this.getRecentFiles();
|
|
|
|
let filesCount = filePaths.length;
|
|
|
|
let pathIndex = filePaths.indexOf(aFile.path);
|
|
|
|
|
|
|
|
// We are already storing this file in the list of recent files.
|
|
|
|
if (pathIndex > -1) {
|
|
|
|
// If it's already the most recent file, we don't have to do anything.
|
|
|
|
if (pathIndex === (filesCount - 1)) {
|
|
|
|
// Updating the menu to clear the disabled state from the wrong menuitem
|
|
|
|
// in rare cases when two or more Scratchpad windows are open and the
|
|
|
|
// same file has been opened in two or more windows.
|
|
|
|
this.populateRecentFilesMenu();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is not the most recent file. Remove it from the list, we add it as
|
|
|
|
// the most recent farther down.
|
|
|
|
filePaths.splice(pathIndex, 1);
|
|
|
|
}
|
|
|
|
// If we are not storing the file and the 'recent files'-list is full,
|
|
|
|
// remove the oldest file from the list.
|
|
|
|
else if (filesCount === maxRecent) {
|
|
|
|
filePaths.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
filePaths.push(aFile.path);
|
|
|
|
|
2012-10-31 20:23:51 +04:00
|
|
|
// WARNING: Do not use setCharPref here, it doesn't play nicely with
|
|
|
|
// Unicode strings.
|
|
|
|
|
|
|
|
let str = Cc["@mozilla.org/supports-string;1"]
|
|
|
|
.createInstance(Ci.nsISupportsString);
|
|
|
|
str.data = JSON.stringify(filePaths);
|
|
|
|
|
|
|
|
let branch = Services.prefs.getBranch("devtools.scratchpad.");
|
|
|
|
branch.setComplexValue("recentFilePaths",
|
|
|
|
Ci.nsISupportsString, str);
|
2012-07-03 17:40:12 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populates the 'Open Recent'-menu.
|
|
|
|
*/
|
|
|
|
populateRecentFilesMenu: function SP_populateRecentFilesMenu()
|
|
|
|
{
|
|
|
|
let maxRecent = Services.prefs.getIntPref(PREF_RECENT_FILES_MAX);
|
|
|
|
let recentFilesMenu = document.getElementById("sp-open_recent-menu");
|
|
|
|
|
|
|
|
if (maxRecent < 1) {
|
|
|
|
recentFilesMenu.setAttribute("hidden", true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let recentFilesPopup = recentFilesMenu.firstChild;
|
|
|
|
let filePaths = this.getRecentFiles();
|
|
|
|
let filename = this.getState().filename;
|
|
|
|
|
|
|
|
recentFilesMenu.setAttribute("disabled", true);
|
|
|
|
while (recentFilesPopup.hasChildNodes()) {
|
|
|
|
recentFilesPopup.removeChild(recentFilesPopup.firstChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filePaths.length > 0) {
|
|
|
|
recentFilesMenu.removeAttribute("disabled");
|
|
|
|
|
|
|
|
// Print out menuitems with the most recent file first.
|
|
|
|
for (let i = filePaths.length - 1; i >= 0; --i) {
|
|
|
|
let menuitem = document.createElement("menuitem");
|
|
|
|
menuitem.setAttribute("type", "radio");
|
|
|
|
menuitem.setAttribute("label", filePaths[i]);
|
|
|
|
|
|
|
|
if (filePaths[i] === filename) {
|
|
|
|
menuitem.setAttribute("checked", true);
|
|
|
|
menuitem.setAttribute("disabled", true);
|
|
|
|
}
|
|
|
|
|
2014-03-10 17:00:31 +04:00
|
|
|
menuitem.addEventListener("command", Scratchpad.openFile.bind(Scratchpad, i));
|
2012-07-03 17:40:12 +04:00
|
|
|
recentFilesPopup.appendChild(menuitem);
|
|
|
|
}
|
|
|
|
|
|
|
|
recentFilesPopup.appendChild(document.createElement("menuseparator"));
|
|
|
|
let clearItems = document.createElement("menuitem");
|
|
|
|
clearItems.setAttribute("id", "sp-menu-clear_recent");
|
|
|
|
clearItems.setAttribute("label",
|
|
|
|
this.strings.
|
|
|
|
GetStringFromName("clearRecentMenuItems.label"));
|
|
|
|
clearItems.setAttribute("command", "sp-cmd-clearRecentFiles");
|
|
|
|
recentFilesPopup.appendChild(clearItems);
|
2011-04-20 12:18:00 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-11-06 22:26:00 +04:00
|
|
|
/**
|
|
|
|
* Clear a range of files from the list.
|
2013-06-18 01:55:25 +04:00
|
|
|
*
|
2012-11-06 22:26:00 +04:00
|
|
|
* @param integer aIndex
|
|
|
|
* Index of file in menu to remove.
|
|
|
|
* @param integer aLength
|
|
|
|
* Number of files from the index 'aIndex' to remove.
|
|
|
|
*/
|
|
|
|
clearFiles: function SP_clearFile(aIndex, aLength)
|
|
|
|
{
|
|
|
|
let filePaths = this.getRecentFiles();
|
|
|
|
filePaths.splice(aIndex, aLength);
|
|
|
|
|
|
|
|
// WARNING: Do not use setCharPref here, it doesn't play nicely with
|
|
|
|
// Unicode strings.
|
|
|
|
|
|
|
|
let str = Cc["@mozilla.org/supports-string;1"]
|
|
|
|
.createInstance(Ci.nsISupportsString);
|
|
|
|
str.data = JSON.stringify(filePaths);
|
|
|
|
|
|
|
|
let branch = Services.prefs.getBranch("devtools.scratchpad.");
|
|
|
|
branch.setComplexValue("recentFilePaths",
|
|
|
|
Ci.nsISupportsString, str);
|
|
|
|
},
|
|
|
|
|
2012-07-03 17:40:12 +04:00
|
|
|
/**
|
|
|
|
* Clear all recent files.
|
|
|
|
*/
|
|
|
|
clearRecentFiles: function SP_clearRecentFiles()
|
|
|
|
{
|
|
|
|
Services.prefs.clearUserPref("devtools.scratchpad.recentFilePaths");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle changes to the 'PREF_RECENT_FILES_MAX'-preference.
|
|
|
|
*/
|
|
|
|
handleRecentFileMaxChange: function SP_handleRecentFileMaxChange()
|
|
|
|
{
|
|
|
|
let maxRecent = Services.prefs.getIntPref(PREF_RECENT_FILES_MAX);
|
|
|
|
let menu = document.getElementById("sp-open_recent-menu");
|
|
|
|
|
|
|
|
// Hide the menu if the 'PREF_RECENT_FILES_MAX'-pref is set to zero or less.
|
|
|
|
if (maxRecent < 1) {
|
|
|
|
menu.setAttribute("hidden", true);
|
|
|
|
} else {
|
|
|
|
if (menu.hasAttribute("hidden")) {
|
|
|
|
if (!menu.firstChild.hasChildNodes()) {
|
|
|
|
this.populateRecentFilesMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.removeAttribute("hidden");
|
|
|
|
}
|
|
|
|
|
|
|
|
let filePaths = this.getRecentFiles();
|
|
|
|
if (maxRecent < filePaths.length) {
|
|
|
|
let diff = filePaths.length - maxRecent;
|
2012-11-06 22:26:00 +04:00
|
|
|
this.clearFiles(0, diff);
|
2012-07-03 17:40:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2011-04-20 12:18:00 +04:00
|
|
|
/**
|
|
|
|
* Save the textbox content to the currently open file.
|
2011-11-24 06:49:03 +04:00
|
|
|
*
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file is saved
|
2011-04-20 12:18:00 +04:00
|
|
|
*/
|
2011-11-24 06:49:03 +04:00
|
|
|
saveFile: function SP_saveFile(aCallback)
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
|
|
|
if (!this.filename) {
|
2011-11-24 06:49:03 +04:00
|
|
|
return this.saveFileAs(aCallback);
|
2011-04-20 12:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
|
|
|
|
file.initWithPath(this.filename);
|
2011-11-24 06:49:03 +04:00
|
|
|
|
2013-04-25 22:30:00 +04:00
|
|
|
this.exportToFile(file, true, false, aStatus => {
|
2012-02-17 21:11:17 +04:00
|
|
|
if (Components.isSuccessCode(aStatus)) {
|
2013-09-20 22:34:10 +04:00
|
|
|
this.dirty = false;
|
|
|
|
document.getElementById("sp-cmd-revert").setAttribute("disabled", true);
|
2012-07-03 17:40:12 +04:00
|
|
|
this.setRecentFile(file);
|
2012-02-17 21:11:17 +04:00
|
|
|
}
|
2011-11-24 06:49:03 +04:00
|
|
|
if (aCallback) {
|
|
|
|
aCallback(aStatus);
|
|
|
|
}
|
|
|
|
});
|
2011-04-20 12:18:00 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the textbox content to a new file.
|
2011-11-24 06:49:03 +04:00
|
|
|
*
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file is saved
|
2011-04-20 12:18:00 +04:00
|
|
|
*/
|
2011-11-24 06:49:03 +04:00
|
|
|
saveFileAs: function SP_saveFileAs(aCallback)
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
|
|
|
let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
|
2013-04-25 22:30:00 +04:00
|
|
|
let fpCallback = aResult => {
|
2012-09-20 03:21:38 +04:00
|
|
|
if (aResult != Ci.nsIFilePicker.returnCancel) {
|
|
|
|
this.setFilename(fp.file.path);
|
2013-04-25 22:30:00 +04:00
|
|
|
this.exportToFile(fp.file, true, false, aStatus => {
|
2012-09-20 03:21:38 +04:00
|
|
|
if (Components.isSuccessCode(aStatus)) {
|
2013-09-20 22:34:10 +04:00
|
|
|
this.dirty = false;
|
2012-09-20 03:21:38 +04:00
|
|
|
this.setRecentFile(fp.file);
|
|
|
|
}
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback(aStatus);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-04-25 22:30:00 +04:00
|
|
|
};
|
2012-09-20 03:21:38 +04:00
|
|
|
|
2011-04-20 12:18:00 +04:00
|
|
|
fp.init(window, this.strings.GetStringFromName("saveFileAs"),
|
|
|
|
Ci.nsIFilePicker.modeSave);
|
2011-05-09 18:51:52 +04:00
|
|
|
fp.defaultString = "scratchpad.js";
|
2013-09-30 22:59:24 +04:00
|
|
|
fp.appendFilter("JavaScript Files", "*.js; *.jsm; *.json");
|
|
|
|
fp.appendFilter("All Files", "*.*");
|
2012-09-20 03:21:38 +04:00
|
|
|
fp.open(fpCallback);
|
2011-04-20 12:18:00 +04:00
|
|
|
},
|
|
|
|
|
2012-09-06 08:20:05 +04:00
|
|
|
/**
|
|
|
|
* Restore content from saved version of current file.
|
|
|
|
*
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file is saved
|
|
|
|
*/
|
|
|
|
revertFile: function SP_revertFile(aCallback)
|
|
|
|
{
|
|
|
|
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
|
|
|
|
file.initWithPath(this.filename);
|
|
|
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-25 22:30:00 +04:00
|
|
|
this.importFromFile(file, false, (aStatus, aContent) => {
|
2012-09-06 08:20:05 +04:00
|
|
|
if (aCallback) {
|
|
|
|
aCallback(aStatus);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prompt to revert scratchpad if it has unsaved changes.
|
|
|
|
*
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file is saved. The callback
|
|
|
|
* receives three arguments:
|
|
|
|
* - aRevert (boolean) - tells if the file has been reverted.
|
|
|
|
* - status (number) - the file revert status result (if the file was
|
|
|
|
* saved).
|
|
|
|
*/
|
|
|
|
promptRevert: function SP_promptRervert(aCallback)
|
|
|
|
{
|
|
|
|
if (this.filename) {
|
|
|
|
let ps = Services.prompt;
|
|
|
|
let flags = ps.BUTTON_POS_0 * ps.BUTTON_TITLE_REVERT +
|
|
|
|
ps.BUTTON_POS_1 * ps.BUTTON_TITLE_CANCEL;
|
|
|
|
|
|
|
|
let button = ps.confirmEx(window,
|
|
|
|
this.strings.GetStringFromName("confirmRevert.title"),
|
|
|
|
this.strings.GetStringFromName("confirmRevert"),
|
|
|
|
flags, null, null, null, null, {});
|
|
|
|
if (button == BUTTON_POSITION_CANCEL) {
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (button == BUTTON_POSITION_REVERT) {
|
2013-04-25 22:30:00 +04:00
|
|
|
this.revertFile(aStatus => {
|
|
|
|
if (aCallback) {
|
2012-09-06 08:20:05 +04:00
|
|
|
aCallback(true, aStatus);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback(false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-04-20 12:18:00 +04:00
|
|
|
/**
|
|
|
|
* Open the Error Console.
|
|
|
|
*/
|
2011-05-09 18:51:52 +04:00
|
|
|
openErrorConsole: function SP_openErrorConsole()
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
2013-08-19 22:49:18 +04:00
|
|
|
this.browserWindow.HUDService.toggleBrowserConsole();
|
2011-04-20 12:18:00 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the Web Console.
|
|
|
|
*/
|
2011-05-09 18:51:52 +04:00
|
|
|
openWebConsole: function SP_openWebConsole()
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
2013-07-23 20:58:27 +04:00
|
|
|
let target = TargetFactory.forTab(this.gBrowser.selectedTab);
|
2012-12-13 17:03:55 +04:00
|
|
|
gDevTools.showToolbox(target, "webconsole");
|
2011-04-20 12:18:00 +04:00
|
|
|
this.browserWindow.focus();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current execution context to be the active tab content window.
|
|
|
|
*/
|
2011-05-09 18:51:52 +04:00
|
|
|
setContentContext: function SP_setContentContext()
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
2011-10-26 15:35:32 +04:00
|
|
|
if (this.executionContext == SCRATCHPAD_CONTEXT_CONTENT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-09 18:51:52 +04:00
|
|
|
let content = document.getElementById("sp-menu-content");
|
2011-05-20 01:10:41 +04:00
|
|
|
document.getElementById("sp-menu-browser").removeAttribute("checked");
|
2012-09-14 04:54:02 +04:00
|
|
|
document.getElementById("sp-cmd-reloadAndRun").removeAttribute("disabled");
|
2011-04-20 12:18:00 +04:00
|
|
|
content.setAttribute("checked", true);
|
2011-05-09 18:51:52 +04:00
|
|
|
this.executionContext = SCRATCHPAD_CONTEXT_CONTENT;
|
2011-10-26 15:35:32 +04:00
|
|
|
this.notificationBox.removeAllNotifications(false);
|
2011-04-20 12:18:00 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current execution context to be the most recent chrome window.
|
|
|
|
*/
|
2011-05-20 01:10:41 +04:00
|
|
|
setBrowserContext: function SP_setBrowserContext()
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
2011-10-26 15:35:32 +04:00
|
|
|
if (this.executionContext == SCRATCHPAD_CONTEXT_BROWSER) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-21 15:59:23 +04:00
|
|
|
let browser = document.getElementById("sp-menu-browser");
|
2012-09-14 04:54:02 +04:00
|
|
|
let reloadAndRun = document.getElementById("sp-cmd-reloadAndRun");
|
|
|
|
|
2011-05-09 18:51:52 +04:00
|
|
|
document.getElementById("sp-menu-content").removeAttribute("checked");
|
2012-09-14 04:54:02 +04:00
|
|
|
reloadAndRun.setAttribute("disabled", true);
|
2011-05-21 15:59:23 +04:00
|
|
|
browser.setAttribute("checked", true);
|
2012-09-14 04:54:02 +04:00
|
|
|
|
2011-05-20 01:10:41 +04:00
|
|
|
this.executionContext = SCRATCHPAD_CONTEXT_BROWSER;
|
2011-10-26 15:35:32 +04:00
|
|
|
this.notificationBox.appendNotification(
|
|
|
|
this.strings.GetStringFromName("browserContext.notification"),
|
|
|
|
SCRATCHPAD_CONTEXT_BROWSER,
|
|
|
|
null,
|
|
|
|
this.notificationBox.PRIORITY_WARNING_HIGH,
|
|
|
|
null);
|
2011-04-20 12:18:00 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-08-25 00:44:35 +04:00
|
|
|
* Gets the ID of the inner window of the given DOM window object.
|
2011-04-20 12:18:00 +04:00
|
|
|
*
|
|
|
|
* @param nsIDOMWindow aWindow
|
|
|
|
* @return integer
|
2011-08-25 00:44:35 +04:00
|
|
|
* the inner window ID
|
2011-04-20 12:18:00 +04:00
|
|
|
*/
|
2011-08-25 00:44:35 +04:00
|
|
|
getInnerWindowId: function SP_getInnerWindowId(aWindow)
|
2011-04-20 12:18:00 +04:00
|
|
|
{
|
|
|
|
return aWindow.QueryInterface(Ci.nsIInterfaceRequestor).
|
2011-08-25 00:44:35 +04:00
|
|
|
getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
|
2011-04-20 12:18:00 +04:00
|
|
|
},
|
2011-04-20 00:42:56 +04:00
|
|
|
|
|
|
|
/**
|
2011-12-10 21:03:57 +04:00
|
|
|
* The Scratchpad window load event handler. This method
|
2011-08-12 01:35:10 +04:00
|
|
|
* initializes the Scratchpad window and source editor.
|
|
|
|
*
|
|
|
|
* @param nsIDOMEvent aEvent
|
2011-04-20 00:42:56 +04:00
|
|
|
*/
|
2011-08-12 01:35:10 +04:00
|
|
|
onLoad: function SP_onLoad(aEvent)
|
2011-04-20 00:42:56 +04:00
|
|
|
{
|
2011-08-12 01:35:10 +04:00
|
|
|
if (aEvent.target != document) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-15 00:12:57 +04:00
|
|
|
|
2011-04-20 00:42:56 +04:00
|
|
|
let chrome = Services.prefs.getBoolPref(DEVTOOLS_CHROME_ENABLED);
|
|
|
|
if (chrome) {
|
2011-10-26 15:35:32 +04:00
|
|
|
let environmentMenu = document.getElementById("sp-environment-menu");
|
|
|
|
let errorConsoleCommand = document.getElementById("sp-cmd-errorConsole");
|
|
|
|
let chromeContextCommand = document.getElementById("sp-cmd-browserContext");
|
|
|
|
environmentMenu.removeAttribute("hidden");
|
2011-04-20 00:42:56 +04:00
|
|
|
chromeContextCommand.removeAttribute("disabled");
|
2011-10-26 15:35:32 +04:00
|
|
|
errorConsoleCommand.removeAttribute("disabled");
|
2011-04-20 00:42:56 +04:00
|
|
|
}
|
2011-06-14 14:44:51 +04:00
|
|
|
|
2012-07-31 01:39:00 +04:00
|
|
|
let initialText = this.strings.formatStringFromName(
|
2012-08-01 02:25:29 +04:00
|
|
|
"scratchpadIntro1",
|
2013-10-22 16:00:51 +04:00
|
|
|
[ShortcutUtils.prettifyShortcut(document.getElementById("sp-key-run"), true),
|
|
|
|
ShortcutUtils.prettifyShortcut(document.getElementById("sp-key-inspect"), true),
|
|
|
|
ShortcutUtils.prettifyShortcut(document.getElementById("sp-key-display"), true)],
|
2012-07-31 01:39:00 +04:00
|
|
|
3);
|
|
|
|
|
2012-09-15 00:12:57 +04:00
|
|
|
let args = window.arguments;
|
2013-10-04 23:44:09 +04:00
|
|
|
let state = null;
|
2012-09-15 00:12:57 +04:00
|
|
|
|
|
|
|
if (args && args[0] instanceof Ci.nsIDialogParamBlock) {
|
|
|
|
args = args[0];
|
2013-10-04 23:44:09 +04:00
|
|
|
this._instanceId = args.GetString(0);
|
2012-09-15 00:12:57 +04:00
|
|
|
|
2013-10-04 23:44:09 +04:00
|
|
|
state = args.GetString(1) || null;
|
|
|
|
if (state) {
|
|
|
|
state = JSON.parse(state);
|
|
|
|
this.setState(state);
|
|
|
|
initialText = state.text;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._instanceId = ScratchpadManager.createUid();
|
2011-11-02 23:32:55 +04:00
|
|
|
}
|
|
|
|
|
2014-01-16 04:23:32 +04:00
|
|
|
let config = {
|
2013-09-20 22:34:10 +04:00
|
|
|
mode: Editor.modes.js,
|
|
|
|
value: initialText,
|
|
|
|
lineNumbers: true,
|
2014-01-15 02:12:39 +04:00
|
|
|
showTrailingSpace: Services.prefs.getBoolPref(SHOW_TRAILING_SPACE),
|
2014-01-18 00:58:45 +04:00
|
|
|
enableCodeFolding: Services.prefs.getBoolPref(ENABLE_CODE_FOLDING),
|
2013-09-20 22:34:10 +04:00
|
|
|
contextMenu: "scratchpad-text-popup"
|
2014-01-16 04:23:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
this.editor = new Editor(config);
|
2013-09-20 22:34:10 +04:00
|
|
|
this.editor.appendTo(document.querySelector("#scratchpad-editor")).then(() => {
|
|
|
|
var lines = initialText.split("\n");
|
2011-06-14 14:44:51 +04:00
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
this.editor.on("change", this._onChanged);
|
2014-01-16 00:04:19 +04:00
|
|
|
this.editor.on("save", () => this.saveFile());
|
2013-09-20 22:34:10 +04:00
|
|
|
this.editor.focus();
|
|
|
|
this.editor.setCursor({ line: lines.length, ch: lines.pop().length });
|
2011-12-10 21:03:57 +04:00
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
if (state)
|
|
|
|
this.dirty = !state.saved;
|
2011-06-14 14:44:51 +04:00
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
this.initialized = true;
|
|
|
|
this._triggerObservers("Ready");
|
|
|
|
this.populateRecentFilesMenu();
|
|
|
|
PreferenceObserver.init();
|
2013-10-01 21:03:15 +04:00
|
|
|
CloseObserver.init();
|
2013-09-20 22:34:10 +04:00
|
|
|
}).then(null, (err) => console.log(err.message));
|
2014-03-10 17:00:31 +04:00
|
|
|
this._setupCommandListeners();
|
|
|
|
this._setupPopupShowingListeners();
|
2011-06-14 14:44:51 +04:00
|
|
|
},
|
|
|
|
|
2012-02-17 21:11:17 +04:00
|
|
|
/**
|
2013-09-20 22:34:10 +04:00
|
|
|
* The Source Editor "change" event handler. This function updates the
|
2012-02-17 21:11:17 +04:00
|
|
|
* Scratchpad window title to show an asterisk when there are unsaved changes.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
2013-09-20 22:34:10 +04:00
|
|
|
_onChanged: function SP__onChanged()
|
2012-02-17 21:11:17 +04:00
|
|
|
{
|
|
|
|
Scratchpad._updateTitle();
|
2013-09-20 22:34:10 +04:00
|
|
|
|
2012-09-06 08:20:05 +04:00
|
|
|
if (Scratchpad.filename) {
|
2013-09-20 22:34:10 +04:00
|
|
|
if (Scratchpad.dirty)
|
2012-09-06 08:20:05 +04:00
|
|
|
document.getElementById("sp-cmd-revert").removeAttribute("disabled");
|
2013-09-20 22:34:10 +04:00
|
|
|
else
|
2012-09-06 08:20:05 +04:00
|
|
|
document.getElementById("sp-cmd-revert").setAttribute("disabled", true);
|
|
|
|
}
|
2012-02-17 21:11:17 +04:00
|
|
|
},
|
|
|
|
|
2011-08-12 01:35:10 +04:00
|
|
|
/**
|
|
|
|
* Undo the last action of the user.
|
|
|
|
*/
|
|
|
|
undo: function SP_undo()
|
|
|
|
{
|
|
|
|
this.editor.undo();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redo the previously undone action.
|
|
|
|
*/
|
|
|
|
redo: function SP_redo()
|
|
|
|
{
|
|
|
|
this.editor.redo();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Scratchpad window unload event handler. This method unloads/destroys
|
|
|
|
* the source editor.
|
2011-06-14 14:44:51 +04:00
|
|
|
*
|
2011-08-12 01:35:10 +04:00
|
|
|
* @param nsIDOMEvent aEvent
|
2011-06-14 14:44:51 +04:00
|
|
|
*/
|
2011-08-12 01:35:10 +04:00
|
|
|
onUnload: function SP_onUnload(aEvent)
|
2011-06-14 14:44:51 +04:00
|
|
|
{
|
2011-08-12 01:35:10 +04:00
|
|
|
if (aEvent.target != document) {
|
|
|
|
return;
|
|
|
|
}
|
2011-06-14 14:44:51 +04:00
|
|
|
|
2012-10-16 03:30:37 +04:00
|
|
|
// This event is created only after user uses 'reload and run' feature.
|
2013-09-20 22:34:10 +04:00
|
|
|
if (this._reloadAndRunEvent && this.gBrowser) {
|
2012-10-16 03:30:37 +04:00
|
|
|
this.gBrowser.selectedBrowser.removeEventListener("load",
|
|
|
|
this._reloadAndRunEvent, true);
|
|
|
|
}
|
|
|
|
|
2012-07-03 17:40:12 +04:00
|
|
|
PreferenceObserver.uninit();
|
2013-11-01 02:49:37 +04:00
|
|
|
CloseObserver.uninit();
|
2012-07-03 17:40:12 +04:00
|
|
|
|
2013-09-20 22:34:10 +04:00
|
|
|
this.editor.off("change", this._onChanged);
|
2011-08-12 01:35:10 +04:00
|
|
|
this.editor.destroy();
|
|
|
|
this.editor = null;
|
2013-09-20 22:34:10 +04:00
|
|
|
|
2013-07-23 20:58:27 +04:00
|
|
|
if (this._sidebar) {
|
|
|
|
this._sidebar.destroy();
|
|
|
|
this._sidebar = null;
|
|
|
|
}
|
2013-09-20 22:34:10 +04:00
|
|
|
|
2013-11-06 10:49:22 +04:00
|
|
|
if (this._prettyPrintWorker) {
|
|
|
|
this._prettyPrintWorker.terminate();
|
|
|
|
this._prettyPrintWorker = null;
|
|
|
|
}
|
|
|
|
|
2013-11-02 04:55:13 +04:00
|
|
|
scratchpadTargets = null;
|
2013-07-23 20:58:27 +04:00
|
|
|
this.webConsoleClient = null;
|
|
|
|
this.debuggerClient = null;
|
2011-12-10 21:03:57 +04:00
|
|
|
this.initialized = false;
|
2011-04-20 00:42:56 +04:00
|
|
|
},
|
2011-11-15 12:41:37 +04:00
|
|
|
|
2011-11-24 06:49:03 +04:00
|
|
|
/**
|
|
|
|
* Prompt to save scratchpad if it has unsaved changes.
|
|
|
|
*
|
|
|
|
* @param function aCallback
|
2012-02-17 21:11:17 +04:00
|
|
|
* Optional function you want to call when file is saved. The callback
|
|
|
|
* receives three arguments:
|
|
|
|
* - toClose (boolean) - tells if the window should be closed.
|
|
|
|
* - saved (boolen) - tells if the file has been saved.
|
|
|
|
* - status (number) - the file save status result (if the file was
|
|
|
|
* saved).
|
2011-11-24 06:49:03 +04:00
|
|
|
* @return boolean
|
|
|
|
* Whether the window should be closed
|
|
|
|
*/
|
|
|
|
promptSave: function SP_promptSave(aCallback)
|
|
|
|
{
|
2013-09-20 22:34:10 +04:00
|
|
|
if (this.dirty) {
|
2011-11-24 06:49:03 +04:00
|
|
|
let ps = Services.prompt;
|
|
|
|
let flags = ps.BUTTON_POS_0 * ps.BUTTON_TITLE_SAVE +
|
|
|
|
ps.BUTTON_POS_1 * ps.BUTTON_TITLE_CANCEL +
|
|
|
|
ps.BUTTON_POS_2 * ps.BUTTON_TITLE_DONT_SAVE;
|
|
|
|
|
|
|
|
let button = ps.confirmEx(window,
|
|
|
|
this.strings.GetStringFromName("confirmClose.title"),
|
|
|
|
this.strings.GetStringFromName("confirmClose"),
|
|
|
|
flags, null, null, null, null, {});
|
|
|
|
|
|
|
|
if (button == BUTTON_POSITION_CANCEL) {
|
2012-02-17 21:11:17 +04:00
|
|
|
if (aCallback) {
|
|
|
|
aCallback(false, false);
|
|
|
|
}
|
2011-11-24 06:49:03 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-02-17 21:11:17 +04:00
|
|
|
|
2011-11-24 06:49:03 +04:00
|
|
|
if (button == BUTTON_POSITION_SAVE) {
|
2013-04-25 22:30:00 +04:00
|
|
|
this.saveFile(aStatus => {
|
2012-02-17 21:11:17 +04:00
|
|
|
if (aCallback) {
|
|
|
|
aCallback(true, true, aStatus);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return true;
|
2011-11-24 06:49:03 +04:00
|
|
|
}
|
|
|
|
}
|
2012-02-17 21:11:17 +04:00
|
|
|
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback(true, false);
|
|
|
|
}
|
2011-11-24 06:49:03 +04:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for window close event. Prompts to save scratchpad if
|
|
|
|
* there are unsaved changes.
|
|
|
|
*
|
|
|
|
* @param nsIDOMEvent aEvent
|
2012-10-18 22:00:33 +04:00
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file is saved/closed.
|
|
|
|
* Used mainly for tests.
|
2011-11-24 06:49:03 +04:00
|
|
|
*/
|
2012-10-18 22:00:33 +04:00
|
|
|
onClose: function SP_onClose(aEvent, aCallback)
|
2011-11-24 06:49:03 +04:00
|
|
|
{
|
2012-02-17 21:11:17 +04:00
|
|
|
aEvent.preventDefault();
|
2012-10-18 22:00:33 +04:00
|
|
|
this.close(aCallback);
|
2011-11-24 06:49:03 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the scratchpad window. Prompts before closing if the scratchpad
|
|
|
|
* has unsaved changes.
|
|
|
|
*
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file is saved
|
|
|
|
*/
|
|
|
|
close: function SP_close(aCallback)
|
|
|
|
{
|
2013-10-01 21:03:15 +04:00
|
|
|
let shouldClose;
|
|
|
|
|
2013-04-25 22:30:00 +04:00
|
|
|
this.promptSave((aShouldClose, aSaved, aStatus) => {
|
2013-10-01 21:03:15 +04:00
|
|
|
shouldClose = aShouldClose;
|
2012-02-17 21:11:17 +04:00
|
|
|
if (aSaved && !Components.isSuccessCode(aStatus)) {
|
|
|
|
shouldClose = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldClose) {
|
2013-05-24 14:26:17 +04:00
|
|
|
telemetry.toolClosed("scratchpad");
|
2012-02-17 21:11:17 +04:00
|
|
|
window.close();
|
|
|
|
}
|
2012-10-26 23:05:00 +04:00
|
|
|
|
2012-02-17 21:11:17 +04:00
|
|
|
if (aCallback) {
|
2013-10-01 21:03:15 +04:00
|
|
|
aCallback(shouldClose);
|
2012-02-17 21:11:17 +04:00
|
|
|
}
|
2013-04-25 22:30:00 +04:00
|
|
|
});
|
2013-10-01 21:03:15 +04:00
|
|
|
|
|
|
|
return shouldClose;
|
2011-11-24 06:49:03 +04:00
|
|
|
},
|
|
|
|
|
2014-04-02 19:17:21 +04:00
|
|
|
/**
|
|
|
|
* Toggle a editor's boolean option.
|
|
|
|
*/
|
|
|
|
toggleEditorOption: function SP_toggleEditorOption(optionName)
|
|
|
|
{
|
|
|
|
let newOptionValue = !this.editor.getOption(optionName);
|
|
|
|
this.editor.setOption(optionName, newOptionValue);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increase the editor's font size by 1 px.
|
|
|
|
*/
|
|
|
|
increaseFontSize: function SP_increaseFontSize()
|
|
|
|
{
|
|
|
|
let size = this.editor.getFontSize();
|
|
|
|
|
|
|
|
if (size < MAXIMUM_FONT_SIZE) {
|
|
|
|
this.editor.setFontSize(size + 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrease the editor's font size by 1 px.
|
|
|
|
*/
|
|
|
|
decreaseFontSize: function SP_decreaseFontSize()
|
|
|
|
{
|
|
|
|
let size = this.editor.getFontSize();
|
|
|
|
|
|
|
|
if (size > MINIMUM_FONT_SIZE) {
|
|
|
|
this.editor.setFontSize(size - 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore the editor's original font size.
|
|
|
|
*/
|
|
|
|
normalFontSize: function SP_normalFontSize()
|
|
|
|
{
|
|
|
|
this.editor.setFontSize(NORMAL_FONT_SIZE);
|
|
|
|
},
|
|
|
|
|
2011-11-15 12:41:37 +04:00
|
|
|
_observers: [],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an observer for Scratchpad events.
|
|
|
|
*
|
|
|
|
* The observer implements IScratchpadObserver := {
|
2013-11-20 03:53:13 +04:00
|
|
|
* onReady: Called when the Scratchpad and its Editor are ready.
|
2011-11-15 12:41:37 +04:00
|
|
|
* Arguments: (Scratchpad aScratchpad)
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* All observer handlers are optional.
|
|
|
|
*
|
|
|
|
* @param IScratchpadObserver aObserver
|
|
|
|
* @see removeObserver
|
|
|
|
*/
|
|
|
|
addObserver: function SP_addObserver(aObserver)
|
|
|
|
{
|
|
|
|
this._observers.push(aObserver);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an observer for Scratchpad events.
|
|
|
|
*
|
|
|
|
* @param IScratchpadObserver aObserver
|
|
|
|
* @see addObserver
|
|
|
|
*/
|
|
|
|
removeObserver: function SP_removeObserver(aObserver)
|
|
|
|
{
|
|
|
|
let index = this._observers.indexOf(aObserver);
|
|
|
|
if (index != -1) {
|
|
|
|
this._observers.splice(index, 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger named handlers in Scratchpad observers.
|
|
|
|
*
|
|
|
|
* @param string aName
|
|
|
|
* Name of the handler to trigger.
|
|
|
|
* @param Array aArgs
|
|
|
|
* Optional array of arguments to pass to the observer(s).
|
|
|
|
* @see addObserver
|
|
|
|
*/
|
|
|
|
_triggerObservers: function SP_triggerObservers(aName, aArgs)
|
|
|
|
{
|
|
|
|
// insert this Scratchpad instance as the first argument
|
|
|
|
if (!aArgs) {
|
|
|
|
aArgs = [this];
|
|
|
|
} else {
|
|
|
|
aArgs.unshift(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// trigger all observers that implement this named handler
|
|
|
|
for (let i = 0; i < this._observers.length; ++i) {
|
|
|
|
let observer = this._observers[i];
|
|
|
|
let handler = observer["on" + aName];
|
|
|
|
if (handler) {
|
|
|
|
handler.apply(observer, aArgs);
|
|
|
|
}
|
|
|
|
}
|
2012-02-21 15:09:29 +04:00
|
|
|
},
|
|
|
|
|
2012-10-26 23:05:00 +04:00
|
|
|
/**
|
|
|
|
* Opens the MDN documentation page for Scratchpad.
|
|
|
|
*/
|
2012-02-21 15:09:29 +04:00
|
|
|
openDocumentationPage: function SP_openDocumentationPage()
|
|
|
|
{
|
|
|
|
let url = this.strings.GetStringFromName("help.openDocumentationPage");
|
|
|
|
let newTab = this.gBrowser.addTab(url);
|
|
|
|
this.browserWindow.focus();
|
|
|
|
this.gBrowser.selectedTab = newTab;
|
|
|
|
},
|
2011-04-20 12:18:00 +04:00
|
|
|
};
|
|
|
|
|
2013-07-23 20:58:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the DebuggerClient connection to a specific tab as used by the
|
|
|
|
* Scratchpad.
|
|
|
|
*
|
|
|
|
* @param object aTab
|
|
|
|
* The tab to connect to.
|
|
|
|
*/
|
|
|
|
function ScratchpadTab(aTab)
|
|
|
|
{
|
|
|
|
this._tab = aTab;
|
|
|
|
}
|
|
|
|
|
|
|
|
let scratchpadTargets = new WeakMap();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the object containing the DebuggerClient and WebConsoleClient for a
|
|
|
|
* given tab or window.
|
|
|
|
*
|
|
|
|
* @param object aSubject
|
|
|
|
* The tab or window to obtain the connection for.
|
|
|
|
* @return Promise
|
|
|
|
* The promise for the connection information.
|
|
|
|
*/
|
|
|
|
ScratchpadTab.consoleFor = function consoleFor(aSubject)
|
|
|
|
{
|
|
|
|
if (!scratchpadTargets.has(aSubject)) {
|
|
|
|
scratchpadTargets.set(aSubject, new this(aSubject));
|
|
|
|
}
|
|
|
|
return scratchpadTargets.get(aSubject).connect();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ScratchpadTab.prototype = {
|
|
|
|
/**
|
|
|
|
* The promise for the connection.
|
|
|
|
*/
|
|
|
|
_connector: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a debugger client and connect it to the debugger server.
|
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* The promise for the result of connecting to this tab or window.
|
|
|
|
*/
|
|
|
|
connect: function ST_connect()
|
|
|
|
{
|
|
|
|
if (this._connector) {
|
|
|
|
return this._connector;
|
|
|
|
}
|
|
|
|
|
|
|
|
let deferred = promise.defer();
|
|
|
|
this._connector = deferred.promise;
|
|
|
|
|
|
|
|
let connectTimer = setTimeout(() => {
|
|
|
|
deferred.reject({
|
|
|
|
error: "timeout",
|
|
|
|
message: Scratchpad.strings.GetStringFromName("connectionTimeout"),
|
|
|
|
});
|
|
|
|
}, REMOTE_TIMEOUT);
|
|
|
|
|
|
|
|
deferred.promise.then(() => clearTimeout(connectTimer));
|
|
|
|
|
|
|
|
this._attach().then(aTarget => {
|
|
|
|
let consoleActor = aTarget.form.consoleActor;
|
|
|
|
let client = aTarget.client;
|
|
|
|
client.attachConsole(consoleActor, [], (aResponse, aWebConsoleClient) => {
|
|
|
|
if (aResponse.error) {
|
|
|
|
reportError("attachConsole", aResponse);
|
|
|
|
deferred.reject(aResponse);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
deferred.resolve({
|
|
|
|
webConsoleClient: aWebConsoleClient,
|
|
|
|
debuggerClient: client
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach to this tab.
|
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* The promise for the TabTarget for this tab.
|
|
|
|
*/
|
|
|
|
_attach: function ST__attach()
|
|
|
|
{
|
|
|
|
let target = TargetFactory.forTab(this._tab);
|
|
|
|
return target.makeRemote().then(() => target);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the DebuggerClient connection to a specific window as used by the
|
|
|
|
* Scratchpad.
|
|
|
|
*/
|
|
|
|
function ScratchpadWindow() {}
|
|
|
|
|
|
|
|
ScratchpadWindow.consoleFor = ScratchpadTab.consoleFor;
|
|
|
|
|
|
|
|
ScratchpadWindow.prototype = Heritage.extend(ScratchpadTab.prototype, {
|
|
|
|
/**
|
|
|
|
* Attach to this window.
|
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* The promise for the target for this window.
|
|
|
|
*/
|
|
|
|
_attach: function SW__attach()
|
|
|
|
{
|
|
|
|
let deferred = promise.defer();
|
|
|
|
|
|
|
|
if (!DebuggerServer.initialized) {
|
|
|
|
DebuggerServer.init();
|
|
|
|
DebuggerServer.addBrowserActors();
|
|
|
|
}
|
|
|
|
|
|
|
|
let client = new DebuggerClient(DebuggerServer.connectPipe());
|
|
|
|
client.connect(() => {
|
|
|
|
client.listTabs(aResponse => {
|
|
|
|
if (aResponse.error) {
|
|
|
|
reportError("listTabs", aResponse);
|
|
|
|
deferred.reject(aResponse);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
deferred.resolve({ form: aResponse, client: client });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-10-04 23:44:09 +04:00
|
|
|
function ScratchpadTarget(aTarget)
|
|
|
|
{
|
|
|
|
this._target = aTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScratchpadTarget.consoleFor = ScratchpadTab.consoleFor;
|
|
|
|
|
|
|
|
ScratchpadTarget.prototype = Heritage.extend(ScratchpadTab.prototype, {
|
|
|
|
_attach: function ST__attach()
|
|
|
|
{
|
|
|
|
if (this._target.isRemote) {
|
|
|
|
return promise.resolve(this._target);
|
|
|
|
}
|
|
|
|
return this._target.makeRemote().then(() => this._target);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-05-07 03:11:44 +04:00
|
|
|
/**
|
|
|
|
* Encapsulates management of the sidebar containing the VariablesView for
|
|
|
|
* object inspection.
|
|
|
|
*/
|
2013-06-18 01:55:25 +04:00
|
|
|
function ScratchpadSidebar(aScratchpad)
|
2013-05-07 03:11:44 +04:00
|
|
|
{
|
2013-07-23 20:58:27 +04:00
|
|
|
let ToolSidebar = require("devtools/framework/sidebar").ToolSidebar;
|
2013-05-07 03:11:44 +04:00
|
|
|
let tabbox = document.querySelector("#scratchpad-sidebar");
|
2013-06-14 03:41:00 +04:00
|
|
|
this._sidebar = new ToolSidebar(tabbox, this, "scratchpad");
|
2013-06-18 01:55:25 +04:00
|
|
|
this._scratchpad = aScratchpad;
|
2013-05-07 03:11:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ScratchpadSidebar.prototype = {
|
|
|
|
/*
|
|
|
|
* The ToolSidebar for this sidebar.
|
|
|
|
*/
|
|
|
|
_sidebar: null,
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The VariablesView for this sidebar.
|
|
|
|
*/
|
|
|
|
variablesView: null,
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Whether the sidebar is currently shown.
|
|
|
|
*/
|
|
|
|
visible: false,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the sidebar, if not open already, and populate it with the properties
|
|
|
|
* of the given object.
|
|
|
|
*
|
|
|
|
* @param string aString
|
|
|
|
* The string that was evaluated.
|
|
|
|
* @param object aObject
|
|
|
|
* The object to inspect, which is the aEvalString evaluation result.
|
|
|
|
* @return Promise
|
|
|
|
* A promise that will resolve once the sidebar is open.
|
|
|
|
*/
|
|
|
|
open: function SS_open(aEvalString, aObject)
|
|
|
|
{
|
|
|
|
this.show();
|
|
|
|
|
2013-07-11 11:12:20 +04:00
|
|
|
let deferred = promise.defer();
|
2013-05-07 03:11:44 +04:00
|
|
|
|
|
|
|
let onTabReady = () => {
|
2013-07-23 20:58:27 +04:00
|
|
|
if (this.variablesView) {
|
|
|
|
this.variablesView.controller.releaseActors();
|
|
|
|
}
|
|
|
|
else {
|
2013-05-07 03:11:44 +04:00
|
|
|
let window = this._sidebar.getWindowForTab("variablesview");
|
|
|
|
let container = window.document.querySelector("#variables");
|
2013-07-23 20:58:27 +04:00
|
|
|
|
2013-06-18 01:55:25 +04:00
|
|
|
this.variablesView = new VariablesView(container, {
|
|
|
|
searchEnabled: true,
|
|
|
|
searchPlaceholder: this._scratchpad.strings
|
|
|
|
.GetStringFromName("propertiesFilterPlaceholder")
|
|
|
|
});
|
2013-07-23 20:58:27 +04:00
|
|
|
|
|
|
|
VariablesViewController.attach(this.variablesView, {
|
2013-09-25 20:03:17 +04:00
|
|
|
getEnvironmentClient: aGrip => {
|
|
|
|
return new EnvironmentClient(this._scratchpad.debuggerClient, aGrip);
|
|
|
|
},
|
2013-08-13 20:08:30 +04:00
|
|
|
getObjectClient: aGrip => {
|
|
|
|
return new ObjectClient(this._scratchpad.debuggerClient, aGrip);
|
2013-07-23 20:58:27 +04:00
|
|
|
},
|
|
|
|
getLongStringClient: aActor => {
|
|
|
|
return this._scratchpad.webConsoleClient.longString(aActor);
|
|
|
|
},
|
|
|
|
releaseActor: aActor => {
|
|
|
|
this._scratchpad.debuggerClient.release(aActor);
|
|
|
|
}
|
|
|
|
});
|
2013-05-07 03:11:44 +04:00
|
|
|
}
|
|
|
|
this._update(aObject).then(() => deferred.resolve());
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this._sidebar.getCurrentTabID() == "variablesview") {
|
|
|
|
onTabReady();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._sidebar.once("variablesview-ready", onTabReady);
|
|
|
|
this._sidebar.addTab("variablesview", VARIABLES_VIEW_URL, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the sidebar.
|
|
|
|
*/
|
|
|
|
show: function SS_show()
|
|
|
|
{
|
|
|
|
if (!this.visible) {
|
|
|
|
this.visible = true;
|
|
|
|
this._sidebar.show();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide the sidebar.
|
|
|
|
*/
|
|
|
|
hide: function SS_hide()
|
|
|
|
{
|
|
|
|
if (this.visible) {
|
|
|
|
this.visible = false;
|
|
|
|
this._sidebar.hide();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-07-23 20:58:27 +04:00
|
|
|
/**
|
|
|
|
* Destroy the sidebar.
|
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* The promise that resolves when the sidebar is destroyed.
|
|
|
|
*/
|
|
|
|
destroy: function SS_destroy()
|
|
|
|
{
|
|
|
|
if (this.variablesView) {
|
|
|
|
this.variablesView.controller.releaseActors();
|
|
|
|
this.variablesView = null;
|
|
|
|
}
|
|
|
|
return this._sidebar.destroy();
|
|
|
|
},
|
|
|
|
|
2013-05-07 03:11:44 +04:00
|
|
|
/**
|
|
|
|
* Update the object currently inspected by the sidebar.
|
|
|
|
*
|
|
|
|
* @param object aObject
|
|
|
|
* The object to inspect in the sidebar.
|
|
|
|
* @return Promise
|
|
|
|
* A promise that resolves when the update completes.
|
|
|
|
*/
|
|
|
|
_update: function SS__update(aObject)
|
|
|
|
{
|
2013-10-02 00:17:20 +04:00
|
|
|
let options = { objectActor: aObject };
|
2013-07-23 20:58:27 +04:00
|
|
|
let view = this.variablesView;
|
|
|
|
view.empty();
|
2013-10-02 00:17:20 +04:00
|
|
|
return view.controller.setSingleVariable(options).expanded;
|
2013-05-07 03:11:44 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-07-23 20:58:27 +04:00
|
|
|
* Report an error coming over the remote debugger protocol.
|
|
|
|
*
|
|
|
|
* @param string aAction
|
|
|
|
* The name of the action or method that failed.
|
|
|
|
* @param object aResponse
|
|
|
|
* The response packet that contains the error.
|
2013-05-07 03:11:44 +04:00
|
|
|
*/
|
2013-07-23 20:58:27 +04:00
|
|
|
function reportError(aAction, aResponse)
|
2013-05-07 03:11:44 +04:00
|
|
|
{
|
2013-07-23 20:58:27 +04:00
|
|
|
Cu.reportError(aAction + " failed: " + aResponse.error + " " +
|
|
|
|
aResponse.message);
|
2013-05-07 03:11:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 17:40:12 +04:00
|
|
|
/**
|
|
|
|
* The PreferenceObserver listens for preference changes while Scratchpad is
|
|
|
|
* running.
|
|
|
|
*/
|
|
|
|
var PreferenceObserver = {
|
|
|
|
_initialized: false,
|
|
|
|
|
|
|
|
init: function PO_init()
|
|
|
|
{
|
|
|
|
if (this._initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.branch = Services.prefs.getBranch("devtools.scratchpad.");
|
|
|
|
this.branch.addObserver("", this, false);
|
|
|
|
this._initialized = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function PO_observe(aMessage, aTopic, aData)
|
|
|
|
{
|
|
|
|
if (aTopic != "nsPref:changed") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aData == "recentFilesMax") {
|
|
|
|
Scratchpad.handleRecentFileMaxChange();
|
|
|
|
}
|
|
|
|
else if (aData == "recentFilePaths") {
|
|
|
|
Scratchpad.populateRecentFilesMenu();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function PO_uninit () {
|
|
|
|
if (!this.branch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.branch.removeObserver("", this);
|
|
|
|
this.branch = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-01 21:03:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The CloseObserver listens for the last browser window closing and attempts to
|
|
|
|
* close the Scratchpad.
|
|
|
|
*/
|
|
|
|
var CloseObserver = {
|
|
|
|
init: function CO_init()
|
|
|
|
{
|
|
|
|
Services.obs.addObserver(this, "browser-lastwindow-close-requested", false);
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function CO_observe(aSubject)
|
|
|
|
{
|
|
|
|
if (Scratchpad.close()) {
|
|
|
|
this.uninit();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aSubject.QueryInterface(Ci.nsISupportsPRBool);
|
|
|
|
aSubject.data = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function CO_uninit()
|
|
|
|
{
|
|
|
|
Services.obs.removeObserver(this, "browser-lastwindow-close-requested",
|
|
|
|
false);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2011-05-09 18:51:52 +04:00
|
|
|
XPCOMUtils.defineLazyGetter(Scratchpad, "strings", function () {
|
|
|
|
return Services.strings.createBundle(SCRATCHPAD_L10N);
|
2011-04-20 12:18:00 +04:00
|
|
|
});
|
|
|
|
|
2011-12-10 21:03:57 +04:00
|
|
|
addEventListener("load", Scratchpad.onLoad.bind(Scratchpad), false);
|
2011-08-12 01:35:10 +04:00
|
|
|
addEventListener("unload", Scratchpad.onUnload.bind(Scratchpad), false);
|
2011-11-24 06:49:03 +04:00
|
|
|
addEventListener("close", Scratchpad.onClose.bind(Scratchpad), false);
|