Gmane
Picon Favicon
From: Robert Ginda <rginda <at> netscape.com>
Subject: overriding filenames in the loaded scripts view
Newsgroups: gmane.comp.mozilla.devel.jsdebugger
Date: 2003-04-09 22:23:01 GMT (5 years, 5 weeks, 1 day, 9 hours and 28 minutes ago)
This question came up in the #venkman IRC channel today.  I figured I'd 
post it here in a blatant attempt to get others thinking about extending 
Venkman.  It also gives me a chance to remind everyone that there is a 
#venkman IRC channel on irc.mozilla.org.  If you have ChatZilla 
installed, you can just click on irc://moznet/venkman to join.  Don't be 
surprised if there are only a few other people there :)  I'm usually 
listening and available for specific Venkman questions.  Read 
http://www.hacksrus.com/~ginda/venkman/faq/venkman-faq.html#irc for more 
information.

Anyway, today's question was...

   Is there any way to over ride what is displayed as the name in the
   'Loaded Scripts' area?  I have a situation where all my scripts in
   the 'Loaded Scripts' have the same name. They're all called Broker.
   The reason this is happening to me, is I have a template system
   where all my code is passed through this Broker java servlet.
   My reference in the html page looks like the following:
       <script type="text/javascript"
         src="/tsc/servlet/Broker?type=templates&file=customer.js">
       </script>
   I'd like to be able to over ride the name being displayed in the
   'Loaded Scripts' area so that it would display 'customer.js'
   Is there any (easy :P) way to perhaps override the display name
   with some kind of regex based on the URI to the include?

The answer is a script that looks like this...

   var hooks = new Object();

   hooks["hook-script-instance-sealed"] =
   function my_hook(e)
   {
       // try to pick the filename out of the url
       ary = e.scriptInstance.url.match(/Broker\?.*file=(\w*\.js)/);
       if (ary)
       {
            // change the text of the label for our tree item
            e.scriptInstance.scriptInstanceRecord.displayName = ary[1];
            // cause the tree item to repaint
            e.scriptInstance.scriptInstanceRecord.invalidate();
       }
   }

   console.commandManager.addHooks(hooks, "my-plugin-id");

   true;

This script can be loaded into Venkman with ``/loadd 
file:///path/to/file.js'', or set to load at startup with ``/pref 
initialScripts file:///path/to/file.js''.

The idea here is to "hook" a command that is invoked by Venkman itself 
when a new instance of a file is completely loaded.  This particular 
command is called "hook-script-instance-sealed".  The hook function will 
be called after the command is executed, and after any hooks that may 
have been registered first.  In our case, the Loaded Scripts view itself 
has also hooked the "hook-script-instance-sealed" command, so that it 
knows when to add a new record to the tree.  This hook is located here: 
http://lxr.mozilla.org/mozilla/source/extensions/venkman/resources/content/venkman-views.js#1077

Our hook will run after the Loaded Scripts hook because "plug-ins" are 
loaded last.  The first thing we do is check to see if the "script 
instance" that was just "sealed" is from a URL that matches the pattern 
we are interested in.  If so, we're going to take action, otherwise we 
do nothing.

All commands in Venkman take exactly one parameter.  This parameter is 
called "e" by convention (the e is short for "event") and is an object 
containing any parameters the command requires.  As you may have already 
noticed, you can find help on any command via the interactive command 
reference.  From the Venkman Help menu, select Command Reference, and 
search for "hook-script-instance-sealed".  If you've got a few minutes 
to explore, try a search for all commands that have the word "hook" in 
them.  Bear in mind that you can hook *any* command, it's just that the 
commands that start with "hook-" exist solely to be hooked, the commands 
themselves do nothing at all (in this way, they more aptly be called 
"events".  Oops, too late :)

In the case of "hook-script-instance-sealed", the only parameter is 
scriptInstance.  For this example all you need to know is that a "script 
instance" is an object that represents a loaded Javascript file.  We 
check the URL of that file to see if it matches the pattern we are 
interested in.  If we get a match, we need to change the label of the 
tree item.  Venkman refers to the objects that represent tree items as 
"records", so any time you see a variable that ends in the word 
"Record", you'll know that's what it is.  In this example, we change the 
"displayName" property of the e.scriptInstance.scriptInstanceRecord 
object.  When that is done, we call the invalidate() method of the 
record to cause it to redraw, and that's it.

So, that's a fairly simple example of a useful Venkman plug-in.  Keep in 
mind that these plug-ins are loaded as part of Venkman itself, and so 
they can do anything Venkman can do.  Venkman is implemented in 
Javascript, in terms of its own plug-in api.  Because of this, it is 
possible to add UI, menu items, and new commands from plug-ins with a 
minimum amount of effort, and there are plenty of examples in Venkman 
itself.  Documentation on the specifics is scarce at this point, but if 
you have questions feel free to ask.  At some point I'll likely post the 
more interesting discussions from this group to the Venkman website for 
posterity.

Rob.

PS, here are some links to source files that may be interesting:

The ScriptManager (files with the same url), ScriptInstance (instance of 
a loaded file), and ScriptWrapper (functions within a file) objects are 
defined in:
http://lxr.mozilla.org/mozilla/source/extensions/venkman/resources/content/venkman-debugger.js#361

The "record" objects (objects that represent tree items) are defined in:
http://lxr.mozilla.org/mozilla/source/extensions/venkman/resources/content/venkman-records.js

There are numerous examples of hooks in this file, which happens to 
contains all of the Venkman views:
http://lxr.mozilla.org/mozilla/source/extensions/venkman/resources/content/venkman-views.js