Permalink
Browse files

Issue 5817: API for registering new Firebug commands

  • Loading branch information...
janodvarko committed Sep 17, 2012
1 parent 085ebe3 commit 306663ae5fd99d7b614578a861690c7b463b69f3
@@ -1682,7 +1682,7 @@ FirebugReps.EventLog = domplate(FirebugReps.Event,
supportsObject: function(object, type)
{
- return object instanceof Firebug.EventMonitor.EventLog;
+ return object instanceof EventMonitor.EventLog;
},
});
@@ -18,7 +18,6 @@ define([
"firebug/lib/xml",
"firebug/lib/array",
"firebug/lib/persist",
- "firebug/console/eventMonitor",
"firebug/lib/keywords",
"firebug/console/console",
"firebug/console/commandLineHelp",
@@ -27,7 +26,8 @@ define([
"firebug/console/commandHistory"
],
function(Obj, Firebug, FirebugReps, Locale, Events, Wrapper, Url, Css, Dom, Firefox, Win, System,
- Xpath, Str, Xml, Arr, Persist, EventMonitor, Keywords, Console, CommandLineHelp) {
+ Xpath, Str, Xml, Arr, Persist, Keywords, Console, CommandLineHelp,
+ CommandLineExposed) {
// ********************************************************************************************* //
// Constants
@@ -1199,42 +1199,12 @@ function FirebugCommandLineAPI(context)
return Firebug.Console.getDefaultReturnValue(context.window);
};
- this.monitorEvents = function(object, types)
- {
- EventMonitor.monitorEvents(object, types, context);
- return Firebug.Console.getDefaultReturnValue(context.window);
- };
-
- this.unmonitorEvents = function(object, types)
- {
- EventMonitor.unmonitorEvents(object, types, context);
- return Firebug.Console.getDefaultReturnValue(context.window);
- };
-
- this.profile = function(title)
- {
- Firebug.Profiler.startProfiling(context, title);
- return Firebug.Console.getDefaultReturnValue(context.window);
- };
-
- this.profileEnd = function()
- {
- Firebug.Profiler.stopProfiling(context);
- return Firebug.Console.getDefaultReturnValue(context.window);
- };
-
this.copy = function(x)
{
System.copyToClipboard(x);
return Firebug.Console.getDefaultReturnValue(context.window);
};
- this.help = function()
- {
- CommandLineHelp.render(context);
- return Firebug.Console.getDefaultReturnValue(context.window);
- };
-
// xxxHonza: removed from 1.10 (issue 5599)
/*this.memoryProfile = function(title)
{
@@ -1247,6 +1217,35 @@ function FirebugCommandLineAPI(context)
Firebug.MemoryProfiler.stop(context);
return Firebug.Console.getDefaultReturnValue(context.window);
};*/
+
+ function createHandler(config, name)
+ {
+ return function()
+ {
+ try
+ {
+ return config.handler.call(null, context, arguments);
+ }
+ catch (err)
+ {
+ Firebug.Console.log(err, context, "errorMessage");
+
+ if (FBTrace.DBG_ERRORS)
+ {
+ FBTrace.sysout("commandLine.api; EXCEPTION when executing " +
+ "a command: " + name + ", " + err, err);
+ }
+ }
+ }
+ }
+
+ // Register user commands.
+ var commands = CommandLineExposed.userCommands;
+ for (var name in commands)
+ {
+ var config = commands[name];
+ this[name] = createHandler(config, name);
+ }
}
// ********************************************************************************************* //
@@ -1,26 +1,27 @@
/* See license.txt for terms of usage */
define([
- "firebug/firebug",
"firebug/lib/wrapper",
"firebug/lib/events",
],
-function(Firebug, Wrapper, Events) {
+function(Wrapper, Events) {
// ********************************************************************************************* //
// Command Line APIs
// List of command line APIs
var commands = ["$", "$$", "$x", "$n", "cd", "clear", "inspect", "keys",
"values", "debug", "undebug", "monitor", "unmonitor", "traceCalls", "untraceCalls",
- "traceAll", "untraceAll", "monitorEvents", "unmonitorEvents", "profile", "profileEnd",
- "copy" /*, "memoryProfile", "memoryProfileEnd"*/];
+ "traceAll", "untraceAll", "copy" /*, "memoryProfile", "memoryProfileEnd"*/];
// List of shortcuts for some console methods
var consoleShortcuts = ["dir", "dirxml", "table"];
// List of console variables.
-var props = ["$0", "$1", "help"];
+var props = ["$0", "$1"];
+
+// Registered commands, name -> config object.
+var userCommands = {};
// ********************************************************************************************* //
// Command Line Implementation
@@ -52,6 +53,24 @@ function createFirebugCommandLine(context, win)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Exposed Properties
+ function createCommandHandler(cmd) {
+ return function() {
+ return notifyFirebug(arguments, cmd, "firebugExecuteCommand");
+ }
+ }
+
+ function createShortcutHandler(cmd) {
+ return function() {
+ return console[cmd].apply(console, arguments);
+ }
+ }
+
+ function createVariableHandler(prop) {
+ return function() {
+ return notifyFirebug(arguments, prop, "firebugExecuteCommand");
+ }
+ }
+
// Define command line methods
for (var i=0; i<commands.length; i++)
{
@@ -61,12 +80,6 @@ function createFirebugCommandLine(context, win)
if (command in contentView)
continue;
- function createCommandHandler(cmd) {
- return function() {
- return notifyFirebug(arguments, cmd, "firebugExecuteCommand");
- }
- }
-
commandLine[command] = createCommandHandler(command);
commandLine.__exposedProps__[command] = "rw";
}
@@ -82,12 +95,6 @@ function createFirebugCommandLine(context, win)
if (command in contentView)
continue;
- function createShortcutHandler(cmd) {
- return function() {
- return console[cmd].apply(console, arguments);
- }
- }
-
commandLine[command] = createShortcutHandler(command);
commandLine.__exposedProps__[command] = "rw";

This comment has been minimized.

Show comment
Hide comment
@simonlindholm

simonlindholm Sep 17, 2012

Member

Maybe these should also be "r" - the alternative is to have writes be silently discarded when the command line object is detached.

@simonlindholm

simonlindholm Sep 17, 2012

Member

Maybe these should also be "r" - the alternative is to have writes be silently discarded when the command line object is detached.

This comment has been minimized.

Show comment
Hide comment
@janodvarko

janodvarko Sep 18, 2012

Member

Done at 25b2891
Honza

@janodvarko

janodvarko Sep 18, 2012

Member

Done at 25b2891
Honza

}
@@ -99,16 +106,31 @@ function createFirebugCommandLine(context, win)
if (prop in contentView)
continue;
- function createVariableHandler(prop) {
- return function() {
- return notifyFirebug(arguments, prop, "firebugExecuteCommand");
- }
- }
-
commandLine.__defineGetter__(prop, createVariableHandler(prop));
commandLine.__exposedProps__[prop] = "r";
}
+ // Define user registered commands.
+ for (var name in userCommands)
+ {
+ // If the method is already defined, don't override it.
+ if (name in contentView)
+ continue;
+
+ var config = userCommands[name];
+
+ if (config.getter)
+ {
+ commandLine.__defineGetter__(name, createVariableHandler(name));
+ commandLine.__exposedProps__[name] = "r";
+ }
+ else
+ {
+ commandLine[name] = createCommandHandler(name);
+ commandLine.__exposedProps__[name] = "rw";

This comment has been minimized.

Show comment
Hide comment
@simonlindholm

simonlindholm Sep 18, 2012

Member

Another "rw" here.

@simonlindholm

simonlindholm Sep 18, 2012

Member

Another "rw" here.

This comment has been minimized.

Show comment
Hide comment
+ }
+ }
+
attachCommandLine();
// xxxHonza: TODO make this private.
@@ -246,6 +268,43 @@ script.src = evalFileSrc;
document.documentElement.appendChild(script);
*/
+// ********************************************************************************************* //
+// User Commands
+
+function registerCommand(name, config)
+{
+ if (commands[name] || consoleShortcuts[name] || props[name] || userCommands[name])
+ {
+ if (FBTrace.DBG_ERRORS)
+ {
+ FBTrace.sysout("firebug.registerCommand; ERROR This command is already " +
+ "registered: " + name);
+ }
+
+ return false;
+ }
+
+ userCommands[name] = config;
+ return true;
+}
+
+function unregisterCommand(name)
+{
+ if (!userCommands[name])
+ {
+ if (FBTrace.DBG_ERRORS)
+ {
+ FBTrace.sysout("firebug.unregisterCommand; ERROR This command is not " +
+ "registered: " + name);
+ }
+
+ return false;
+ }
+
+ delete userCommands[name];
+ return true;
+}
+
// ********************************************************************************************* //
// Registration
@@ -255,6 +314,9 @@ Firebug.CommandLineExposed =
commands: commands,
consoleShortcuts: consoleShortcuts,
properties: props,
+ userCommands: userCommands,
+ registerCommand: registerCommand,
+ unregisterCommand: unregisterCommand,
};
return Firebug.CommandLineExposed;
@@ -86,7 +86,12 @@ var HelpEntry = domplate(
onClick: function(event)
{
var object = Firebug.getRepObject(event.target);
- Win.openNewTab("http://getfirebug.com/wiki/index.php/" + object.name);
+
+ var helpUrl = "http://getfirebug.com/wiki/index.php/" + object.name;
+ if (object.helpUrl)
+ helpUrl = object.helpUrl;

This comment has been minimized.

Show comment
Hide comment
@simonlindholm

simonlindholm Sep 17, 2012

Member

Perhaps add an option for "no help available"? Otherwise non-Firebug extenders end up at non-existent Wiki pages if they don't add helpUrls.

@simonlindholm

simonlindholm Sep 17, 2012

Member

Perhaps add an option for "no help available"? Otherwise non-Firebug extenders end up at non-existent Wiki pages if they don't add helpUrls.

This comment has been minimized.

Show comment
Hide comment
@janodvarko

janodvarko Sep 18, 2012

Member

Yep agree, good idea.
5411494

There is a message box saying that help page is not available.

We could also disable the link and have a tooltip?
If you prefer this or any other better solution, please create a new issue and I'll fix it.

Thanks
Honza

@janodvarko

janodvarko Sep 18, 2012

Member

Yep agree, good idea.
5411494

There is a message box saying that help page is not available.

We could also disable the link and have a tooltip?
If you prefer this or any other better solution, please create a new issue and I'll fix it.

Thanks
Honza

+
+ Win.openNewTab(helpUrl);
},
getName: function(object)
@@ -99,6 +104,9 @@ var HelpEntry = domplate(
getDesc: function(object)
{
+ if (object.nol10n)
+ return object.desc;
+
return Locale.$STR(object.desc);
}
});
@@ -147,15 +155,42 @@ var CommandLineHelp = domplate(
})
}
+ for (var name in CommandLineExposed.userCommands)
+ {
+ var config = CommandLineExposed.userCommands[name];
+ commands.push({
+ name: name,
+ desc: config.description,
+ nol10n: true,
+ helpUrl: config.helpUrl ? config.helpUrl: null,
+ type: config.getter ? CMD_TYPE_PROPERTY : CMD_TYPE_COMMAND,
+ })
+ }
+
HelpEntry.tag.insertRows({commands: commands}, tBody);
return row;
}
});
+// ********************************************************************************************* //
+// Command Implementation
+
+function onExecuteCommand(context)
+{
+ CommandLineHelp.render(context);
+ return Firebug.Console.getDefaultReturnValue(context.window);
+}
+
// ********************************************************************************************* //
// Registration
+Firebug.registerCommand("help", {
+ getter: true,
+ handler: onExecuteCommand.bind(this),
+ description: Locale.$STR("console.cmd.help.help")
+});
+
return CommandLineHelp;
// ********************************************************************************************* //
Oops, something went wrong.

1 comment on commit 306663a

@janodvarko

This comment has been minimized.

Show comment
Hide comment
@janodvarko

janodvarko Sep 18, 2012

Member

Thanks for your comments Simon.
Honza

Member

janodvarko commented on 306663a Sep 18, 2012

Thanks for your comments Simon.
Honza

Please sign in to comment.