Waifuist 8chan Enhancer 2

8chan extender with several functions. Included tripcode checkbox, youtube title loading, relative time and last seen tripnames.

目前為 2014-11-16 提交的版本,檢視 最新版本

// ==UserScript==
// @name           Waifuist 8chan Enhancer 2
// @include        https://8chan.co/*
// @description    8chan extender with several functions. Included tripcode checkbox, youtube title loading, relative time and last seen tripnames.
// @version        0.0.15
// @require     http://cdn.jsdelivr.net/jquery.timeago/1.4.1/jquery.timeago.js
// @namespace https://greasyfork.org/users/2657

// ==/UserScript==

//check if thread


if (window.location.href.indexOf("res") > -1) {
    
    //Config Vars
    var Configs = {linkimgopt:"OFF", arrows:"ON", noko:"ON", antiflood:"ON", quickreply:"ON", inlinereply:"ON", linkstoview:"ON", nofloat:"OFF", textformat:"ON", centerthread:"OFF", autorefresh:"ON", namecheck:"ON", lastseen:"ON", yttitles:"ON", relativetime:"ON"};
    var defCon = Configs;
    var Description = {linkimgopt:"Links To Images", arrows:"Scroll Arrows", noko:"Noko", antiflood:"Anti-Flood Detection", quickreply:"Quick Reply", inlinereply:"In-Line Reply", linkstoview:"Links To Buttons", nofloat:"Unfix Floating Boards", textformat:"Text-Formation Buttons", centerthread:"Center Threads", autorefresh:"Auto-Refresh", namecheck:"Tripcode Checkbox", lastseen:"Last Seen", yttitles:"Load YT titles", relativetime:"Show Relative Time"};
    $('form:first-of-type').addClass( "postboxy" );    
    
    //Load Configs System
    if (localStorage.getItem('Configs')){
        var Configs2 = localStorage.getItem('Configs');                                                                               
        Configs = JSON.parse(Configs2);
    };
    
    
    
    //Configs Saver
    function save(){
        localStorage.setItem("Configs", JSON.stringify(Configs));
    };
    
    //
    
    
    //Notifier/Counter
    
    var count = 0;
    
    $('#favicon').remove();
    $('head').append('<link href="http://i.imgur.com/wBQJqld.png" id="favicon" rel="shortcut icon">');
    
    $(document).on( "new_post", function() {
        count++;
        if (Configs["linkimgopt"] == "ON") {linkimg();};
        if (Configs["inlinereply"] == "ON") {genreply();};
        if (Configs["linkstoview"] == "ON") {linkstoview();};
    });
    
    var title_regex = /^\(\d+\) (.*)$/;
    var original_title = document.title;
    var match = title_regex.exec(document.title);
    if (match != null) {
        original_title = match[1];
    };
    
    setInterval(function() {
        var state2 = document["visibilityState"];
        if (state2 == "visible"){
            count = 0;};
        if (count > 0 && state2 == "hidden") {
            document.title = '('+count+') '+original_title;
            $('#favicon').remove();
            $('head').append('<link href="http://i.imgur.com/lfAW4fq.png" id="favicon" rel="shortcut icon">');
        };
    }, 10000);
    
    var onVisibilityChange = function (args) {
        var state = document["visibilityState"];
        if (state == "visible")
        {
            document.title = original_title;
            count = 0;
            $('#favicon').remove();
            $('head').append('<link href="http://i.imgur.com/wBQJqld.png" id="favicon" rel="shortcut icon">');
        }
    };
    
    document.addEventListener("visibilitychange", onVisibilityChange, false);
    
    
    
    //Auto-Refresh Json
    if (Configs["autorefresh"] == "ON") {
        $.fn.reverse = [].reverse;
        
        var URL = window.location.pathname;
        var URLs = URL.slice(0, -5);
        var URLf = URLs.split("/");
        var board_name = URLf[1];
        
        setInterval(function () {
            $.getJSON(configRoot + board_name + "/0.json", function (j) {
                var new_threads = 0;
                
                j.threads.forEach(function (t) {
                    var s_thread = $("#thread_" + t.posts[0].no);
                    
                    if (s_thread.length) {
                        var my_posts = s_thread.find(".post.reply").length;
                        
                        var omitted_posts = s_thread.find(".omitted");
                        if (omitted_posts.length) {
                            omitted_posts = omitted_posts.html().match("^[^0-9]*([0-9]+)")[1] | 0;
                            my_posts += omitted_posts;
                        }
                        
                        my_posts -= t.posts[0].replies | 0;
                        my_posts *= -1;
                        
                        if (my_posts != 0) {
                            
                            my_posts = 0;
                            
                            $.ajax({
                                url: document.location,
                                success: function(data) {
                                    $(data).find('div.post.reply').each(function() {
                                        var id = $(this).attr('id');
                                        if($('#' + id).length == 0) {
                                            $(this).insertAfter($('div.post:last').next()).after('<br class="clear">');
                                            $(document).trigger('new_post', this);
                                        }
                                    });
                                    
                                }
                            });
                            
                            
                        };
                        
                    } else {
                        
                    }
                });
                
            });
        }, 10000);
    };
    
    
    //Menu
    
    $('body').append("<div class='menu' style='position:fixed; right:10%; bottom:20%; width:49px; background:grey;'></div>")
    if (Configs["arrows"] == "ON") {
        $('.menu').append("<center><font size='30'><a alt='Scroll Up' style='text-decoration: none' href='javascript:window.scrollTo(0,0);'>▲</a><br><a style='text-decoration: none' href='javascript:window.scrollTo(0,900000000);'>▼</a>")
    };
    
    
    //Config Menu    
    $("body").append("<div class='optionslink' style='cursor: pointer; cursor: hand; width:auto;'>[Script Options]</div>")
    $("body").append("<div class='optionsmenu' style='display:none; position:fixed; right:10%; top:5%; width:200px; height:300px; background:grey; padding:5px;'></div>")
    $(document).on('click', ".optionslink", function() {
        $(".optionsmenu").css("display","inline");
        $('.optionslink').html("[Close Script Options]");
        $('.optionslink').attr('class', 'optionslinkclose');
        
    });
    $(document).on('click', ".optionslinkclose", function() {
        $(".optionsmenu").css("display","none");
        $('.optionslinkclose').html("[Script Options]");
        $('.optionslinkclose').attr('class', 'optionslink');
    });
    
    
    
    function renmenu(){
        if (localStorage.getItem('Configs')){
            var Configs2 = localStorage.getItem('Configs');                                                                               
            Configs = JSON.parse(Configs2);
        };
        
        rendermenu = '';
        $.each(Configs, function(i, key) {
            rendermenu += '<div alt="' + i + '" class="menubutton" style="cursor: pointer; cursor: hand; width:auto;">' + Description[i] + ' [' + Configs[i] + ']</div>';
            
        });
        var bgurl2;
        if (localStorage.getItem('bgurl')){
            bgurl2 = localStorage.getItem('bgurl');
        };
        rendermenu += '<div class="reset" style="cursor: pointer; cursor: hand; width:auto;">[Reset Configurations]</div>'
        rendermenu += '<div class="backbround" style="cursor: pointer; cursor: hand; width:auto;">BG URL:<input type="text" name="bgurl" class="bgurl" size="10" autocomplete="off" value="' + bgurl2 + '"></div>'
        
        $(".optionsmenu").html(rendermenu);        
    };
    
    renmenu();
    
    $(document).on('click', ".reset", function() { 
        delete window.localStorage["Configs"]        
        Configs = defCon;
        save();
        renmenu();
    });
    
    var bgstore = $('body').css('background');
    //    localStorage.setItem("bgurl", "derp");
    $( ".bgurl" ).change(function() {
        bgurl = $('.bgurl').val();
        localStorage.setItem("bgurl", bgurl);        
        if (bgurl.match(/\.(jpg|png|gif)/g)){
            $('body').css('background', 'url("' + bgurl + '")');
        } else { $('body').css('background', bgstore)};
    });
    if (localStorage.getItem('bgurl')){
        if (localStorage.getItem('bgurl').match(/\.(jpg|png|gif)/g)){
            $("<style>").text("body {background:url('" + localStorage.getItem('bgurl') + "');}").appendTo("head"); 
        };};
    
    $(document).on('click', ".menubutton", function() {
        buttonid = $(this).attr('alt');
        changevar = Configs[buttonid];
        if (changevar === "ON") {changevar = "OFF";} else {changevar = "ON";};
        Configs[buttonid] = changevar;
        save();
        renmenu();        
    });
    
    // Custom Background
    
    
    
    //linkimg
    function linkimg(){
        $('a[href$=".png"][rel="nofollow"], a[href$=".jpg"][rel="nofollow"], a[href$=".gif"][rel="nofollow"]').each(function(){
            $(this).replaceWith('<div class="linkedimg2" style="width:100px;display:inline-block;"><img class="linkedimg2" style="max-width: 100%;" src="' + $(this).attr('href') + '" /></div>');
        });
        
        $(document).on('click', ".linkedimg2", function() {
            $(this).css('width', 'auto');
            $(this).attr('class', 'clicked');
        });
        
        $(document).on('click', ".clicked", function() {
            $(this).css('width', '100px');
            $(this).attr('class', 'linkedimg2');
        });
        
    };
    
    // Tripcode Checkbox
    if (Configs["namecheck"] == "ON") {
        $("tr th:contains('Name')").append(' <input type="checkbox" name="Nom" class="checkname" value="Nom" checked>');
        var checker = localStorage.getItem('check');
        if (checker == "OFF"){ $('.checkname').prop('checked', false) 
        $("[name='name']").prop('disabled', true)}
        
        $(".checkname").change(function() {
            if(this.checked) { localStorage.setItem("check", "ON")
            $("[name='name']").prop('disabled', false)
                             } else { localStorage.setItem("check", "OFF")
                             $("[name='name']").prop('disabled', true)};
        });
    };
    
    if (Configs["linkimgopt"] == "ON") {linkimg();};
    
    // No Floating Boards
    
    if (Configs["nofloat"] == "ON") {$(document).ready(function(){ $('html').removeClass("desktop-style"); $('html').removeClass("mobile-style");});};
    
    
    //linkstoview
    function linkstoview(){
        $('a[href$=".png"][rel="nofollow"], a[href$=".jpg"][rel="nofollow"], a[href$=".gif"][rel="nofollow"]').each(function(){
            var linkURL = $(this).attr('href');
            $(this).replaceWith('<div class="viewbutton" id="' + $(this).attr('href') + '"><b>[Click to View Image]</b> <br>' + $(this).attr('href') + '</a></div>')
        });
        
        
        $(document).on('click', ".viewbutton", function() {
            $(this).replaceWith('<br><div class="linkedimg2" style="width:100px;display:inline-block;"><img class="linkedimg2" style="max-width: 100%;" src="' + $(this).attr('id') + '" /></div>');
        });
        
        $(document).on('click', ".linkedimg2", function() {
            $(this).css('width', 'auto');
            $(this).attr('class', 'clicked');
        });
        
        $(document).on('click', ".clicked", function() {
            $(this).css('width', '100px');
            $(this).attr('class', 'linkedimg2');
        });
        
    };
    
    if (Configs["linkstoview"] == "ON") {linkstoview();};
    
    
    // Quick Reply
    if (Configs["quickreply"] == "ON") {
        
        
        
        
        $('.menu').append("<center><font size='30'><a style='text-decoration: none' class='QR'><img style='cursor: pointer; cursor: hand;' src='http://i.imgur.com/f8L6S1O.png'></a><a style='text-decoration:none;display:none;cursor: pointer; cursor: hand;' class='QR2'>X</a><br>");
        $("<style>").text(".postboxy { resize:horizontal; overflow:auto; padding:5px; max-width:80%;}").appendTo("head"); 
        $('body').on('click', '.QR', function() {
            $('.close-btn').click();
            $('.postboxy').after("<div class='dummytext' style='height:350px;'></div>");
            $('.postboxy').css({"background-color":"#D1D1D1", position:"fixed", top:"1%", right:"calc(10% + 49px)"});   
            $(".posttable").css("margin","auto");
            $(".QR").css("display","none");
            $(".QR2").css("display","inline");
            wii = screen.width / 2;
            hii = screen.height / 2;
            $('#body').css({width:"100%", "max-width":"100%", resize:"vertical", "max-height":hii});
            $('.posttable').css({width:"100%",  resize:"none"});
            //$(".posttable").css("margin","inherit");
            
            
            
        });
        $('body').on('click', '.QR2', function() {
            $( ".dummytext" ).remove();
            $('.postboxy').css({"background-color":"", position:"", top:"", right:""});
            $('.postboxy').insertAfter($('.banner'));
            $(".posttable").css("margin","auto");
            $(".QR").css("display","inline");
            $(".QR2").css("display","none");
            $('#body').css({width:"", height:""});
            $('.posttable').css({width:"auto",  resize:"none"});
            
        });
    };
    
    // In-Line Reply
    if (Configs["inlinereply"] == "ON") {
        $('table:first-of-type').addClass( "posttable" );
        function quotador(id){
            $("textarea[id*='body']")[0].value += '>>' + id + '\n';
            reply_id = 'reply_' + id;
            $( ".dummytext" ).remove();
            $('.banner').after("<div class='dummytext' style='height:301px;'></div>");
            $('.postboxy').insertAfter("#" + reply_id);   
            $(".posttable").css("margin","inherit");
            $(".QR").css("display","none");
            $(".QR2").css("display","inline");
        };
        
        function genreply(){
            $("iframe").removeAttr('style');
            if(window.location.href.indexOf("res") > -1) {
                $('.quotes').remove();
                $( "div[id^='reply_']" ).each(function() {
                    var replyid = $(this).attr("id")
                    replyid = replyid.replace("reply_", "");
                    $(this).find('> .intro').append('<div class="quotes" id="'+ replyid +'" style="display:inline-block; float:right; background-color: #FF6600; border: 1px solid; margin: 3px; padding: 3px; cursor: pointer; cursor: hand;">Reply</div></div>');
                    
                })};
        };
        
        genreply();
        
        $('body').on('click', 'div.quotes', function() {
            var currentId = $(this).attr('id');
            $(".postboxy").removeAttr('style');
            quotador(currentId);
        });
        
    };
    
    
    
    //Text Formatting
    if (Configs["textformat"] == "ON") {
        jQuery.fn.extend({
            insertAtCaret: function(myValue, myValueE){
                return this.each(function(i) {
                    if (document.selection) {
                        //For browsers like Internet Explorer
                        this.focus();
                        sel = document.selection.createRange();
                        sel.text = myValue + myValueE;
                        this.focus();
                    }
                    else if (this.selectionStart || this.selectionStart == '0') {
                        //For browsers like Firefox and Webkit based
                        var startPos = this.selectionStart;
                        var endPos = this.selectionEnd;
                        var scrollTop = this.scrollTop;
                        this.value = this.value.substring(0,     startPos)+myValue+this.value.substring(startPos,endPos)+myValueE+this.value.substring(endPos,this.value.length);
                        this.focus();
                        this.selectionStart = startPos + myValue.length;
                        this.selectionEnd = ((startPos + myValue.length) + this.value.substring(startPos,endPos).length);
                        this.scrollTop = scrollTop;
                    } else {
                        this.value += myValue;
                        this.focus();
                    }
                })
            }
        });
        $('#body').after('<br class="stylebar">');
        
        $('.stylebar').after('<input class="formbutton_SS" type="button" value="Spoiler" /><input class="formbutton_B" type="button" value="B" /><input class="formbutton_R" type="button" value="R" /><input class="formbutton_S" type="button" value="S" /><input class="formbutton_I" type="button" value="I" /><input class="formbutton_C" type="button" value="Code" />');
        $('.formbutton_I').css('font-style', 'italic');
        $('.formbutton_S').css('text-decoration', 'line-through');
        $('.formbutton_R').css('color', 'red');
        $('.formbutton_B').css('font-weight', 'bold');
        $('.formbutton_SS').css('font-weight', 'bold');
        
        $('.formbutton_C').on('click', function(){ 
            $("textarea[id*='body']").insertAtCaret("[code]", "[/code]");
        });
        $('.formbutton_I').on('click', function(){ 
            $("textarea[id*='body']").insertAtCaret("''", "''");
        });
        $('.formbutton_S').on('click', function(){ 
            $("textarea[id*='body']").insertAtCaret("~~", "~~");
        });
        $('.formbutton_R').on('click', function(){ 
            $("textarea[id*='body']").insertAtCaret("==", "==");
        });
        $('.formbutton_B').on('click', function(){ 
            $("textarea[id*='body']").insertAtCaret("'''", "'''");
        });
        $('.formbutton_SS').on('click', function(){ 
            $("textarea[id*='body']").insertAtCaret("**", "**");
        });
    };
    // Last Seen
    
    if (Configs["lastseen"] == "ON") {
        var $data = [];
        $data = [];
        var $names = [];
        $('.intro').each(function() {
            var $this = $(this);    
            $nam = $this.find('.name').text() + $this.find('.trip').text();
            $names.push($nam);
        });
        
        var uniqueNames = [];
        $.each($names, function(i, el){
            if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
        });
        
        var $maxtimes = {};
        
        $.each( uniqueNames, function( index, value ) {
            var max = 0;
            var themax = [];
            var naname = value;
            
            $( 'div:contains("' + value + '")' ).each(function(value) {
                var $this = $(this);        
                var then = Date.parse($this.find( "time:last" ).attr("datetime"));
                if(then > max)
                {
                    max = then;
                };
                $maxtimes[naname] = then;
            });
        });
        
        var now = "";
        now = $( "time:last" ).attr("datetime");
        dnow = Date.parse(now);
        var d = new Date();
        var n = d.toJSON();
        var z = Date.parse(n);
        $.each( uniqueNames, function( index, value ) {
            then = $maxtimes[value]
            sub = z - then;
            seconds = sub / 1000;
            minutes = Math.floor(seconds / 60);
            hours = Math.floor(minutes / 60);
            if (hours > 0){
                thecount = hours + " Hours Ago." }else{ thecount = minutes + " Minutes Ago." };
            
            $data +=  value + " - " + thecount + "<br>";    
        });
        
        $('.uselist').remove();
        $('.blotter').append('<div class="uselist"><br><br>Users seen: <br>' + $data + '</div>');
        
        function uptime(){
            $data = [];
            $('.intro').slice(-5).each(function() {
                var $this = $(this);    
                $nam = $this.find('.name').text() + $this.find('.trip').text();
                var datet = Date.parse($this.find( "time:last" ).attr("datetime"));
                $maxtimes[$nam] = datet;
            });
            var now = "";
            now = $( "time:last" ).attr("datetime");
            dnow = Date.parse(now);
            var d = new Date();
            var n = d.toJSON();
            var z = Date.parse(n);
            $.each( $maxtimes, function( index, value ) {
                then = $maxtimes[index]
                sub = z - then;
                seconds = sub / 1000;
                minutes = Math.floor(seconds / 60);
                hours = Math.floor(minutes / 60);
                if (hours > 0){
                    thecount = hours + " Hours Ago." }else{ thecount = minutes + " Minutes Ago." };
                
                $data +=  index + " - " + thecount + "<br>";    
            });
            
            $('.uselist').remove();
            $('.blotter').append('<div class="uselist"><br><br>Users seen: <br>' + $data + '</div>');
        };
        
        $(document).on( "new_post", function() {
            uptime();
        });
        
        setInterval(function () {
            uptime();
        }, 6000);
    };
    
    //Youtube Titles
    
    function yttitle(me){
        var myregexp = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i;
        var src = $(me).attr("href"); 
        $(me).append(" - Loading Title...");
        var id = src.match(myregexp)[1];
        var link = me;
        $.ajax({
            url: "http://gdata.youtube.com/feeds/api/videos/" + id + "?v=2&alt=jsonc",
            
            dataType: "json",
            success: function(data) {
                parseresults(link,data)
            }
        });
        
        function parseresults(link,result) {
            var linktitle = result.data.title;
            $(link).text(linktitle); //setting title from extracted id
        };
        
        
        
    };
    
    if (Configs["yttitles"] == "ON") {
        $( document ).ready(function() {
            $('a[href*="youtube.com/watch?v="]').each(function() { 
                yttitle(this); 
            });   
        });
        
        $(document).on( "new_post", function() {
            $('a[href*="youtube.com/watch?v="]').slice(-5).each(function() { 
                yttitle(this); 
            });   
        });
        
    };
    
    /************************************************************************
RELATIVE TIME (Taken from 8chanX, all credits due~)
*************************************************************************/
    if (Configs["relativetime"] == "ON") {
        $(document).ready(function() {  
            $("time").timeago();
        });
        
        // Show the relative time for new posts
        $(document).on('new_post', function (e, post) {  
            $("time").timeago();
        });
    };
        
        //smaller functions
        
        if (Configs["centerthread"] == "ON") {
            
            
            $("<style>").text("div.post.reply.post-hover { width:auto;} body {width:70%; margin:auto;} div.post.reply {width:100%;}").appendTo("head"); 
            
            
        };
        
        if (Configs["antiflood"] == "ON") {
            
            function makeid(){
                var text = "";
                var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";    
                for( var i=0; i < 10; i++ )
                    text += possible.charAt(Math.floor(Math.random() * possible.length));    
                return text;
            };
            
            function checkandffill(){
                $("#body").each(function() {
                    if($(this).val() === "")
                        var number = 12345678 + Math.floor(Math.random() * 6);
                    $("#body").text(makeid());
                });};
            
            $nofill = 'no';
            
            $(".post_no").click(function(){
                $nofill = 'yes';
            });
            
            $("input[name='post']").click(function(){	
                if($nofill === "no"){
                    window.onload=checkandffill();
                };
            });
            
        };
        
        //autonoko
        if (Configs["noko"] == "ON") {
            function noko(){
                document.getElementsByName("email", "input")[0].value = "noko";
            };
            
            
        };
        
    };