GCU Dancer on the Midway
Paul Wright's blog
Monkeying around 
2nd Jan 2006, 10:14 pm
giles
I've finally got around to writing the Greasemonkey script which I've long been threatening.

What it does

The script remembers which comments you've seen on LJ (or Dreamwidth) and helps you navigate to new comments. That's right, I'm finally dragging LiveJournal kicking and screaming into the 1980s.

If you're on an entry page, pressing "n" skips you to the next new comment, and "p" skips to the previous one. If the style has an "Expand" link, moving to an unexpanded comment with these keys will also expand the thread. If the style has a permanent link or a reply link for each comment in that comment's header or footer, the script inserts another link next to it, labelled "NEW". That link shows you that the comment is new at a glance. Clicking the "NEW" link selects the comment so that pressing "n" will go to the next comment from there. On some styles, the currently selected comment will be outlined with a dotted line.

On a journal or friends page, the script will also add the number of new comments to the link text, so that, say, "15 comments" becomes "15 comments (10 new)", and enable the "n" and "p" keys to move between entries which have new comments, and the "Enter" key to view the selected entry. This only works if you're looking at a journal which adds "nc=N" to entry links to say there are N comments on an entry (LJ can do this as a trick to confuse your browser's history function into thinking you've not visited that entry whenever there are new comments). If you want to turn this on for your journal then ensure you're logged in, visit this page, check the box which says "Add &nc=xx to comment URLs" and hit the "Save" button.

How it works

You don't need to understand this section to use the script. If you don't care about programming, skip to the next part.

LJ makes it a total pig to do this sort of thing: there's so little uniformity in journal styles that getting a script like this to work for all of them is impossible. It's fair enough that LJ allows people to customise their journal's appearance, but there aren't even standardised CSS class names for stuff. Not that I'm bitter. So, what the script does is look for anchor tags of the form <a name="tNNNN"> or elements with an id attribute of ljcmtNNNN or tNNNN. NNNN is the comment number, which seems to be unique for each comment on a given user's journal. It then looks for the permanent link to that comment, which is usually to be found in the header of the comment (or footer, in my current style), and adds a "New" link after that. So, new comments are marked with a link to the next new comment.

The upshot of all this is that if you're reading a journal with a style which doesn't use either anchor tags or elements with the given id for all comments, the script won't work correctly. If the style doesn't provide each comment with a permanent link in the comment's header, the comment won't be marked with a "New" link. Such is life. Please don't ask me for special case changes to make it work with LJ's many horribly customised journals. Pick a sensible style of your own and learn to use "style=mine" instead. There's even another Greasemonkey userscript which will help. On the other hand, if there's a large class of the standard styles for which it doesn't work, tell me and I'll have a look at it.

Using it

If you want to use it, you will need:

  • Firefox, the web browser, version 1.5 or later.
  • Greasemonkey, the extension which lets people write little bits of Javascript to run on certain pages.
  • LJ New Comments, which is what I've imaginatively entitled my script. If the userscripts site is down again, you can find a copy on my site.
  • Your flask of weak lemon drink.

After you've installed all of the above, visit an entry on LJ and marvel at the "NEW" links on all the new comments (which will be all of them at this point, as the script wasn't around previously to remember which ones you'd seen before). See above for operating instructions.

Privacy

Note that the script stores a Firefox preference key for each journal entry you visit, listing the IDs of the comments it finds there. The script doesn't let the database grow without limit: when the script has seen 500 entries, it starts to drop the history for the entries you've not visited recently.

Clearing the browser's history doesn't affect the script's list of visited entries. Thus your visits to polybdsmfurries will be recorded for posterity, even if you clear the browser's history. You can wipe the entire history by using the "Manage User Scripts" entry on the Tools menu to delete the script and its associated preferences (you can re-install it afterwards, but you must clear out the preferences for it to delete the history).

The script does not record the contents of any entry or comment. The script does not transmit any information to LJ or any other website, it merely acts on what it sees when you request journal entries.

Your questions

I've given this entry as the homepage for the script on Userscripts.org. That means this entry is intended to serve as a repository for questions about the script, so if you've got a question, comment here. I prefer this to commenting on my other entries or to emailing me, unless you already know me. Ta.

To keep up to date with new releases of my greasemonkey scripts, track the tag "greasemonkey" on my journal. This link should enable you to subscribe to that tag and get notified when I post a new entry about greasemonkey scripts.

Revision history

2006-01-02, version 0.1: First version.

2006-01-03, version 0.2: Added the "p" key. Used javascript to move between comments so doing so does not pollute the browser's history. Coped with the id=ljcmtNNNN way of marking comments. Made "n" and "p" keys work even in the absence of permalinks on each comment.

2006-01-04, version 0.3: Apparently you can have id=tNNNN, too.

2006-01-04, version 0.4: Broke 0.3, fixed it again. I hope.

2006-01-19, version 0.5: Updated to cope with LJ's new URL formats. Changed how comments are stored internally so that the database does not grow without limit: the script now remembers comments for the last 500 entries you visited, and forgets the entries you've visited least. Also added "New" marker based on reply link as well as thread link, for styles which don't have a thread link for every comment.

2006-01-19, version 0.6: Convert dashes I find in URLs to underscores internally, to preserve access to history from older versions of the script before LJ's URL change.

2006-02-09, version 0.7: Work around the fact that Firefox leaks memory like a sieve. Never display negative number of new comments. Change licence to MIT as GPL is overkill for this script.

2006-02-09, version 0.8: There was a bug in the workaround code I got off the Greasemonkey mailing list. Fixed that.

2006-06-04, version 0.9: Enabled the "n" and "p" keys on the friends/journal view. Added the box around the current comment.

2007-02-20, version 1.0, baby: Try harder to draw a box around the current new comment. Applied legolas's fix for pressing CTRL at same time as the N or P keys (see comments).

2008-03-31, version 1.1: Make it work faster on entries with lots of comments. Altered behaviour of "NEW" link so it now selects the comment you're clicking on, as that makes more sense.

2008-09-24, version 1.2: Support Russian keyboards thanks to mumische, make threads expand.

2009-01-27, version 1.3: Support for independentminds journals.

2009-05-04, version 1.4: Support for Dreamwidth.

2009-09-22, version 1.5: Amend support for Dreamwidth.

2010-08-09, version 1.6: Made syndicated journals work.

Comments 
8th Jan 2006, 03:42 pm (UTC)
This is an excellent script, thank you!

I do have one problem with it though. I can't navigate to the "next" and "previous" new comments when I use "begin finding as you begin typing" option in Firefox, because it brings up the find dialog instead.

How can I modify the script to use for example the Alt-n and Alt-p combinations instead?
8th Jan 2006, 08:27 pm (UTC)
Funny: my Firefox doesn't do the search thing unless I press "/" and then start typing. Maybe it's a configuration thing. Anyway...

If you're happy changing a bit of Javascript, the part you're interested in is the keypress_handler function near the bottom of the script. The event has event.altKey set true if the Alt key was pressed along with the key, event.ctrlKey set if the Ctrl key was pressed, and so on. So you'd want to change the test for each key to something like: if (event.which == 112 && event.altKey), I think, that is, you'd say "press p and alt key at the same time".

Hope this helps.
9th Jan 2006, 01:25 am (UTC)
I like this script a lot. I can't figure out what it's supposed to do on friends pages, though, as there's nothing new showing up on mine.
10th Jan 2006, 12:19 am (UTC)
You need to turn on the option which says "Add Add &nc=xx to comment URLs" on your Edit Personal Information page. It then modifies the links to tell you how many new comments are on those entries.
(Deleted comment)
(Deleted comment)
22nd Jan 2006, 11:51 pm (UTC)
This is a really handy script; thanks. Are you interested in new-feature suggestions? You could make "n" on a journal page find the first journal entry on the page with unread comments, load it and point the browser at either the top of the entry (if all comments are unread) or the first unread comment.
22nd Jan 2006, 11:56 pm (UTC)
Oh, and in the 'bug report' category, after hitting 'back' to return from an entry page to my friends page the link was updated to say "(13 comments (-1 new)" -- note the negative number. Unfortunately it doesn't seem to be reliably reproducible :-(
24th Jan 2006, 01:09 am (UTC)
I've seen that on entries with deleted comments, as the number of comments goes down but the markers for the comments are still there (and anyway, there's no way to know the comment was deleted before opening the entry). I think the best thing for the script to do is not modify the link text when it comes up with a negative number.
(no subject) - Anonymous - Expand
12th Feb 2006, 01:43 pm (UTC)
Another bug, although I'm not sure what provokes this: sometimes my friends page ends up with links like this: "8 comments (1 new) (1 new)"...

(I'm still using 0.6.)
21st Feb 2006, 12:41 am (UTC)
Hmmm... I'd hope that Greasemonkey did its own idempotency checks to prevent that sort of thing. I've never seen that one with GM 0.6.4 on FF 1.5. Any more ideas on how to reproduce it?
4th Jun 2006, 07:09 pm (UTC)
Hello! I've been using your script before, but I switched OS and lost it. As Userscripts.org is down right now, I can't access it. Have you made it available elsewhere?
5th Jun 2006, 07:14 pm (UTC)
Hi. Userscripts is a bit unreliable at the moment, so I've placed a copy of the script on my site (and also uploaded the new version I've had kicking around on my hard drive for a while).
9th Oct 2006, 10:45 pm (UTC)
I didn't realize how much I relied on this script until I changed my style to use .permalink_url?format=light instead of $.comments.read_url (so that the killfile script would work properly) and found that losing ?nc=xx also lost me the new comment notes. Fortunately, using $.comments.read_url&format=light appears to work. Thanks for a great script!
15th Oct 2006, 10:28 pm (UTC)
Thanks for the comment: I'm glad to know that people find it useful.

I keep thinking I'll generalise its knowledge of where comments are in various styles to extend the killfiling and thread unfolding functions that other people have implemented for the default comment style, but I've been a bit busy lately. Maybe now I've got some more free time I'll give it a go.
8th Nov 2006, 06:30 pm (UTC)
How does this interact with the LJ thread unfolder?
8th Nov 2006, 07:36 pm (UTC)
It sort-of works, but they don't know about each other, so they don't interoperate properly. What happens on my machine is that on pages with the default comment style, if I unfold a thread, LJ New Comments will fail for the comments on that thread, but if I keep pressing "n", I will eventually reach the bottom of the thread.

This makes sense: LJ Thread Unfolder destroys a piece of the DOM and replaces it with the comments it loaded, so Firefox refuses to scrollIntoView the objects which LJ New Comments has cached (the folded up comments, which are just the header without the text). Making LJ New Comments recache its idea of where each comment is (without updating its idea of newness) whenever LJ Thread Unfolder unfolds a thread would make things work, but I don't know of a good way for two GM scripts to communicate like that.

My master plan is to use LJ New Comment's ability to recognise a comment in a variety of styles to make a better thread unfolder (the current one only really works in the default style, useful though that is) and possibly a kill-file (I want my Usenet). Don't hold your breath, though.

Edited at 2008-03-30 11:54 pm (UTC)
11th Nov 2006, 10:25 am (UTC)
Oooh, thanks. This will be useful.
1st Feb 2007, 12:47 pm (UTC)
Thanks *_* Just found your script on Userscripts.org and I love it! Really useful! Thanks a lot!
19th Feb 2007, 10:53 pm (UTC)
Bugreport: installing this script and then opening a new window with ctrl+n makes my friends page jump to whatever 'n' would jump to. I'm trying to fix this, I'll let you know if I do.
19th Feb 2007, 11:16 pm (UTC)
Ok, I fixed it like this: I added these lines to the keypress handler:

    //return if any modifier is active, so we don't handle e.g. ctrl+n
    if(event.ctrlKey || event.altKey  || event.ctrlKey  || event.metaKey  || event.shiftKey)
	return;

Just under the already existing
    if (event.which != 110 && event.which != 112)
        return;
6th Sep 2007, 03:55 am (UTC)
Of all the userscripts out there, yours is probably the most useful and most reliable userscript out there. There's a few system styles out there that it doesn't work on. But no biggie.
6th Sep 2007, 09:00 am (UTC)
Thanks! I'd be interested to know which system styles it doesn't work on, just in case the fix is easy.
3rd Apr 2008, 08:38 pm (UTC)
FYI, it is working for me in opera 9.26 with:

- the 'return' statements replaced by exit
(I don't know enough js to know if this is actually the same thing, but WFM)
- the 'emulate greasemonkey script' linked here http://www.howtocreate.co.uk/operaStuff/userJavaScript.html

4th Apr 2008, 12:12 am (UTC)
er, I spoke slightly too soon. I *also* had to put an if block around
all the entry-specific stuff.

if (userName) {

}

*then* I find it is storing too long a comment list: it is storing a
string of comment ids, plus "extend, override, init, destroy" so the
list is four items too long. I thought this was a style issue but I
poked some more and it turns out to be something to do with the js
objects because they show up even with

commentHash = new Object();
for (commentNumber in commentHash) {

so I blame opera, and I fix that loop to ignore those items....

*then* I realise that (??) because the gm emulating thing works by
storing document cookies, it's not working properly because of post and
friends page being in different domains. So I have to edit the gm
emulator to take the domain as an argument to its value storing thing,
and edit your script to send that.

*now*, now it works.

18th May 2008, 06:37 pm (UTC)
After installing the 1.1 version, the n/p keys have stopped working in the posts to go to the next new comment. They work fine on my friends page.
20th May 2008, 12:16 am (UTC)
Works for me. Can you point to a page where it's not working or is it everywhere?
14th Aug 2008, 05:40 pm (UTC)
Excellent script, thank you! But I'm often using russian keyboard layout, so I'v made some changes. Do you planning to publish international versions?
14th Aug 2008, 11:34 pm (UTC)
I could do. Looks like Javascript can detect the locale with navigator.language. If the only differences are which keys do what, it should be fairly easy to fold your changes in, with a Greasemonkey menu option to select between Russian and English, defaulting to Russian if that's the navigator.language. Can I see your code?
30th Jan 2009, 11:18 pm (UTC)
Hi! Awesome script, but I'm having some troubles with it. (I'm using Firefox v.3.05 on Windows XP.) At first when I installed the script, it worked like it was supposed to. Now, for some reason, all comments are labelled NEW, no matter how many times I view the page.

I haven't really changed anything/installed anything new, but my antivirus (AVG, free edition) did clean out some tracking cookies, could this have anything to do with it?

31st Jan 2009, 11:50 pm (UTC)
The script doesn't store the comments you've seen as cookies, it stores them as preference settings in Firefox (because that's how Greasemonkey does it). Firefox only writes preference settings to disk when the browser closes down properly, so if Firefox crashes it'll lose its memory of the comments you've seen since you started Firefox.

It sounds like that's not what you're seeing here, though. If you keep hitting reload on a page, do you get NEW comments all the time, or is it just when you go back to a page after restarting Firefox?
3rd May 2009, 06:12 pm (UTC)
I've been using your script for ages, and it's a great help. Thank you very much.

I was wondering whether you were planning on making a version for Dreamwidth? It's one of those clever things you don't realise how much you miss, until you don't have access to it any more.
4th May 2009, 12:56 am (UTC)
Glad you like the script.

I do intend to make it compatible with Dreamwidth (and with other popular LJ clones) but I've been waiting for DW's official launch as they've been changing styles and suchlike as they've been developing it. It looks like the way the script detects comments will work just as it did on LJ, so I hope to have something fairly soon.
16th Nov 2009, 05:35 pm (UTC)
It doesn't seem to be working on dreamwidth for me -- I've tried looking at several different journals.

I removed and reinstalled the script, just in case I had an old one.
16th Nov 2009, 06:50 pm (UTC)
Works for me on the one person whose journal I read at DW :-) Can you point me at a journal where it doesn't work?

Edited at 2009-11-16 06:50 pm (UTC)
5th Aug 2010, 03:19 am (UTC)
Hello, thanks for the great script! It doesn't seem to be working on posts in syndicated feeds, though - either the "X new" on your friends page or on individual comments in a syndicated post. I've tried disabling and reenabling the script, un/reinstalling it, and checked the included and excluded pages (both default). I'm pretty sure it was working a couple weeks ago, not sure why it stopped.
6th Aug 2010, 04:00 pm (UTC)
Looking at it, I don't think it should ever have worked for syndicated accounts, because it doesn't recognise the URL of the page as being a LiveJournal entry. I'll try to fix it this weekend, hopefully.
16th Sep 2011, 11:48 pm (UTC)
Thanks for the script!

Question/comment: as far as you know, is this compatible with Insanejournal? Figured I'd check in case there's something messed up on my end and it's supposed to actually work. I just use the default entry views (not customized) and the comment URL structure is ljcmtNNNN, but it doesn't seem to be working for me.
26th Sep 2011, 01:15 pm (UTC)
It partially works on IJ if you just tell it to run on *.insanejournal.com using the Greasemonkey configuration. Under Windows, that's Tools, GreaseMonkey, Manage User Scripts, Options (on the script), Included Pages, Add, then add http://*.insanejournal.com/*

What doesn't work is comments on communities, because IJ has changed the URL for that. I'll have a look at fixing that when I get a moment.
17th Nov 2011, 10:18 pm (UTC)
Hi there. I'm not sure if you're still maintaining this script, but lately when I've been clicking on the Expand link on an entry that has lots and lots of comments, the page view sometimes jumps to a higher point on the page and then I have to scroll down again to find the comment I expanded. It's not happening in IE, only Firefox, and when I contacted LJ Support about it, they recommended disabling all my add-ons and then re-enabling them one at a time to find out if one of them is causing the problem. It looks like your script is the culprit. Do you have any suggestions for a workaround? I love your script and I don't want to disable it.
18th Nov 2011, 10:05 am (UTC)
I'm still looking after the script. Can you point me at a page where this happens?
21st Nov 2011, 09:26 pm (UTC)
Hi, I have this on my main computer and it works fine. However, I've been trying to get it to work on my laptop for a few days now, but whenever I click to install it, I get the error 'Script could not be installed TypeError : lines is null.' Any idea what the problem is?
22nd Nov 2011, 12:26 am (UTC)
I Googled for the error message and people seem to think it's a problem with Firefox and Greasemonkey, as it's been seen with various different Greasemonkey scripts. Suggestions include making sure you've got the latest Firefox and Greasemonkey, and uninstalling and reinstalling Greasemonkey.
29th May 2012, 03:40 am (UTC)
Is there any way to change the color of the link to make it more noticeable? I looked at the code, but I know nothing about this stuff, so it was all gibberish to me.
This page was loaded Dec 29th 2014, 4:18 am GMT.