Greasemonkey script for DevMaster

A8433b04cb41dd57113740b779f61acb
3
Reedbeta 167 Sep 14, 2013 at 03:18 devmaster

I created this Greasemonkey script to make the experience of reading unread comments on DevMaster a bit better. When you open a thread, this will automatically scroll you to the first unread comment (they are marked by a yellow bar). You can also navigate between unread comments by pressing the ‘n’ and ‘p’ keys (next and previous). Of course, you can change it to be different keys if you like.

I thought it was worth sharing. To use it you’ll need the Greasemonkey add-in for your browser. You can just create a new user script and paste this in:

// ==UserScript==
// @name        DevMaster Unread Posts Navigation
// @namespace   http://reedbeta.com
// @include     http://devmaster.net/*
// @version     1
// @grant       none
// ==/UserScript==

var allComments = document.getElementsByClassName('comment');
var unreadComments = document.getElementsByClassName('unread');

if (unreadComments.length > 0)
{
    var curComment = 0;

    // If some comments are already read, scroll to the first unread one
    // (don't scroll if all but one comments are unread, as this indicates a new post;
    // the post itself is the first comment and is never unread)
    if (unreadComments.length + 1 < allComments.length)
        unreadComments[0].scrollIntoView();

    // Add keypress handler
    document.addEventListener('keypress',
        function(e)
        {
            var node = e.target;
            var name = node.nodeName.toLowerCase();
            if (name == "textarea" || (name == "input" && node.type == "text") ||
                e.ctrlKey || e.altKey || e.metaKey)
            {
                return; // don't intercept textarea/text input field input, or control keys
            }
            var key = e.charCode || e.keyCode;
            if (key == 110)         // N: next unread comment
            {
                if (curComment < unreadComments.length - 1)
                    ++curComment;
                unreadComments[curComment].scrollIntoView();
            }
            else if (key == 112)    // P: previous unread comment
            {
                if (curComment > 0)
                    --curComment;
                unreadComments[curComment].scrollIntoView();
            }
            else
            {
                return; // don't intercept other keys
            }
            e.preventDefault();
            e.stopPropagation();
        }
    );
}

1 Reply

Please log in or register to post a reply.

Fdbdc4176840d77fe6a8deca457595ab
0
dk 158 Sep 15, 2013 at 03:37

Nice! That’s quite useful. Thanks for sharing!