Greasy Fork is available in English.

Acfun过滤UP计划

帮助你屏蔽不想看的UP主

Stan na 30-05-2019. Zobacz najnowsza wersja.

// ==UserScript==
// @name         Acfun过滤UP计划
// @namespace    http://tampermonkey.net/
// @version      0.544
// @description  帮助你屏蔽不想看的UP主
// @author       人文情怀
// @match        http://www.acfun.cn/a/ac*
// @match        http://www.acfun.cn/v/list*
// @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*
// @grant        GM_getValue
// @grant        GM_setValue
// @reference    https://greasyfork.org/en/scripts/377561-block-acfun-up
// ==/UserScript==

// 在0.40版本中受到 https://greasyfork.org/en/scripts/377561-block-acfun-up 的启发添加的借用代码。可以更准确的等待页面载入。
// https://gist.github.com/raw/2625891/waitForKeyElements.js not suppored by Greasy Fork, copied here
/*--- waitForKeyElements():  A utility function, for Greasemonkey scripts,
    that detects and handles AJAXed content.

    Usage example:

        waitForKeyElements (
            "div.comments"
            , commentCallbackFunction
        );

        //--- Page-specific function to do what we want when the node is found.
        function commentCallbackFunction (jNode) {
            jNode.text ("This comment changed by waitForKeyElements().");
        }

    IMPORTANT: This function requires your script to have loaded jQuery.
*/
function waitForKeyElements (
selectorTxt,    /* Required: The jQuery selector string that
                        specifies the desired element(s).
                    */
 actionFunction, /* Required: The code to run when elements are
                        found. It is passed a jNode to the matched
                        element.
                    */
 bWaitOnce,      /* Optional: If false, will continue to scan for
                        new elements even after the first match is
                        found.
                    */
 iframeSelector  /* Optional: If set, identifies the iframe to
                        search.
                    */
) {
    var targetNodes, btargetsFound;

    if (typeof iframeSelector == "undefined")
        targetNodes     = $(selectorTxt);
    else
        targetNodes     = $(iframeSelector).contents ()
            .find (selectorTxt);

    if (targetNodes  &&  targetNodes.length > 0) {
        btargetsFound   = true;
        /*--- Found target node(s).  Go through each and act if they
            are new.
        */
        targetNodes.each ( function () {
            var jThis        = $(this);
            var alreadyFound = jThis.data ('alreadyFound')  ||  false;

            if (!alreadyFound) {
                //--- Call the payload function.
                var cancelFound     = actionFunction (jThis);
                if (cancelFound)
                    btargetsFound   = false;
                else
                    jThis.data ('alreadyFound', true);
            }
        } );
    }
    else {
        btargetsFound   = false;
    }

    //--- Get the timer-control variable for this selector.
    var controlObj      = waitForKeyElements.controlObj  ||  {};
    var controlKey      = selectorTxt.replace (/[^\w]/g, "_");
    var timeControl     = controlObj [controlKey];

    //--- Now set or clear the timer as appropriate.
    if (btargetsFound  &&  bWaitOnce  &&  timeControl) {
        //--- The only condition where we need to clear the timer.
        clearInterval (timeControl);
        delete controlObj [controlKey]
    }
    else {
        //--- Set a timer, if needed.
        if ( ! timeControl) {
            timeControl = setInterval ( function () {
                waitForKeyElements (    selectorTxt,
                                    actionFunction,
                                    bWaitOnce,
                                    iframeSelector
                                   );
            },
                                       300
                                      );
            controlObj [controlKey] = timeControl;
        }
    }
    waitForKeyElements.controlObj   = controlObj;
}

(function() {
    'use strict';
    function cleanList(){

        GM_setValue("ACFUN_BLOCK_LIST", []);
    }


    function helper()
    {
        var head= document.getElementsByTagName('head')[0];
        var script= document.createElement('script');
        script.type= 'text/javascript';
        script.src= 'https://d3js.org/d3.v3.min.js';
        head.appendChild(script);
    };
    helper();
    let up=function(){
        return GM_getValue("ACFUN_BLOCK_LIST",[]);
    };

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

        old.push(id);
        GM_setValue("ACFUN_BLOCK_LIST", old);
        $.info.show("已将UP主["+id+"]加入屏蔽列表")
        console.trace("a");
    }

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

    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(-100%,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;
    }


    function displayList(){
        let data = up();
        d3.select("#blockpanel")
            .selectAll("div.blockname")
            .data(up)
            .exit()
            .remove();

        let entersel=        d3.select("#blockpanel")
        .style("font-size","11px")
        .selectAll("div.blockname")
        .data(up)
        .enter()
        let nameblock = entersel.append("div")
        .attr("class","blockname")
        .style("display","inline-block")
        .style("margin", "2px")
        .style("height","18px")
        .style("background-color","white");
        nameblock.append("span")
            .text(function(d){return d})
        nameblock.append("button")
            .text("×")
            .style("width","18px")
            .style("height","18px")
            .style("line-height","18px")
            .style("padding","2px")
            .style("background-color","rgb(255,120,120)")
            .style("border-width","0px")
            .on("click", function(d){
            console.log("remove",d);
            removeFromList(d);
            displayList();
        })
    }
    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();
            }
        })
    }
    let a = 0;
    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","visible")
                    $(tag).parent().parent().css("overflow","visible")
                    $(tag).parent().parent().parent().css("overflow","visible")
                    $(tag).parent().parent().parent().parent().css("overflow","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(100%,0)")
                    }

                    $(filterButton)
                        .on("click",
                            function(){
                        console.log("current", window.currentTag, window.currentUser);
                        if (typeof window.currentTag!=="undefined" && window.currentTag!==null){
                            addToList(window.currentUser);
                            console.log("a=",a);
                            a++;
                            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 FilterVideos(){
        let upList=up();

        //过滤主页视频
        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){
                $(tag).css("visibility","hidden");
            }else{
                $(tag).css("visibility","visible");
            }
        }
        //过滤右边排行榜视频
        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){
                        //找到了,隐藏本条
                        $(rowTag).css("visibility","hidden");
                    }else{
                        $(rowTag).css("visibility","visible");
                    }
                }
            }
        }
    }

    //过滤主页文章区
    function FilterArticles(){
        let upList=up();
        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);
                            $(rowTag).css("visibility","hidden");
                        }else{
                            $(rowTag).css("visibility","visible");
                        }
                    }
                }
            }
        }
    }

    //过滤文章列表页
    function FilterListPage(){
        let upList=up();
        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);
                //屏蔽
                $(row).css("display","none");
                //$(row).removeClass("wblog-item");
                //或者删除
                //$(row).remove();
            }else{
                $(row).css("display","block");
            }
        }
    }
    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..");
            setTimeout(initLoad, 1000);
            return;
        }

        initButton();
        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){
            console.log("DEBUG handle list page");
            HandleListPage();
        }
        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();
        }

    }

    waitForKeyElements ("body", initLoad);


})();