fafranco82 / EDGE - En desarrollo

// ==UserScript==
// @name         EDGE - En desarrollo
// @namespace    http://franco.nom.es/
// @version      0.5
// @description  Muestra los cambios de estado en la página de desarrollo de EDGE
// @author       Francisco Franco Eslava
// @match        http://www.edgeent.com/en_desarrollo
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery.rest/1.0.2/jquery.rest.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/forerunnerdb/1.3.909/fdb-all.js
// @require      https://cdn.rawgit.com/caolan/async/master/dist/async.min.js
// @grant        none
// ==/UserScript==

(function(window) {
    'use strict';
   
    function getdata(elem) {
        var $elem = $(elem);
        return {
            id: $elem.attr('id'),
            name: $elem.find('.product_name a:eq(0)').text(),
            category: $elem.data('cat_name'),
            collection: $elem.data('col_name'),
            universe: $elem.data('uni_name'),
            group: $elem.find('.product_name a span:eq(1)').text(),
            code: $elem.find('.product_view_detail .text_smallest').text(),
            status: $elem.find('.product_status .bold').text(),
            date: $elem.find('.product_status .text_smallest .text_smallest').text()
        };
    }
    
    function markChange(elem, old, current) {
        var $elem = $(elem);
        $elem.find('.product_upper_container').css('background-color', 'yellow');
        $elem.attr('data-recent', 'yes');
        
        if(old) {
            var $td = $elem.find('.product_status_name td').eq(0);
            if(old.status!=current.status) {
                $('<del>').text(old.status).css('margin-right', '2px').prependTo($td.find('span.bold'));
            }
            if(old.date!=current.date) {
                $('<del>').text(old.date).css('margin-right', '2px').prependTo($td.find('span.remove_bold span.text_smallest'));
            }
        }
    }
    
    function testChange(old, current) {
        if(!old)
            return true;
        
        return old.status!=current.status || old.date != current.date;
    }
    
    var fbd = new ForerunnerDB();
    var db = fbd.db('edgeent');
    var news = db.collection('news', {primaryKey: 'code'});        
    
    $(document).ready(function() {
        var games = $('div.five_grid_games_collection_page');
        
        var html = 
            '<div id="recent_filters" class="news_box">' +
            '	<div class="news_box_content">' +
            '        <h6><span class="color_grey_dark">Filtrar por recientes</span></h6>' +
            '    </div>    ' +
            '    <hr>    ' +
            '    <div class="news_box_content">' +
            '    	<div class="tags_checked filter" id="filter-recent_all" data-recent_name="all">Todos</div>' +
            '    	<div class="tags filter" id="filter-recent_only" data-recent_name="only">Solo recientes</div>' +
            '    </div>' +
            '</div>';
        
        $(html).insertAfter('.news_box:eq(0)');
        
        function updateHeaders() {
            $('section.grid_image_mode > div > div:first-child').each(function() {
                var $header = $(this);
                var $elems = $(this).add($header.nextAll('hr'));
                if($header.nextAll('div:visible').length === 0) { 
                    $elems.hide(); 
                } else { 
                    $elems.show(); 
                }
            });   
        }
        
        $('#recent_filters .filter').click(function(e) {
            e.stopPropagation();
            $('#recent_filters .filter').removeClass('tags_checked').addClass('tags');
            $(this).removeClass('tags').addClass('tags_checked');
            
            var recent_name = $(this).data('recent_name');
            $('[data-recent]').show();
            if(recent_name == 'only')
                $('[data-recent=no]').hide();
            updateHeaders();
        });
        
        async.series([function(cb) {
            news.load(function(err) {
                cb(err);
            });
        }, function(done) {
            async.each(games, function(game, cb) {
                $(game).attr('data-recent', 'no');
                var data = getdata(game);
                var article = news.findById(data.code);
                if(testChange(article, data)) {
                    markChange(game, article, data);
                }
                if(!article)
                    news.insert(data);
                else
                    news.updateById(data.code, data);
                cb();
            }, function(err) {
                done(err);
            });
        }, function(cb) {
            news.save(function(err) {
                cb(err);
            });
        }], function(err) {
            console.log("DONE");
            $('#recent_filters .filter:last-child').click();
            window.news = news;
        });
    });
})(window);