bangumi 回复排序

bangumi 回复排序,左下角有按钮控制,input可以输入回复序号,最下方的数字是回复总数

Verzia zo dňa 03.01.2018. Pozri najnovšiu verziu.

// ==UserScript==
// @name         bangumi 回复排序
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  bangumi 回复排序,左下角有按钮控制,input可以输入回复序号,最下方的数字是回复总数
// @author       鈴宮華緋
// @include      /https?:\/\/(bgm\.tv|bangumi\.tv|chii\.in)\/(group|subject)\/topic.*/
// @require      http://code.jquery.com/jquery-latest.js
// ==/UserScript==

(function() {
    function postsort() {
        var idlist = [];
        var nowid = 0;
        var re = /post_\d*/;
        var divs = $("div");
        var len = divs.length;
        console.log(len);
        function sortNumber(a,b)
        {
            return a - b;
        }
        function move() {
            //location.hash = "#post_" + nowid;
            console.log($("#post_" + nowid).offset());
            divs.removeClass("reply_highlight");
            $("#post_" + nowid).addClass("reply_highlight");
            $('html').animate({
                scrollTop: $("#post_" + nowid).offset().top - 30
            }, 100);
        }
        for(var i = 0; i < len; i++){
            var id = divs.eq(i).attr("id");
            //console.log(id);
            if (re.test(id)){
                id = id.replace("post_","");
                id = parseInt(id);
                idlist.push(id);
            }
        }
        idlist.sort(sortNumber);
        console.log(idlist);
        $("body").append("<div id='postsort_buttongroup'><button type='previous' class='postsort_button'>▲</button><button type='next' class='postsort_button'>▼</button><input id='postnumber'><div id='postcount'></div></div>");
        $("#postsort_buttongroup").css({
            "position" : "fixed",
            "bottom" : 40,
            "left" : 20,
            "width" : "40px",
            "background" : "rgb(20,120,220)",
            "padding" : "2px"
        });
        $("#postnumber").css({
            "width" : "100%",
            "height" : "20px",
            "margin-top" : "2px",
            "color" : "rgb(20,120,220)",
            "text-align" : "center",
            "border" : "none",
            "outline" : "none"
        }).bind('input propertychange', function() {
            if($(this).val() <= 0 || $(this).val() > idlist.length) {
                return;
            }
            nowid = idlist[$(this).val() - 1];
            move();
        });
        $("#postcount").css({
            "width" : "100%",
            "height" : "20px",
            "background" : "rgb(20,120,220)",
            "color" : "white",
            "text-align" : "center",
            "cursor" : "pointer"
        }).text(idlist.length).click(function() {
            $("#postnumber").val(idlist.length).trigger('propertychange');
        });
        $(".postsort_button").css({
            "height" : "30px",
            "width" : "100%",
            "display" : "block",
            "background" : "rgb(20,120,220)",
            "color" : "white",
            "border" : "none",
            "outline" : "none"
        }).hover(function() {
            $(this).css({
                "background" : "rgb(55,155,255)",
            });
        },function() {
            $(this).css({
                "background" : "rgb(20,120,220)",
            });
        }).click(function() {
            var type = $(this).attr("type");
            if(nowid === 0) {
                nowid = idlist[idlist.length - 1];
            } else {
                if(type == "previous") {
                    for(var i = idlist.length - 1; i >= 0; i--) {
                        if(idlist[i] < nowid) {
                            nowid = idlist[i];
                            break;
                        }
                    }
                } else if(type == "next") {
                    for(var j = 0; j < idlist.length; j++) {
                        if(idlist[j] > nowid) {
                            nowid = idlist[j];
                            break;
                        }
                    }
                }
            }
            $("#postnumber").val($.inArray(nowid, idlist) + 1);
            move();
        });
    }
    setTimeout(postsort,0);
})();