Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Issue 5817: API for registering new Firebug commands

  • Loading branch information...
commit 306663ae5fd99d7b614578a861690c7b463b69f3 1 parent 085ebe3
@janodvarko janodvarko authored
View
2  extension/content/firebug/chrome/reps.js
@@ -1682,7 +1682,7 @@ FirebugReps.EventLog = domplate(FirebugReps.Event,
supportsObject: function(object, type)
{
- return object instanceof Firebug.EventMonitor.EventLog;
+ return object instanceof EventMonitor.EventLog;
},
});
View
63 extension/content/firebug/console/commandLine.js
@@ -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);
+ }
}
// ********************************************************************************************* //
View
108 extension/content/firebug/console/commandLineExposed.js
@@ -1,11 +1,10 @@
/* 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
@@ -13,14 +12,16 @@ function(Firebug, Wrapper, Events) {
// 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";
@simonlindholm Owner

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

@janodvarko Owner

Done at 25b2891
Honza

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
}
@@ -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";
@simonlindholm Owner

Another "rw" here.

@janodvarko Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+ }
+
attachCommandLine();
// xxxHonza: TODO make this private.
@@ -247,6 +269,43 @@ 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
Firebug.CommandLineExposed =
@@ -255,6 +314,9 @@ Firebug.CommandLineExposed =
commands: commands,
consoleShortcuts: consoleShortcuts,
properties: props,
+ userCommands: userCommands,
+ registerCommand: registerCommand,
+ unregisterCommand: unregisterCommand,
};
return Firebug.CommandLineExposed;
View
37 extension/content/firebug/console/commandLineHelp.js
@@ -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;
@simonlindholm Owner

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.

@janodvarko Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ 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,6 +155,18 @@ 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;
@@ -154,8 +174,23 @@ var CommandLineHelp = domplate(
});
// ********************************************************************************************* //
+// 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;
// ********************************************************************************************* //
View
46 extension/content/firebug/console/eventMonitor.js
@@ -5,13 +5,14 @@ define([
"firebug/firebug",
"firebug/lib/trace",
"firebug/lib/events",
+ "firebug/lib/locale",
],
-function(Obj, Firebug, FBTrace, Events) {
+function(Obj, Firebug, FBTrace, Events, Locale) {
// ********************************************************************************************* //
// EventMonitor Module
-Firebug.EventMonitor = Obj.extend(Firebug.Module,
+var EventMonitor = Obj.extend(Firebug.Module,
{
dispatchName: "eventMonitor",
@@ -157,14 +158,14 @@ Firebug.EventMonitor = Obj.extend(Firebug.Module,
onMonitorEvent: function(event, context)
{
- var obj = new Firebug.EventMonitor.EventLog(event);
+ var obj = new EventMonitor.EventLog(event);
Firebug.Console.log(obj, context);
}
});
// ********************************************************************************************* //
-Firebug.EventMonitor.EventLog = function(event)
+EventMonitor.EventLog = function(event)
{
this.event = event;
}
@@ -207,11 +208,42 @@ function getMonitoredEventTypes(types)
}
// ********************************************************************************************* //
-// Registration & Export
+// CommandLine Support
-Firebug.registerModule(Firebug.EventMonitor);
+function monitorEvents(context, args)
+{
+ var object = args[0];
+ var types = args[1];
+
+ EventMonitor.monitorEvents(object, types, context);
+ return Firebug.Console.getDefaultReturnValue(context.window);
+}
+
+function unmonitorEvents(context, args)
+{
+ var object = args[0];
+ var types = args[1];
+
+ EventMonitor.unmonitorEvents(object, types, context);
+ return Firebug.Console.getDefaultReturnValue(context.window);
+}
+
+// ********************************************************************************************* //
+// Registration
+
+Firebug.registerModule(EventMonitor);
+
+Firebug.registerCommand("monitorEvents", {
+ handler: monitorEvents.bind(this),
+ description: Locale.$STR("console.cmd.help.monitorEvents")
+})
+
+Firebug.registerCommand("unmonitorEvents", {
+ handler: unmonitorEvents.bind(this),
+ description: Locale.$STR("console.cmd.help.unmonitorEvents")
+})
-return Firebug.EventMonitor;
+return EventMonitor;
// ********************************************************************************************* //
});
View
26 extension/content/firebug/console/profiler.js
@@ -513,11 +513,37 @@ function ProfileCall(script, context, callCount, totalTime, totalOwnTime, minTim
}
// ********************************************************************************************* //
+// CommandLine Support
+
+function profile(context, args)
+{
+ var title = args[0];
+ Firebug.Profiler.startProfiling(context, title);
+ return Firebug.Console.getDefaultReturnValue(context.window);
+};
+
+function profileEnd(context)
+{
+ Firebug.Profiler.stopProfiling(context);
+ return Firebug.Console.getDefaultReturnValue(context.window);
+};
+
+// ********************************************************************************************* //
// Registration
Firebug.registerModule(Firebug.Profiler);
Firebug.registerRep(Firebug.Profiler.ProfileCall);
+Firebug.registerCommand("profile", {
+ handler: profile.bind(this),
+ description: Locale.$STR("console.cmd.help.profile")
+})
+
+Firebug.registerCommand("profileEnd", {
+ handler: profileEnd.bind(this),
+ description: Locale.$STR("console.cmd.help.profileEnd")
+})
+
return Firebug.Profiler;
// ********************************************************************************************* //
View
14 extension/content/firebug/firebug.js
@@ -22,9 +22,10 @@ define([
"firebug/lib/dom",
"firebug/lib/http",
"firebug/trace/traceListener",
+ "firebug/console/commandLineExposed",
],
function(FBL, Obj, Firefox, ChromeFactory, Domplate, Options, Locale, Events,
- Wrapper, Url, Css, Win, Str, Arr, Dom, Http, TraceListener) {
+ Wrapper, Url, Css, Win, Str, Arr, Dom, Http, TraceListener, CommandLineExposed) {
// ********************************************************************************************* //
// Constants
@@ -89,6 +90,7 @@ window.Firebug =
earlyRegPanelTypes: earlyRegPanelTypes,
uiListeners: [],
reps: reps,
+ commands: {},
@simonlindholm Owner

Looks unused.

@janodvarko Owner

Nice catch Simon!
Fixed at 8bbd96a
Thanks
Honza

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
stringCropLength: 50,
@@ -652,6 +654,16 @@ window.Firebug =
Firebug.TraceModule.removeListener(listener);
},
+ registerCommand: function(name, config)
+ {
+ return CommandLineExposed.registerCommand(name, config);
+ },
+
+ unregistereCommand: function(name)
+ {
+ return CommandLineExposed.unregisterCommand(name);
+ },
+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Options

1 comment on commit 306663a

@janodvarko
Owner

Thanks for your comments Simon.
Honza

Please sign in to comment.
Something went wrong with that request. Please try again.