Greasy Fork is available in English.

Acfun过滤UP计划

帮助你屏蔽不想看的UP主

Version vom 31.05.2019. Aktuellste Version

// ==UserScript==
// @name         Acfun过滤UP计划
// @namespace    http://tampermonkey.net/
// @version      1.06
// @description  帮助你屏蔽不想看的UP主
// @author       人文情怀
// @match        http://www.acfun.cn/a/ac*
// @match        http://www.acfun.cn/v/list63
// @match        https://www.acfun.cn/a/ac*
// @match        http://www.acfun.cn/*
// @match        https://www.acfun.cn/*
// @match        https://www.acfun.cn/v/list*
// @match        http://www.acfun.cn/a/ac*
// @match        https://www.acfun.cn/a/ac*
// @match        http://www.acfun.cn/v/ac*
// @match        https://www.acfun.cn/v/ac*
// @require      https://code.jquery.com/jquery-3.4.1.min.js
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM.getValue
// @grant        GM.setValue
// @run-at      document-idle
// ==/UserScript==


function core() {

    'use strict';
    $.info = unsafeWindow.$.info;
    let empty = (a) => {
        if (typeof a == "undefined") {
            return () => {
            };
        } else {
            return a;
        }
        ;
    };

    let GM_set = typeof GM_setValue == "undefined" ?
        function (key, value, callback, failcallback) {
            //console.log("GM.setvalue")
            let p = GM.setValue(key, value);
            p.then(empty(callback), empty(failcallback));


        }
    : function (key, value, callback, failcallback) {
        let res = GM_setValue(key, value);
        callback();
    };


    let GM_get = typeof GM_getValue == "undefined" ?
        function (key, value, callback, failcallback) {
            let p = GM.getValue(key, value);
            p.then((debug)=>{empty(callback)(debug); }, empty(failcallback));
            //console.log("empty", empty(callback));
        }
    : function (key, value, callback, failcallback) {
        let res = GM_getValue(key, value);
        callback(res);
    }

    function cleanList() {

        GM_set("ACFUN_BLOCK_LIST", []);
    }

    let up = function (callback) {
        GM_get("ACFUN_BLOCK_LIST", [], callback);
    };

    function addToList(id, callback) {
        //let old =
        up((old) => {
            if (old.indexOf(id) >= 0) {
                $.info.show("你已经屏蔽过UP主[" + id + "]啦!")
                return;
            }

            old.push(id);
            GM_set("ACFUN_BLOCK_LIST", old, () => {
                $.info.show("已将UP主[" + id + "]加入屏蔽列表")
                empty(callback)();

            });


        });
    }

    function removeFromList(id, callback) {
        up((old) => {
            let i = old.indexOf(id);
            if (i >= 0) {
                old.splice(i, 1);
            }
            GM_set("ACFUN_BLOCK_LIST", old, () => {
                $.info.show("已将UP主[" + id + "]移出屏蔽列表。");
                empty(callback)();
            });

        });
    }

    let filterButton = document.createElement("div");

    function initButton() {
        document.body.appendChild(filterButton);
        $(filterButton)
            .css("width", "15px")
            .css("height", "15px")
            .css("background-color", "rgba(255,0,0,1)")
            .css("font-familty", "'黑体',serif")
            .css("font-weight", "600")
            .css("font-size", "11px")
            .css("padding", "1.5px")
            .css("left", "0")
            .css("top", "0")
            .css("transform", "translate(0,0)")
            .css("display", "block")
            .css("position", "absolute")
            .css("z-index", "99999")
            .css("background-size", "contain")
            .css("background-image", "url('https://cdn.aixifan.com/dotnet/20130418/umeditor/dialogs/emotion/images/ac/12.gif')")
    }


    //获得所有主页的UP的DOM TAG
    function getHomeSelection(mark, mval, addMark) {
        if (typeof addMark == "undefined") {
            addMark = false;
        }
        let res = [];
        //过滤主页视频
        let selections = $("a[data-info]");
        for (let i = 0; i < selections.length; i++) {
            let tag = selections[i];
            let m = tag[mark];
            //if (addMark) tag[mark] = mval;
            if (typeof mark !== "undefined" && m !== mval && !addMark) {
                //continue;
            }
            let info = $(tag).attr("data-info");
            let json = JSON.parse(info);
            let username = json.userName;
            res.push({tag: $(tag).parent()[0], username: username, type: 1});
            //$(tag)
        }

        //过滤右边排行榜视频
        let sel = $("ul[data-con]");

        //For each rank
        for (let i = 0; i < sel.length; i++) {
            let tag = sel[i];
            let rows = $(tag).find("li");
            //For each row
            for (let ri = 0; ri < rows.length; ri++) {
                let rowTag = rows[ri];
                //
                let m = rowTag[mark];
                //if (addMark) rowTag[mark] = mval;
                if (typeof mark !== "undefined" && m !== mval && !addMark) {
                    //continue;
                }
                //
                let aTag = $(rowTag).find("[title]")[0];
                let title = $(aTag).attr("title");
                let matches = usernameByTitle(title)
                if (matches) {
                    let username = matches[0].substring(3).trim();
                    //$(rowTag)
                    res.push({tag: rowTag, username: username, type: 2});
                }
            }
        }
        //--------------------
        sel = $("div[data-con]");
        for (let i = 0; i < sel.length; i++) {
            let tag = sel[i];
            let rows = $(tag).find("li");
            //For each row
            for (let ri = 0; ri < rows.length; ri++) {
                let rowTag = rows[ri];
                //
                let m = rowTag[mark];
                //if (addMark) rowTag[mark] = mval;
                if (typeof mark !== "undefined" && m !== mval && !addMark) {
                    //continue;
                }
                //
                let aTag = $(rowTag).find("[title]")[0];
                let title = $(aTag).attr("title");
                if (title) {
                    let matches = usernameByTitle(title)
                    if (matches) {
                        let username = matches[0].substring(3).trim();

                        res.push({tag: rowTag, username: username, type: 3});

                    }
                }
            }
        }
        //香蕉搒
        var allBananas = $("figure.fl.block-box.block-video.weblog-item").find("> figcaption > em > a");
        for (let i = 0; i < allBananas.length; i++) {
            let sel = allBananas[i];
            let upname = $(sel).attr("title");
            let tag = $(sel).parent().parent().parent()[0];
            //console.log(tag);
            res.push({tag: tag, username: upname, type: 1});
        }
        return res;
    }

    function getListSelection(mark, mval, addMark) {
        if (typeof addMark == "undefined") {
            addMark = false;
        }
        let res = [];
        let sel = $("div.weblog-item");
        //console.log("sel weblog")
        for (let i = 0; i < sel.length; i++) {
            let row = sel[i];

            //
            let m = row[mark];
            if (addMark) row[mark] = mval;
            //console.log(mark, mval, m);
            if (typeof mark !== "undefined" && m !== mval && !addMark) {
                //console.log("continue");
                continue;
            }
            //

            let aTag = $(row).find(".atc-up")[0];
            let username = $(aTag).attr("title");

            res.push({tag: row, username: username, type: 4});

        }
        return res;
    }
    let d3=unsafeWindow.d3;

    function displayList() {
        up((data)=>{
            $("#blockpanel").empty();
            for (let i=0;i<data.length;i++){
                let style1 = "font-size:11px;display:inline-block;margin:2px;height:18px;background-color:white;"
                let style2 = "width:18px; height:18px; line-height:18px; padding: 2px; background-color: rgb(255,120,120); border-width:0;";
                let b = $("<div class='blockname' style='"+style1+"'><span>"+data[i]+"</span><button style='"+style2+"'>×</button></div>")
                let str = data[i];
                b.on("click", function(){
                    removeFromList(str, ()=>{
                        displayList();
                    });

                })
                $("#blockpanel").append(b);
            }

        });
    }

    let show = false;

    function addPanel() {
        let pbutton = $("<button id='blockbutton' style=';z-index:999;position:fixed;height: 100px; width: 23px; padding: 3px; background-color:rgba(255,50,50,0.3); left: 0; top: 300px;text-orientation:upright; border-width:0;'>屏蔽列表</button>");
        let p = $("<div id='blockpanel' style='overflow-y:scroll;display:none; padding: 3px;z-index:998;position:fixed; background-color: rgba(0,120,255,0.3);left:23px; top: 300px;; height: 200px; width: 200px;'></div>");
        $(document.body).append(pbutton);
        $(document.body).append(p);
        pbutton.on("click", function () {
            show = !show;
            p.css("display", show ? "block" : "none");
            if (show) {
                displayList();
            }
        })
    }


    function addButton(pageType) {
        //console.log("page type", pageType);
        let attach = function (list) {
            //console.log("list",list)
            for (let i = 0; i < list.length; i++) {

                let tag = list[i].tag;
                $(tag).mousemove(function () {

                    //$(tag).parent().css("overflow-x", "visible")
                    //$(tag).parent().parent().css("overflow-x", "visible")
                    //$(tag).parent().parent().parent().css("overflow-x", "visible")
                    //$(tag).parent().parent().parent().parent().css("overflow-x", "visible")

                    //console.log(tag);
                    if (window.currentTag == tag) return;
                    let rect = tag.getBoundingClientRect();
                    let brect = filterButton.getBoundingClientRect();
                    let button = $(filterButton).detach();
                    //.css("left", (rect.right-brect.width)+"px")
                    //.css("top", rect.top+"px")
                    //.show();
                    $(tag)
                        .css("white-space", "nowrap")
                        .css("overflow", "visible")
                        .css("position", "relative")
                        .append(button)

                    window.currentTag = tag;
                    window.currentUser = list[i].username;
                    $(filterButton).off("click");
                    if (pageType !== "home") {
                        $(filterButton)

                            .css("right", "0")
                            .css("top", "0")
                            .css("transform", "translate(0,0)")
                        //.css("z-index", "99999")
                    }

                    $(filterButton)
                        .on("click",
                            function () {
                        console.log("current", window.currentTag, window.currentUser);
                        if (typeof window.currentTag !== "undefined" && window.currentTag !== null) {
                            addToList(window.currentUser, ()=>{displayList();});

                        }
                    })
                        .show();
                })
                $(tag).mouseleave(function () {
                    let rect = tag.getBoundingClientRect();
                    $(filterButton).detach();
                    window.currentTag = null;
                    window.currentUser = null;
                    $(filterButton).off("click").hide()

                })

            }
        }
        if (pageType == "home") {
            let list = getHomeSelection("ATTACHED", true, true);
            attach(list);
        } else {
            let list = getListSelection("ATTACHED", true, true);
            attach(list);
        }

    }

    let pannelAdded = false;

    function addUI(pageType) {
        addButton(pageType);
        if (!pannelAdded) {
            addPanel();
            pannelAdded = true;
        }
    }


    //
    console.log("match type", window.location.href)

    function usernameByTitle(title) {
        let regex = /UP:(.+)[\s\v\n]/g
        return title.match(regex);
    }

    function hideTag(tag){
        if ($(tag).css("visibility")!=="hidden"){
            $(tag).css("visibility", "hidden");
        }
    }

    function showTag(tag){
        if ($(tag).css("visibility")!=="visible"){
            $(tag).css("visibility", "visible");
        }
    }


    function FilterVideos() {
        up((upList) => {


            //过滤主页视频
            let selections = getHomeSelection("FILTER", false, false);
            for (let i = 0; i < selections.length; i++) {
                let tag = selections[i].tag;
                ;
                let username = selections[i].username;
                if (upList.indexOf(username) >= 0) {
                    //console.log("hiding", tag);
                    hideTag(tag);
                } else {
                    showTag(tag);
                }
            }
            //过滤右边排行榜视频
            let sel = $("ul[data-con]");

            //For each rank
            for (let i = 0; i < sel.length; i++) {
                let tag = sel[i];
                let rows = $(tag).find("li");
                //For each row
                for (let ri = 0; ri < rows.length; ri++) {
                    let rowTag = rows[ri];
                    let aTag = $(rowTag).find("[title]")[0];
                    let title = $(aTag).attr("title");
                    let matches = usernameByTitle(title)
                    if (matches) {
                        let username = matches[0].substring(3).trim();
                        if (upList.indexOf(username) >= 0) {
                            //找到了,隐藏本条
                            hideTag(rowTag);
                        } else {
                            showTag(rowTag);
                        }
                    }
                }
            }
        });

    }

    //过滤主页文章区
    function FilterArticles() {
        up((upList) => {
            let sel = $("div[data-con]");
            for (let i = 0; i < sel.length; i++) {
                let tag = sel[i];
                let rows = $(tag).find("li");
                //For each row
                for (let ri = 0; ri < rows.length; ri++) {
                    let rowTag = rows[ri];
                    let aTag = $(rowTag).find("[title]")[0];
                    let title = $(aTag).attr("title");
                    if (title) {
                        let matches = usernameByTitle(title);
                        if (matches) {
                            let username = matches[0].substring(3).trim();
                            if (upList.indexOf(username) >= 0) {
                                //找到了,隐藏本条
                                // console.log("过滤文脏区 rank=", i, "row=",ri,"username=",username);
                                hideTag(rowTag);
                            } else {
                                showTag(rowTag);
                            }
                        }
                    }
                }
            }
        });

    }

    //过滤文章列表页
    function FilterListPage() {
        up(((upList) => {
            let sel = $("div.weblog-item");
            //console.log("uplist = ", upList, sel);
            for (let i = 0; i < sel.length; i++) {
                let row = sel[i];
                //if (typeof row.visited=="undefined"){ row.visited = true; }else{continue;}
                let aTag = $(row).find(".atc-up")[0];
                let username = $(aTag).attr("title");

                //console.log("username",username, window.currentUser);
                if (upList.indexOf(username) >= 0) {
                    //console.log("过滤文脏区",username);
                    //屏蔽
                    hideTag(row);
                } else {
                    showTag(row);
                }
            }
        }))

    }

    function HandleHomePage() {
        //屏蔽首页UP,包括视频和右边的文章区
        FilterVideos();
        FilterArticles();
        addUI("home");
        setTimeout(HandleHomePage, 800);
    }

    function HandleListPage() {
        //屏蔽文章页,
        FilterListPage();
        addUI("list");
        setTimeout(HandleListPage, 500);
    }

    function HandleArticlePage() {
        //对于单独的文章页或者视频,添加一个按钮屏蔽当前UP主

        console.log(window.location.href.indexOf("www.acfun.cn/a/ac"), window.location.href.indexOf("www.acfun.cn/v/ac"))

        let button = $("<div style='z-index: 9999;position:fixed; left:0px; top: 200px;  width:30px; padding: 5px; background-color: rgba(255,50,50,0.3)'>屏蔽UP</div>")
        console.log("DEBUG Attach Button S.");
        $(document.body).append(button);
        console.log("DEBUG Attach Button S Done.");
        button.on("click", function () {
            let upname = "";
            if (window.location.href.indexOf("www.acfun.cn/v/ac") >= 0) {
                upname = $("a.name-wrap").html();
                console.log("upname", upname);
            } else if (window.location.href.indexOf("www.acfun.cn/a/ac") >= 0) {
                upname = $("a.upname").html();
                console.log("upname", upname);
            }
            addToList(upname);

        })
    }

    function initLoad() {

        if (typeof $ == "undefined") {
            console.log("Loading..", typeof unsafeWindow.$);
            $=unsafeWindow.$;
            setTimeout(initLoad, 1000);
            return;
        }

        initButton();

        function isArtileListPage(){
            let t = ["list63","list110","list73","list164","list184","list74","list75"];
            let url = window.location.href;
            for (let i=0;i<t.length; i++){
                if (url.indexOf(t[i])>=0) return true;
            }
            return false;
        }
        console.log("DEBUG body Loaded");
        if (window.location.href === "http://www.acfun.cn/" || window.location.href === "https://www.acfun.cn/") {
            HandleHomePage();
        }
        if (window.location.href.indexOf("www.acfun.cn/v/list") >= 0) {
            //可能是文章列表

            if (isArtileListPage()){
                console.log("DEBUG handle list page");
                HandleListPage();
            }else{
                HandleHomePage();
            }
        }
        if (window.location.href.indexOf("www.acfun.cn/v/ac") >= 0 || window.location.href.indexOf("www.acfun.cn/a/ac") >= 0) {
            console.log("DEBUG handle single Page");
            HandleArticlePage();
        }

    }
    initLoad();
};

core();