Monday, August 17, 2009

Greasemonkey hackery

Kind of foreshadowing a possible Firefox Configuration post in the future (it's changed a fair amount since the last one). I was changing up my configuration a few days ago, and noticed that one of the extensions I installed lacks a feature that the one it replaced had.

I had been using Pimpoflage to put the menu bar and status bar on autohide, to get them out of the way unless I actually needed them for something. It was nice, but it had issues. Its "show menubar+statusbar on command" keyboard shortcut hardly ever worked, and it conflicted with Tab Mix Plus' feature that forces new windows to become new tabs in the current window instead. I made some settings workarounds so I could cope with it while I looked for an extension to replace it with, and then I finally found DisableMenu.

DisableMenu does the job without conflicting with Tab Mix Plus. It does it a little differently from Pimpoflage, when you mouse where the status bar should be the menu bar pops up too. Its keyboard shortcut actually works though. The big flaw, that was the reason for the Greasemonkey hackery, is that when I mouse over a link it doesn't pop up the status bar telling me where the link will take me.

So I wrote a Greasemonkey script to do that. The script itself is actually just a stub that injects a script tag into the head of the document loading a script off of my website. That script does everything. This was necessary because I needed to have functions be called on a couple of different events, and if they're defined on the Greasemonkey side of things then Greasemonkey's own security protections won't let the page call them.

The visual styling takes after SRWare Iron's (and by extension, Google Chrome's) behavior for showing link targets. It just pops up in the lower left corner, covering up the page. I even copied the rounded corner on the top right corner of it. It takes its colors from your OS' visual theme, so it should look somewhat decent even on a dark theme or whatever, as long as your colors contrast enough.

It doesn't work everywhere, I've encountered a couple of sites that it either does absolutely nothing on, or the injection of the script (and the CSS that gets injected later) messes up the page somehow. Blogger's post creator/editor is basically rendered inoperable by the injection of the script, and I haven't seen it trigger at all on xkcd. (Edit: oh lol I wasn't allowing xkcd to run scripts, it works fine there)

In addition, when viewing my blog, another oddity arose. The navbar that blogger puts at the top of the page is actually in an iframe. With its own separate document object model. Meaning the link target showed up at the bottom of the iframe instead of the bottom of the browser window. Maybe there's a proper fix for this (checking to see if the document has a parent? does that work?), but I doubt there's a concrete fix that will work across all sites on the internet (i.e. with vanilla frames). It doesn't even matter anyway, it's just a workaround until DisableMenu's author gets around to implementing the feature.

All of the bugs and oddities can easily be fixed by specifying exclusions in Greasemonkey, i.e. pages the script shouldn't be included on. I had to exclude it from secure (https) pages entirely since the script is being loaded over a regular http connection. Mixing secure and insecure content is bad.

The only thing left to implement is a short timer so it doesn't pop up and flicker while trying to display link targets as I move my mouse over a bunch of links. A 500 millisecond timer should do the trick, if my mouse has been over the link for half a second I'm probably interested in where it leads.

I could make the script available for others to install, but I won't. The requirements justifying its use are fairly specific to my case, so it's unlikely that anyone reading this will need it. If for some reason you do actually need it, let me know and I'll send you the stub script. The CSS uses a Mozilla-specific selector (for the rounded corner) since Firefox 3 doesn't have CSS3, so it might not work right in Fx3.5 or in Opera. It'll be up to you to add additional exclusions according to your own browsing habits though. If you just want to see the code, well, be warned, it's probably going to be hard to read. When I write something up quickly I tend to omit whitespace, and whitespace makes code a lot more readable. Maybe I'll fix it, maybe I won't.

Speaking of Firefox 3.5, I'm waiting for one extension to be updated (I might try just hacking the maxVersion) before I update. Everything got updated a lot faster this time around.

No comments:

Post a Comment

I moderate comments because when Blogger originally implemented a spam filter it wouldn't work without comment moderation enabled. So if your comment doesn't show up right away, that would be why.