<commit_before>// ==UserScript==
// @name          Hacker News: New Comments
// @namespace     http://mschade.me/userscripts
// @description   Make new comments on Hacker News more noticeable. Created by <NAME> (@michaelschade, http://mschade.me/)
// @match         http://news.ycombinator.com/item?id=*
// @include       http://news.ycombinator.com/item?id=*
// ==/UserScript==

// URL to jQuery Library
var jqURL = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";

function main() {
    /* Options */
    var NEW_COMMENT_HIGHLIGHT = 'yellow';

    /* Add ability to compute difference between two arrays.

       Snippet via http://stackoverflow.com/q/4026828
     */
    Array.prototype.diff = function(a) {
        return this.filter(function(i) {return !(a.indexOf(i) > -1);});
    };

    /* Strips the prefix of query string to retrieve the intended id. */
    function getId(s) {
        return s.replace("item", "").replace("?id=", "");
    }

    /* Processes the comments on page to determine which, if any, are new,
       marking the new comments accordingly.
     */
    function processComments() {
        var   post        = getId(window.location.search)
            , comments    = []
            , oldComments = []
            , comheads    = {}
            ;

        // Populate comments
        $(".comhead a:last-child").each(function(index) {
            var cid = getId($(this).attr('href'));
            comments.push(cid);
            comheads[cid] = $(this).parent();
        });

        // Try to retrieve existing set of comments for this page
        if (post in localStorage) {
            oldComments = JSON.parse(localStorage.getItem(post));

            // Determine new comments and mark them visually
            var newComments = comments.diff(oldComments);
            $.each(newComments, function(index, cid) {
                comheads[cid].css('background-color', NEW_COMMENT_HIGHLIGHT);
            });

            /* If appropriate, update page to reflect number of new comments */
            // New comment notification text
            var ncomm = newComments.length ? newComments.length : "No";
            ncomm    += " new comment";
            if (newComments.length != 1) {
                ncomm+= 's';
            }

            // Update page
            var nspot = $("center > table > tbody > tr > td > br:first");
            var ntext = '<div style="margin-left: 25px; color: black;">'
                      + '<p><strong>' + ncomm + '</strong></p></div>'
                      ;
            nspot.replaceWith(ntext);
        }

        // Save to browser storage
        localStorage.setItem(post, JSON.stringify(comments));
    }

    function processPosts() {
    }

    processComments();
}

/* Injects jQuery into the page and makes useable from the below Greasemonkey
   script.

   Snippet via
   http://erikvold.com/blog/index.cfm/2010/6/14/using-jquery-with-a-user-script
 */
function addJQuery(callback) {
    var script = document.createElement("script");
    script.setAttribute("src", jqURL);
    script.addEventListener('load', function() {
            var script = document.createElement("script");
            script.textContent = "(" + callback.toString() + ")();";
            document.body.appendChild(script);
            }, false);
    document.body.appendChild(script);
}
<commit_msg>Move text pluralization logic to separate function (pluralizeText).<commit_after>// ==UserScript==
// @name          Hacker News: New Comments
// @namespace     http://mschade.me/userscripts
// @description   Make new comments on Hacker News more noticeable. Created by <NAME> (@michaelschade, http://mschade.me/)
// @match         http://news.ycombinator.com/item?id=*
// @include       http://news.ycombinator.com/item?id=*
// ==/UserScript==

// URL to jQuery Library
var jqURL = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";

function main() {
    /* Options */
    var NEW_COMMENT_HIGHLIGHT = 'yellow';

    /* Add ability to compute difference between two arrays.

       Snippet via http://stackoverflow.com/q/4026828
     */
    Array.prototype.diff = function(a) {
        return this.filter(function(i) {return !(a.indexOf(i) > -1);});
    };

    /* Strips the prefix of query string to retrieve the intended id. */
    function getId(s) {
        return s.replace("item", "").replace("?id=", "");
    }

    /* Makes text plural only if count is not one. For example:

       pluralizeText("new post", 0) => "0 new posts"
       pluralizeText("new post", 1) => "1 new post"
       pluralizeText("new post", 2) => "2 new posts"
     */
    function pluralizeText(text, count) {
        var ntext  = count ? count : "No";
        ntext     += ' ' + text;
        if (count != 1) {
            ntext += 's';
        }
        return ntext;
    }

    /* Processes the comments on page to determine which, if any, are new,
       marking the new comments accordingly.
     */
    function processComments() {
        var   post        = getId(window.location.search)
            , comments    = []
            , oldComments = []
            , comheads    = {}
            ;

        // Populate comments
        $(".comhead a:last-child").each(function(index) {
            var cid = getId($(this).attr('href'));
            comments.push(cid);
            comheads[cid] = $(this).parent();
        });

        // Try to retrieve existing set of comments for this page
        if (post in localStorage) {
            oldComments = JSON.parse(localStorage.getItem(post));

            // Determine new comments and mark them visually
            var newComments = comments.diff(oldComments);
            $.each(newComments, function(index, cid) {
                comheads[cid].css('background-color', NEW_COMMENT_HIGHLIGHT);
            });

            /* Update top of page to reflect number of new comments. */
            var nspot = $("center > table > tbody > tr > td > br:first");
            var ntext = '<div style="margin-left: 25px; color: black;">'
                      + '<p><strong>'
                      + pluralizeText("new comment", newComments.length)
                      + '</strong></p></div>'
                      ;
            nspot.replaceWith(ntext);
        }

        // Save to browser storage
        localStorage.setItem(post, JSON.stringify(comments));
    }

    function processPosts() {
    }

    processComments();
}

/* Injects jQuery into the page and makes useable from the below Greasemonkey
   script.

   Snippet via
   http://erikvold.com/blog/index.cfm/2010/6/14/using-jquery-with-a-user-script
 */
function addJQuery(callback) {
    var script = document.createElement("script");
    script.setAttribute("src", jqURL);
    script.addEventListener('load', function() {
            var script = document.createElement("script");
            script.textContent = "(" + callback.toString() + ")();";
            document.body.appendChild(script);
            }, false);
    document.body.appendChild(script);
}

{'fraction_non_alphanumeric': 0.13780418788907753, 'fraction_numerical': 0.006791171477079796, 'mean_word_length': 2.0694745983499785, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '634226', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1972, 'n_words': 498}