Sam's infrequently-updated cabinet of curiosities
Monday, 18 June 2007

Userscript: Reddit unread comments helper

Edit - 2008-05-27: Updated to work with the (horrid) Reddit redesign. I can't be bothered updating the screenshots too.

Or: (ab)using Greasemonkey and Google Gears to add features that would be handled better server-side.

The script tracks comments you've seen at Reddit, then exposes the data in several small ways that each make your life a little easier. Features:

  • On the main Reddit list pages, replace the "n comments" links with "x unread comments (n total)".


    before the userscript is applied


    after the userscript is applied
  • On clicking through to a page where you've already read some of the comments, jump to the first unread comment.

  • Highlight unread comments with a bright but non-distracting left margin.

Download or install it.


My ulterior motive was testing the Gears DB with Greasemonkey. More than once I've wished it had a binding to SQLite, and with Gears it does: it just got a thousand times more useful. It'd be nicer yet if I could save to an arbitrary cross-domain database, but this is still a tremendous step up.

This script uses a bit of a hack and writes itself directly into the window, rather than just manipulating the DOM from the usual plexiglass sandbox. Strictly speaking it's not necessary, and only possible at all because I have no use for the GM_* API functions, but a userscript with Gears does require at least some meddling of this kind.

Gears prompts the user to allow it to run on a specific domain, but the dialog doesn't appear if it's initialized from within Greasemonkey; it has to be done from the unsafe window. Once it's set up -- once the local database has been created and what have you -- Greasemonkey is fine, but that first step is critical.

Still, that's basically the only hurdle, and it's trivial to surmount. Gears is a dream: the API seems a little sparsely featured, but it's so easy to build a platform around that the lack of convenience methods doesn't matter. I wrote a very simple DB wrapper of my own, and others are already building full ORMs. There's no sight of JavaScript on Jacks just yet, but it can't be far off.