dragon0rca / Fresh4ever

// ==UserScript==
// @name         Fresh4ever
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  Making 9gag great again!
// @author       You
// @include      http://9gag.com/?id=*
// @include      http://9gag.com/
// @updateURL    https://openuserjs.org/meta/dragon0rca/Fresh4ever.meta.js
// @grant        none
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
// @require      https://raw.githubusercontent.com/kapetan/jquery-observe/master/jquery-observe.js
// @require      https://code.jquery.com/ui/1.12.1/jquery-ui.js

// ==/UserScript==


var i = 0;
$(window).on('scroll',function(){
    if (!localStorage.getItem('names')) {
        var names = [];
    }
    else {
        var names = JSON.parse(localStorage.getItem('names'));
    }
    var x = $("article");
    var data = x.eq(i).attr("data-entry-url");
    if(names.indexOf(data) === -1 && x.eq(i).css('display') !== 'none'){
        names.push(data);
    }

    if($(window).scrollTop() >= x.eq(i).offset().top){

        localStorage.setItem("names", JSON.stringify(names));
        
        i++;
    }});

window.onbeforeunload = function(){

    var grab =JSON.parse(localStorage.getItem('names')) || [];
    if(window.location.href == "http://9gag.com/"){
        var article = $("article").eq(0).attr('data-entry-url'); 
        localStorage.setItem("links", JSON.stringify(article));
    }
    var articles = $("article"),
        lastUrl = grab[grab.length - 1],
        seenArticles = [];
    for (var i = 0; i < articles.length; i++) {
        seenArticles.push($(articles[i]));

        if ($(articles[i]).attr('data-entry-url') === lastUrl) {
            break;
        }
    }
    
    if(articles.length > 19){
        
        var a = seenArticles.pop().attr("data-entry-id");
        var b = seenArticles.pop().attr("data-entry-id");
        var c = seenArticles.pop().attr("data-entry-id");
        

        var nextpage = 'http://9gag.com/?id='+a+'%2C'+b+'%2C'+c+'&c';
        localStorage.setItem("url", nextpage);
    }
};

$(window).on('load',function(){
if(window.location.href == 'http://9gag.com/'){
var new_url =  localStorage.getItem('url');    
var buton = $("<div id='buton' style=' visibility:hidden; box-shadow:inset 0px 1px 0px 0px #bbdaf7; background:linear-gradient(to bottom, #79bbff 5%, #378de5 100%); background-color:#79bbff; border-radius:6px; border:1px solid #84bbf3; display:inline-block; cursor:pointer; color:#ffffff; font-family:Arial; font-size:15px; font-weight:bold; padding:6px 24px; text-shadow:0px 1px 0px #528ecc; text-decoration:none; position:fixed; top:765px; left:20px'>Dig 9gag</div>");
    buton.click(function(){
     window.location.replace(new_url);
     
     localStorage.removeItem('names');
    });
$('#container').append(buton);}
var dark = $("<div style='box-shadow:inset 0px 1px 0px 0px #bbdaf7; background:linear-gradient(to bottom, #507885 5%, #378de5 100%); background-color:#507885; border-radius:6px; border:1px solid #84bbf3; display:inline-block; cursor:pointer; color:#ffffff; font-family:Arial; font-size:15px; font-weight:bold; padding:6px 24px; text-shadow:0px 1px 0px #528ecc; text-decoration:none; position:fixed; top:800px; left:20px'>Dark theme</div>");
     var x = false;
    var sheet = document.styleSheets[0];
dark.on('click',function(){
   
    if(!x){
$('body').css('background-image',"url('http://eskipaper.com/images/black-floor-1.jpg')");
        sheet.addRule('.badge-item-title > a','color: white !important',1);
        sheet.addRule('.badge-entry-container','border: 0px !important',0);
        sheet.addRule('.badge-item-title > a','font-family: Comic Sans MS !important',0);
dark.text("Light theme");
     x = true;}
    else{
    $('body').removeAttr('style');
        sheet.deleteRule(3);
        
   
    dark.text("Dark theme");
     x = false;}
});
    $('#container').append(dark);
});

var exclude = JSON.parse(localStorage.getItem('links'));
$(".badge-entry-collection").observe("childlist","article[data-entry-url='"+exclude+"']",function(record){
 
    var div = $('#buton');
    
    div.animate({top:'350px'},"slow");
    div.css('visibility','visible');
    div.effect( "bounce", { times: 7 }, "slow" );
    div.effect("shake");
    div.animate({top:'765'},1600);
    div.effect( "bounce", { times: 10 }, "slow" );
    
});