Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <style>
            body {
                background-color: #222;
                color: #FFF;
            }
        </style>
    </head>
    <body>
        <script src="test.js"></script>
    </body>
</html>
 
var parseLinks = function(data) {
    var div = data.match(/<div class="reddit-link .*?><\/div><\/div>/g);
    var href, title, week, diff, lastLink, extra, lastWeek;
    for(var i = 0, len = div.length; i < len; i++) {
        href = div[i].match(/href="(.*?)"/)[1];
        title = div[i].match(/href.*>(.*)<\/a><br /)[1]
        extra = title.match(/^(?:\[Monthly |\[Weekly )/i);
        try {
            week = title.match(/Challenge\s*#\s*(\d{1,})/i)[1];
            diff = title.match(/\[Easy]|\[Intermediate]|\[Hard]|\[Difficult]/i)[0];
        }
        catch(e){
            if( extra == null ) {
                console.log('warning: div unhandled\n',href,'\n',title,'\n',div[i]);
                continue;
            }
            week = lastWeek;
            diff = '[Bonus]';
        }
        lastWeek = week;
        diff = diff[0] + diff[1].toUpperCase() + diff.slice(2).toLowerCase();
        if( diff == '[Difficult]' ) diff = '[Hard]';
        weeks[week] || (weeks[week] = {'[Easy]':[],'[Intermediate]':[],'[Hard]':[],'[Bonus]':[]});
        weeks[week][diff].push(' [' + title + '](' + href + ') ');
        extra = null;
    };
    lastLink =  div[div.length-1].match(/id-(.*?) /)[1];
    if( div.length < 25 || (div.length + links >= scrapeFor) ) genOutput();
    else { links += div.length; scrape( 'after=' + lastLink + '&' ); }
};
var genOutput = function() {
    var output = 'Easy | Intermediate | Hard | Weekly/Bonus<br />' +
                 '-----|--------------|------|-------------<br />' +
                 '| []() | []() | []() | **-** |<br />';
    Object.keys(weeks).sort((a,b) => b-a).forEach(function(week) {
        for(var diff in weeks[week]) {
            if( weeks[week][diff].length == 0 ) weeks[week][diff].push('[]()');
        }
        ['[Easy]','[Intermediate]','[Hard]','[Bonus]'].forEach(function(diff){
            output += '|' + weeks[week][diff].join();
        });
        output += '|<br />';
    });
    document.getElementsByTagName('body')[0].insertAdjacentHTML('afterbegin', output);
};
var scrape = function(after='') {
    var script = document.createElement('script');
    script.src = 'https://www.reddit.com/r/dailyprogrammer/new.embed?' + after + 'callback=parseLinks';
    document.getElementsByTagName('body')[0].appendChild(script);
    script.parentNode.removeChild(script);
};
var weeks = {};
var links = 0;
var scrapeFor = 30;
scrape();
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers