斗鱼小工具

斗鱼批量取关

目前为 2019-12-08 提交的版本。查看 最新版本

// ==UserScript==
// @name			斗鱼小工具
// @namespace		http://tampermonkey.net/
// @version			0.0.1
// @icon			http://www.douyutv.com/favicon.ico
// @description		斗鱼批量取关
// @author			H2P
// @compatible		chrome
// @require			https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js
// @match			*://*.douyu.com/0*
// @match			*://*.douyu.com/1*
// @match			*://*.douyu.com/2*
// @match			*://*.douyu.com/3*
// @match			*://*.douyu.com/4*
// @match			*://*.douyu.com/5*
// @match			*://*.douyu.com/6*
// @match			*://*.douyu.com/7*
// @match			*://*.douyu.com/8*
// @match			*://*.douyu.com/9*
// @match			*://*.douyu.com/topic/*
// @match           https://www.douyu.com/directory/myFollow
// @note			2019.12.08-V0.0.01 
// ==/UserScript==

(()=>{
	'use strict';

	// 解决 jQuery $ 符号冲突
    window.$h2p_j = jQuery.noConflict();
    window.h2pScript_Func = {}

    let OK_Btn_CancelFollow = false;
    let INVL_AddBtnCancelFollow = undefined;

    if (window.location.href.indexOf('https://www.douyu.com/directory/myFollow') > -1){
        function Set_INVL_AddBtnCancelFollow(INVLObj){
            INVLObj = setInterval(()=>{
                if ($h2p_j('div#filter-tab-expandable-wrapper').length > 0){
                    let btn_cancelFollow = $h2p_j('<a id="a-cancelFollow" class="layout-Module-label"><strong>取消关注</strong></a>');
                    $h2p_j('div#filter-tab-expandable-wrapper').append(btn_cancelFollow);
                    window.clearInterval(INVLObj);
                    setTimeout(()=>{
                        $h2p_j(document).on('click','a#a-cancelFollow', getAnchorSelectedIds);
                    }, 100);
                }
            }, 100);
        }
    
        let anchorsSelected = [];
        function getAnchorSelectedIds(){
            let anchorSelected = $h2p_j('li.layout-Cover-item div.DyLiveCover-selectArea.is-active').toArray();
            anchorSelected.forEach(anchor => {
                let anchorHref = $h2p_j(anchor).next().attr('href');
                if (!anchorHref || anchorHref.length == 0){
                    anchorHref = $h2p_j(anchor).parent().attr('href')
                }
                let anchorId = anchorHref.split('/').pop();
                anchorsSelected.push(anchorId);
            });
            console.log(anchorsSelected);
            Set_TIME_CancelFollow();
        }
        function Set_TIME_CancelFollow(){
            setTimeout(()=>{
                if (anchorsSelected && anchorsSelected.length > 0){
                    let anchorId = anchorsSelected.shift();
                    cancelFollow(anchorId);
                    console.log('取消关注主播:'+anchorId);
                    Set_TIME_CancelFollow();
                }
            }, 500);
        }
        function cancelFollow(anchorId){
            fetch('https://www.douyu.com/room/follow/cancel_confuse/'+anchorId, {
                method: 'POST'
            }).then(response => response.json())
            .then((res)=>{
                if (res && 'error' in res && res['error'] === 0){
                    console.log('成功取消关注主播:'+anchorId);
                    let parentEle = $h2p_j('a[href="/'+anchorId+'"]').parent();
                    // 从主播 id 找到主播信息所在 ele 的根节点
                    while ($h2p_j(parentEle).attr('class').indexOf('layout-Cover-item') === -1){
                        parentEle = $h2p_j(parentEle).parent();
                        if ($h2p_j(parentEle).prop("tagName").toLowerCase() === 'body'){
                            break;
                        }
                    }
                    if ($h2p_j(parentEle).attr('class').indexOf('layout-Cover-item') > -1 && $h2p_j(parentEle).prop("tagName").toLowerCase() != 'body'){
                        $h2p_j(parentEle).remove();
                    }
                }
            });
        }
    
        function sleep(sec){
            let start = new Date().getTime();
            sec = Math.min(sec, 600);
            while ((new Date().getTime() - start) / 1000 < sec){}
        }
    
        /**
         * 方法调用
         */
        Set_INVL_AddBtnCancelFollow(INVL_AddBtnCancelFollow);
    }

    window.h2pScript_Func.cancelFollow = cancelFollow;
    window.h2pScript_Func.sleep = sleep;
})();