fafranco82 / Favoritos PorDD

// ==UserScript==
// @name         Favoritos PorDD
// @namespace    http://franco.nom.es/
// @version      0.3
// @description  GestiĆ³n de favoritos de PorDescargaDirecta
// @author       fafranco82
// @match        http://pordescargadirecta.com/subscription.php?folderid=1
// @require      https://code.jquery.com/jquery-2.2.3.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery.rest/1.0.2/jquery.rest.min.js
// @require      https://cdn.rawgit.com/caolan/async/master/dist/async.min.js
// @require      https://cdn.rawgit.com/padolsey-archive/jquery.fn/master/sortElements/jquery.sortElements.js
// @grant        none
// ==/UserScript==

(function($) {
    'use strict';
    
    
    var safeParse = function(n) {
        try {
            var r = parseInt(n, 10);
            if(isNaN(r)) {
                return null;
            } else {
                return r;
            }
        } catch(e) {
            return null;
        }
    };
    
    var client = new $.RestClient('http://api.franco.nom.es/collections/');
    client.add('pordd');
    
    $(document).ready(function($) {
        
        async.each($('#threads li.threadbit'), function(li, done) {
            var $li = $(li);
            var $info = $('<div>').css({
                position: 'relative',
                float: 'left',
                clear: 'right',
                width: '4em',
                border: '1px solid black',
                borderRadius: '5px',
                backgroundColor: 'white',
                padding: '0.5em',
                fontSize: '1.5em',
                fontWeight: 'bold',
                marginRight: '10px',
                textAlign: 'center',
                cursor: 'pointer',
                whitespace: 'no-wrap'
            }).html('LOADING').on('end', function() {
                 $(this).html('&nbsp;&nbsp;&nbsp;').off('click');
            });
            $li.find('a.threadstatus').before($info).remove();
            var idTopic = /^thread_(\d+)$/.exec($li.attr('id'))[1];
            var info = /(\d\d)\/(..)/.exec($li.find('a.title').text());
            var currentChapter = safeParse(info[1]);
            var totalChapters = safeParse(info[2]);
            client.pordd.read(idTopic).done(function(data) {
                if(!data) {
                    $li.data('order', 0);
                    $info.html('NEW').click(function(e) {
                        e.stopPropagation();
                        client.pordd.create({"_id": idTopic, lastChapter: currentChapter}).done(function() {
                            $info.trigger('end');
                        });
                    });
                } else if (data.lastChapter < currentChapter) {
                    $li.data('order', 0);
                    $info.html('UPD<br/>'+data.lastChapter+' &#8594; '+currentChapter).click(function(e) {
                        e.stopPropagation();
                        client.pordd.update(idTopic, {lastChapter: currentChapter}).done(function() {
                            $info.trigger('end');
                        });
                    });
                } else if (data.lastChapter == totalChapters) {
                    $li.data('order', 2);
                    $info.html('END');
                } else {
                    $li.data('order', 1);
                    $info.trigger('end');
                }
                done();
            });
        }, function(err) {
            $('#threads li.threadbit').sortElements(function(a, b){
                if($(a).data('order') == $(b).data('order')) {
                   return $(a).find('a.title').text() > $(b).find('a.title').text() ? 1 : -1;
                } else {
                    return $(a).data('order') > $(b).data('order') ? 1 : -1;
                }
            });
            console.log("DONE");
        });
    });
})(jQuery);