r/programmingrequests • • Mar 08 '16

Can someone make a script that counts the amount of karma /u/warlizard has acquired from commenting "ಠ_ಠ"?

Can someone make a script that counts the amount of karma /u/warlizard has acquired from commenting "ಠ_ಠ"?

3 Upvotes

1 comment sorted by

4

u/LenAnderson Mar 08 '16 edited Mar 08 '16

You can paste this in the browser console (F12). If you want to search for other users / different comment content just replace the user and content variables at the beginning of the script.

(function() {
    //
    // CONFIG
    //
    var user = 'warlizard';
    var content = 'ಠ_ಠ';
    
    
    
    var karma = 0;
    var comments = 0;
    var pages = 0;
    countKarmaOnPage(location.protocol+'//'+location.host+'/u/'+user);
    
    function countKarmaOnPage(url) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.onload = function() {
            pages++;
            var html = document.createElement('div');
            html.innerHTML = xhr.responseText;
            Array.prototype.forEach.call(html.querySelectorAll('#siteTable > .thing.comment'), function(thing) {
                if (thing.querySelector('.entry > .usertext > .usertext-body > .md').textContent.trim() == content) {
                    karma += thing.querySelector('.entry > .tagline > .score.unvoted').textContent.trim().replace(/^(\d)+[^\d]*$/, '$1')*1;
                    comments++;
                }
            });
            console.info('Page', pages);
            console.log('\t', 'Comments', comments);
            console.log('\t', 'Karma', karma);
            var next = html.querySelector('.nav-buttons > .nextprev > [rel="nofollow next"]');
            if (next && next.href) {
                countKarmaOnPage(next.href);
            } else {
                console.info('DONE');
            }
        };
        xhr.send();
    }
})();