You're debugging your web application and are suddenly faced with all so familiar error page, presenting you with an error message and, naturally, a stack trace. You wade through the call stack, finding respective files, entering line numbers - much more hassle than it needs to be.
Wouldn't it be nicer to have stack frame references as links which you could click to open in editor, positioned on the right line, looking like this:
Step 1. Install Greasemonkey
To install Greasomonkey, go to its page in Mozilla add-on repository, click "Add to Firefox" and follow the instructions: http://addons.mozilla.org/firefox/addon/748/
Step 2. Add Greasemonkey script to convert stack trace to links
Once Greasemonkey is installed, we need to add our custom script that does the actual transformation. When it is run, text like /path/to/file.html:123 will be turned into HTML: <a href="edit:///path/to/file.html:123">/path/to/file.html:123</a> The script itself can be downloaded here.
To install the script, navigate to your download folder in Firefox and click the downloaded script. Greasemonkey install dialog will open. I haven't found a way to install local Greasemonkey scripts other than via clicking them, even though there used to be a menu item for that in earlier versions of Greasemonkey. Also, script file name MUST end with .user.js, or Greasemonkey will not recognise it.
The code itself is simple enough:
// ==UserScript== // @name Mason error source URLificator // @namespace http://lvalue.blogspot.com // @description Converts source code references in HTML::Mason error page into edit:// protocol links. // @include * // ==/UserScript== document.body.innerHTML = document.body.innerHTML.replace(/^ +([^ ]+:\d+)<br>$/mg, '<a href="edit://$1">$1</a><br>');
You will probably want to edit the @include property to limit the script to particular URL pattern only, so it doesn't mess up or slow down things outside of your debugging sessions. Here at work, I have it set to: \*.lovefilm.\*, so it includes internal and external websites of the company.
Step 3. Create wrapper script to call your editor
When executing external program as custom protocol handler, Firefox passes it whole URL as single command-line parameter, with protocol prefix included. Since our editor expects correct file path and, separately, a line number, we will perform another simple transformation with the following Perl script:
#!/usr/bin/perl $ARGV =~ m!^edit://(.+):(\d+)$!s or die "Invalid URL: $ARGV"; exec '/usr/bin/emacsclient', "+$2", $1;
You can download this script here.
Step 4. Install wrapper script as edit:// protocol handler in Firefox
WARNING: these instructions apply strictly to Firefox 3.6+. Search the web for help on earlier versions which require a slightly different procedure.
For Firefox 3.6+, Enter "about:config" in your URL bar, press enter, then click the confirmation button. You'll see list a long of settings. Right-click anywhere on it and choose "New", then "Boolean". Enter name: "network.protocol-handler.expose.edit", value: "false", and that's it for editing the settings.
Now the only thing left to do is telling Firefox know what program to run as the editor. You'll do this in configuration dialog that pops up when you first click an edit:// link. Remember, you want Firefox to run our wrapper script, and not your editor directly. If you accidentially chose a wrong program, you can fix it by editing ~/.mozilla/firefox/<profile dir>/mimeTypes.rdf. To make configuration dialog appear right now, click the following dummy edit link Happy debugging of your Mason errors!