Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Issue 5817: API for registering new Firebug commands

  • Loading branch information...
commit 306663ae5fd99d7b614578a861690c7b463b69f3 1 parent 085ebe3
Jan Odvarko authored September 17, 2012
2  extension/content/firebug/chrome/reps.js
@@ -1682,7 +1682,7 @@ FirebugReps.EventLog = domplate(FirebugReps.Event,
1682 1682
 
1683 1683
     supportsObject: function(object, type)
1684 1684
     {
1685  
-        return object instanceof Firebug.EventMonitor.EventLog;
  1685
+        return object instanceof EventMonitor.EventLog;
1686 1686
     },
1687 1687
 });
1688 1688
 
63  extension/content/firebug/console/commandLine.js
@@ -18,7 +18,6 @@ define([
18 18
     "firebug/lib/xml",
19 19
     "firebug/lib/array",
20 20
     "firebug/lib/persist",
21  
-    "firebug/console/eventMonitor",
22 21
     "firebug/lib/keywords",
23 22
     "firebug/console/console",
24 23
     "firebug/console/commandLineHelp",
@@ -27,7 +26,8 @@ define([
27 26
     "firebug/console/commandHistory"
28 27
 ],
29 28
 function(Obj, Firebug, FirebugReps, Locale, Events, Wrapper, Url, Css, Dom, Firefox, Win, System,
30  
-    Xpath, Str, Xml, Arr, Persist, EventMonitor, Keywords, Console, CommandLineHelp) {
  29
+    Xpath, Str, Xml, Arr, Persist, Keywords, Console, CommandLineHelp,
  30
+    CommandLineExposed) {
31 31
 
32 32
 // ********************************************************************************************* //
33 33
 // Constants
@@ -1199,42 +1199,12 @@ function FirebugCommandLineAPI(context)
1199 1199
         return Firebug.Console.getDefaultReturnValue(context.window);
1200 1200
     };
1201 1201
 
1202  
-    this.monitorEvents = function(object, types)
1203  
-    {
1204  
-        EventMonitor.monitorEvents(object, types, context);
1205  
-        return Firebug.Console.getDefaultReturnValue(context.window);
1206  
-    };
1207  
-
1208  
-    this.unmonitorEvents = function(object, types)
1209  
-    {
1210  
-        EventMonitor.unmonitorEvents(object, types, context);
1211  
-        return Firebug.Console.getDefaultReturnValue(context.window);
1212  
-    };
1213  
-
1214  
-    this.profile = function(title)
1215  
-    {
1216  
-        Firebug.Profiler.startProfiling(context, title);
1217  
-        return Firebug.Console.getDefaultReturnValue(context.window);
1218  
-    };
1219  
-
1220  
-    this.profileEnd = function()
1221  
-    {
1222  
-        Firebug.Profiler.stopProfiling(context);
1223  
-        return Firebug.Console.getDefaultReturnValue(context.window);
1224  
-    };
1225  
-
1226 1202
     this.copy = function(x)
1227 1203
     {
1228 1204
         System.copyToClipboard(x);
1229 1205
         return Firebug.Console.getDefaultReturnValue(context.window);
1230 1206
     };
1231 1207
 
1232  
-    this.help = function()
1233  
-    {
1234  
-        CommandLineHelp.render(context);
1235  
-        return Firebug.Console.getDefaultReturnValue(context.window);
1236  
-    };
1237  
-
1238 1208
     // xxxHonza: removed from 1.10 (issue 5599)
1239 1209
     /*this.memoryProfile = function(title)
1240 1210
     {
@@ -1247,6 +1217,35 @@ function FirebugCommandLineAPI(context)
1247 1217
         Firebug.MemoryProfiler.stop(context);
1248 1218
         return Firebug.Console.getDefaultReturnValue(context.window);
1249 1219
     };*/
  1220
+
  1221
+    function createHandler(config, name)
  1222
+    {
  1223
+        return function()
  1224
+        {
  1225
+            try
  1226
+            {
  1227
+                return config.handler.call(null, context, arguments);
  1228
+            }
  1229
+            catch (err)
  1230
+            {
  1231
+                Firebug.Console.log(err, context, "errorMessage");
  1232
+
  1233
+                if (FBTrace.DBG_ERRORS)
  1234
+                {
  1235
+                    FBTrace.sysout("commandLine.api; EXCEPTION when executing " +
  1236
+                        "a command: " + name + ", " + err, err);
  1237
+                }
  1238
+            }
  1239
+        }
  1240
+    }
  1241
+
  1242
+    // Register user commands.
  1243
+    var commands = CommandLineExposed.userCommands;
  1244
+    for (var name in commands)
  1245
+    {
  1246
+        var config = commands[name];
  1247
+        this[name] = createHandler(config, name);
  1248
+    }
1250 1249
 }
1251 1250
 
1252 1251
 // ********************************************************************************************* //
108  extension/content/firebug/console/commandLineExposed.js
... ...
@@ -1,11 +1,10 @@
1 1
 /* See license.txt for terms of usage */
2 2
 
3 3
 define([
4  
-    "firebug/firebug",
5 4
     "firebug/lib/wrapper",
6 5
     "firebug/lib/events",
7 6
 ],
8  
-function(Firebug, Wrapper, Events) {
  7
+function(Wrapper, Events) {
9 8
 
10 9
 // ********************************************************************************************* //
11 10
 // Command Line APIs
@@ -13,14 +12,16 @@ function(Firebug, Wrapper, Events) {
13 12
 // List of command line APIs
14 13
 var commands = ["$", "$$", "$x", "$n", "cd", "clear", "inspect", "keys",
15 14
     "values", "debug", "undebug", "monitor", "unmonitor", "traceCalls", "untraceCalls",
16  
-    "traceAll", "untraceAll", "monitorEvents", "unmonitorEvents", "profile", "profileEnd",
17  
-    "copy" /*, "memoryProfile", "memoryProfileEnd"*/];
  15
+    "traceAll", "untraceAll", "copy" /*, "memoryProfile", "memoryProfileEnd"*/];
18 16
 
19 17
 // List of shortcuts for some console methods
20 18
 var consoleShortcuts = ["dir", "dirxml", "table"];
21 19
 
22 20
 // List of console variables.
23  
-var props = ["$0", "$1", "help"];
  21
+var props = ["$0", "$1"];
  22
+
  23
+// Registered commands, name -> config object.
  24
+var userCommands = {};
24 25
 
25 26
 // ********************************************************************************************* //
26 27
 // Command Line Implementation
@@ -52,6 +53,24 @@ function createFirebugCommandLine(context, win)
52 53
     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53 54
     // Exposed Properties
54 55
 
  56
+    function createCommandHandler(cmd) {
  57
+        return function() {
  58
+            return notifyFirebug(arguments, cmd, "firebugExecuteCommand");
  59
+        }
  60
+    }
  61
+
  62
+    function createShortcutHandler(cmd) {
  63
+        return function() {
  64
+            return console[cmd].apply(console, arguments);
  65
+        }
  66
+    }
  67
+
  68
+    function createVariableHandler(prop) {
  69
+        return function() {
  70
+            return notifyFirebug(arguments, prop, "firebugExecuteCommand");
  71
+        }
  72
+    }
  73
+
55 74
     // Define command line methods
56 75
     for (var i=0; i<commands.length; i++)
57 76
     {
@@ -61,12 +80,6 @@ function createFirebugCommandLine(context, win)
61 80
         if (command in contentView)
62 81
             continue;
63 82
 
64  
-        function createCommandHandler(cmd) {
65  
-            return function() {
66  
-                return notifyFirebug(arguments, cmd, "firebugExecuteCommand");
67  
-            }
68  
-        }
69  
-
70 83
         commandLine[command] = createCommandHandler(command);
71 84
         commandLine.__exposedProps__[command] = "rw";
72 85
     }
@@ -82,12 +95,6 @@ function createFirebugCommandLine(context, win)
82 95
         if (command in contentView)
83 96
             continue;
84 97
 
85  
-        function createShortcutHandler(cmd) {
86  
-            return function() {
87  
-                return console[cmd].apply(console, arguments);
88  
-            }
89  
-        }
90  
-
91 98
         commandLine[command] = createShortcutHandler(command);
92 99
         commandLine.__exposedProps__[command] = "rw";
93 100
     }
@@ -99,16 +106,31 @@ function createFirebugCommandLine(context, win)
99 106
         if (prop in contentView)
100 107
             continue;
101 108
 
102  
-        function createVariableHandler(prop) {
103  
-            return function() {
104  
-                return notifyFirebug(arguments, prop, "firebugExecuteCommand");
105  
-            }
106  
-        }
107  
-
108 109
         commandLine.__defineGetter__(prop, createVariableHandler(prop));
109 110
         commandLine.__exposedProps__[prop] = "r";
110 111
     }
111 112
 
  113
+    // Define user registered commands.
  114
+    for (var name in userCommands)
  115
+    {
  116
+        // If the method is already defined, don't override it.
  117
+        if (name in contentView)
  118
+            continue;
  119
+
  120
+        var config = userCommands[name];
  121
+
  122
+        if (config.getter)
  123
+        {
  124
+            commandLine.__defineGetter__(name, createVariableHandler(name));
  125
+            commandLine.__exposedProps__[name] = "r";
  126
+        }
  127
+        else
  128
+        {
  129
+            commandLine[name] = createCommandHandler(name);
  130
+            commandLine.__exposedProps__[name] = "rw";
  131
+        }
  132
+    }
  133
+
112 134
     attachCommandLine();
113 135
 
114 136
     // xxxHonza: TODO make this private.
@@ -247,6 +269,43 @@ document.documentElement.appendChild(script);
247 269
 */
248 270
 
249 271
 // ********************************************************************************************* //
  272
+// User Commands
  273
+
  274
+function registerCommand(name, config)
  275
+{
  276
+    if (commands[name] || consoleShortcuts[name] || props[name] || userCommands[name])
  277
+    {
  278
+        if (FBTrace.DBG_ERRORS)
  279
+        {
  280
+            FBTrace.sysout("firebug.registerCommand; ERROR This command is already " +
  281
+                "registered: " + name);
  282
+        }
  283
+
  284
+        return false;
  285
+    }
  286
+
  287
+    userCommands[name] = config;
  288
+    return true;
  289
+}
  290
+
  291
+function unregisterCommand(name)
  292
+{
  293
+    if (!userCommands[name])
  294
+    {
  295
+        if (FBTrace.DBG_ERRORS)
  296
+        {
  297
+            FBTrace.sysout("firebug.unregisterCommand; ERROR This command is not " +
  298
+                "registered: " + name);
  299
+        }
  300
+
  301
+        return false;
  302
+    }
  303
+
  304
+    delete userCommands[name];
  305
+    return true;
  306
+}
  307
+
  308
+// ********************************************************************************************* //
250 309
 // Registration
251 310
 
252 311
 Firebug.CommandLineExposed =
@@ -255,6 +314,9 @@ Firebug.CommandLineExposed =
255 314
     commands: commands,
256 315
     consoleShortcuts: consoleShortcuts,
257 316
     properties: props,
  317
+    userCommands: userCommands,
  318
+    registerCommand: registerCommand,
  319
+    unregisterCommand: unregisterCommand,
258 320
 };
259 321
 
260 322
 return Firebug.CommandLineExposed;
37  extension/content/firebug/console/commandLineHelp.js
@@ -86,7 +86,12 @@ var HelpEntry = domplate(
86 86
     onClick: function(event)
87 87
     {
88 88
         var object = Firebug.getRepObject(event.target);
89  
-        Win.openNewTab("http://getfirebug.com/wiki/index.php/" + object.name);
  89
+
  90
+        var helpUrl = "http://getfirebug.com/wiki/index.php/" + object.name;
  91
+        if (object.helpUrl)
  92
+            helpUrl = object.helpUrl;
  93
+
  94
+        Win.openNewTab(helpUrl);
90 95
     },
91 96
 
92 97
     getName: function(object)
@@ -99,6 +104,9 @@ var HelpEntry = domplate(
99 104
 
100 105
     getDesc: function(object)
101 106
     {
  107
+        if (object.nol10n)
  108
+            return object.desc;
  109
+
102 110
         return Locale.$STR(object.desc);
103 111
     }
104 112
 });
@@ -147,6 +155,18 @@ var CommandLineHelp = domplate(
147 155
             })
148 156
         }
149 157
 
  158
+        for (var name in CommandLineExposed.userCommands)
  159
+        {
  160
+            var config = CommandLineExposed.userCommands[name];
  161
+            commands.push({
  162
+                name: name,
  163
+                desc: config.description,
  164
+                nol10n: true,
  165
+                helpUrl: config.helpUrl ? config.helpUrl: null,
  166
+                type: config.getter ? CMD_TYPE_PROPERTY : CMD_TYPE_COMMAND,
  167
+            })
  168
+        }
  169
+
150 170
         HelpEntry.tag.insertRows({commands: commands}, tBody);
151 171
 
152 172
         return row;
@@ -154,8 +174,23 @@ var CommandLineHelp = domplate(
154 174
 });
155 175
 
156 176
 // ********************************************************************************************* //
  177
+// Command Implementation
  178
+
  179
+function onExecuteCommand(context)
  180
+{
  181
+    CommandLineHelp.render(context);
  182
+    return Firebug.Console.getDefaultReturnValue(context.window);
  183
+}
  184
+
  185
+// ********************************************************************************************* //
157 186
 // Registration
158 187
 
  188
+Firebug.registerCommand("help", {
  189
+    getter: true,
  190
+    handler: onExecuteCommand.bind(this),
  191
+    description: Locale.$STR("console.cmd.help.help")
  192
+});
  193
+
159 194
 return CommandLineHelp;
160 195
 
161 196
 // ********************************************************************************************* //
46  extension/content/firebug/console/eventMonitor.js
@@ -5,13 +5,14 @@ define([
5 5
     "firebug/firebug",
6 6
     "firebug/lib/trace",
7 7
     "firebug/lib/events",
  8
+    "firebug/lib/locale",
8 9
 ],
9  
-function(Obj, Firebug, FBTrace, Events) {
  10
+function(Obj, Firebug, FBTrace, Events, Locale) {
10 11
 
11 12
 // ********************************************************************************************* //
12 13
 // EventMonitor Module
13 14
 
14  
-Firebug.EventMonitor = Obj.extend(Firebug.Module,
  15
+var EventMonitor = Obj.extend(Firebug.Module,
15 16
 {
16 17
     dispatchName: "eventMonitor",
17 18
 
@@ -157,14 +158,14 @@ Firebug.EventMonitor = Obj.extend(Firebug.Module,
157 158
 
158 159
     onMonitorEvent: function(event, context)
159 160
     {
160  
-        var obj = new Firebug.EventMonitor.EventLog(event);
  161
+        var obj = new EventMonitor.EventLog(event);
161 162
         Firebug.Console.log(obj, context);
162 163
     }
163 164
 });
164 165
 
165 166
 // ********************************************************************************************* //
166 167
 
167  
-Firebug.EventMonitor.EventLog = function(event)
  168
+EventMonitor.EventLog = function(event)
168 169
 {
169 170
     this.event = event;
170 171
 }
@@ -207,11 +208,42 @@ function getMonitoredEventTypes(types)
207 208
 }
208 209
 
209 210
 // ********************************************************************************************* //
210  
-// Registration & Export
  211
+// CommandLine Support
211 212
 
212  
-Firebug.registerModule(Firebug.EventMonitor);
  213
+function monitorEvents(context, args)
  214
+{
  215
+    var object = args[0];
  216
+    var types = args[1];
  217
+
  218
+    EventMonitor.monitorEvents(object, types, context);
  219
+    return Firebug.Console.getDefaultReturnValue(context.window);
  220
+}
  221
+
  222
+function unmonitorEvents(context, args)
  223
+{
  224
+    var object = args[0];
  225
+    var types = args[1];
  226
+
  227
+    EventMonitor.unmonitorEvents(object, types, context);
  228
+    return Firebug.Console.getDefaultReturnValue(context.window);
  229
+}
  230
+
  231
+// ********************************************************************************************* //
  232
+// Registration
  233
+
  234
+Firebug.registerModule(EventMonitor);
  235
+
  236
+Firebug.registerCommand("monitorEvents", {
  237
+    handler: monitorEvents.bind(this),
  238
+    description: Locale.$STR("console.cmd.help.monitorEvents")
  239
+})
  240
+
  241
+Firebug.registerCommand("unmonitorEvents", {
  242
+    handler: unmonitorEvents.bind(this),
  243
+    description: Locale.$STR("console.cmd.help.unmonitorEvents")
  244
+})
213 245
 
214  
-return Firebug.EventMonitor;
  246
+return EventMonitor;
215 247
 
216 248
 // ********************************************************************************************* //
217 249
 });
26  extension/content/firebug/console/profiler.js
@@ -513,11 +513,37 @@ function ProfileCall(script, context, callCount, totalTime, totalOwnTime, minTim
513 513
 }
514 514
 
515 515
 // ********************************************************************************************* //
  516
+// CommandLine Support
  517
+
  518
+function profile(context, args)
  519
+{
  520
+    var title = args[0];
  521
+    Firebug.Profiler.startProfiling(context, title);
  522
+    return Firebug.Console.getDefaultReturnValue(context.window);
  523
+};
  524
+
  525
+function profileEnd(context)
  526
+{
  527
+    Firebug.Profiler.stopProfiling(context);
  528
+    return Firebug.Console.getDefaultReturnValue(context.window);
  529
+};
  530
+
  531
+// ********************************************************************************************* //
516 532
 // Registration
517 533
 
518 534
 Firebug.registerModule(Firebug.Profiler);
519 535
 Firebug.registerRep(Firebug.Profiler.ProfileCall);
520 536
 
  537
+Firebug.registerCommand("profile", {
  538
+    handler: profile.bind(this),
  539
+    description: Locale.$STR("console.cmd.help.profile")
  540
+})
  541
+
  542
+Firebug.registerCommand("profileEnd", {
  543
+    handler: profileEnd.bind(this),
  544
+    description: Locale.$STR("console.cmd.help.profileEnd")
  545
+})
  546
+
521 547
 return Firebug.Profiler;
522 548
 
523 549
 // ********************************************************************************************* //
14  extension/content/firebug/firebug.js
@@ -22,9 +22,10 @@ define([
22 22
     "firebug/lib/dom",
23 23
     "firebug/lib/http",
24 24
     "firebug/trace/traceListener",
  25
+    "firebug/console/commandLineExposed",
25 26
 ],
26 27
 function(FBL, Obj, Firefox, ChromeFactory, Domplate, Options, Locale, Events,
27  
-    Wrapper, Url, Css, Win, Str, Arr, Dom, Http, TraceListener) {
  28
+    Wrapper, Url, Css, Win, Str, Arr, Dom, Http, TraceListener, CommandLineExposed) {
28 29
 
29 30
 // ********************************************************************************************* //
30 31
 // Constants
@@ -89,6 +90,7 @@ window.Firebug =
89 90
     earlyRegPanelTypes: earlyRegPanelTypes,
90 91
     uiListeners: [],
91 92
     reps: reps,
  93
+    commands: {},
92 94
 
93 95
     stringCropLength: 50,
94 96
 
@@ -652,6 +654,16 @@ window.Firebug =
652 654
             Firebug.TraceModule.removeListener(listener);
653 655
     },
654 656
 
  657
+    registerCommand: function(name, config)
  658
+    {
  659
+        return CommandLineExposed.registerCommand(name, config);
  660
+    },
  661
+
  662
+    unregistereCommand: function(name)
  663
+    {
  664
+        return CommandLineExposed.unregisterCommand(name);
  665
+    },
  666
+
655 667
     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
656 668
     // Options
657 669
 

1 note on commit 306663a

Simon Lindholm

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

Jan Odvarko

Nice catch Simon!
Fixed at 8bbd96a
Thanks
Honza

Simon Lindholm

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.

Jan Odvarko

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

Jan Odvarko

Thanks for your comments Simon.
Honza

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