B站弹幕自动切大会员专属颜色

自动切大会员弹幕颜色!

// ==UserScript==
// @name         B站弹幕自动切大会员专属颜色
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  自动切大会员弹幕颜色!
// @author       ProDark
// @match       https://www.bilibili.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...

     function pageAcive(){
        //后台打开的页面没有播放器载入,主要功能失效。这个用于判断页面处于后台还是前台。
        //console.log(document.hidden);
        if(document.hidden){
            // 如果当前页面在后台打开
            return false;
        }
        else
        {
            return true;
        }
    };

    function MouseEnter(Target){
        var event = new MouseEvent("mouseenter", {
            bubbles: false,
            cancelable: true,
            view: window,
        });
        Target.dispatchEvent(event);
        //console.log("mouseenter");
    };
    function MouseLeave(Target){
        var event = new MouseEvent("mouseleave", {
            bubbles: false,
            cancelable: true,
            view: window,
        });
        Target.dispatchEvent(event)
        //console.log("mouseleave");
    };


    function ClickButton(Target){
        var e = document.createEvent("MouseEvents");
        e.initMouseEvent("click", true, false);
        Target.dispatchEvent(e);
    };

    //------------------------------------------------------------------------------------------------------------------------
    // 隐藏窗口选项窗口,没能及时隐藏窗口,只能连父节点一起隐藏了。
    function HideNode(Target,flag,mode){
        if(Target == null){return;}

        if(flag == true){
            if(mode == "display"){
                Target.style.display = "none";
            }else if ( mode == "visibility"){
                Target.style.visibility = "hidden";
             }else if ( mode == "width"){
                Target.style.transform = "scale(0,0)";
            }else{
                //Target.hidden = true;
                Target.style.opacity = 0;
            }
        }else{
            if(mode == "display"){
                Target.style.display = "";
           }else if ( mode == "visibility"){
               Target.style.visibility = "visible";
           }else if ( mode == "width"){
                Target.style.transform = "scale(1,1)";
           }else{
               //Target.hidden = false;
               Target.style.opacity = 1;
           }

        }
    };

    //------------------------------------------------------------------------------------------------------------------------
    // 颜色选择按钮点击。不知道为什么,父节点和所有子节点都点一次,才成功。
    function Select_Colourful(Target){
        ClickButton(Target)
        var child_list = Target.childNodes
        for(var i=0;i<child_list.length;i++){
            //child_list[i].setAttribute("class", "active")
            ClickButton(child_list[i]);
        }
        return true
    }


    //------------------------------------------------------------------------------------------------------------------------
    // 找到字体颜色切换标签
    var color_tag = null;// 节省计算量,在这里放一个储存位置
    function get_color_tag_button(){
        if (color_tag != null ) {
            return color_tag
        }
        var divs = document.getElementsByTagName("div");
        for(var i=0;i<divs.length;i++){
            if( divs[i].getAttribute("class") == "bpx-player-video-btn-dm")
            {
                color_tag = divs[i]
                return divs[i];
            }
        }
        return null;
    };
    //------------------------------------------------------------------------------------------------------------------------
    //得到要点击的按钮
    function get_colourful_button(){
        var divs = document.getElementsByTagName("ul");
        for(var i=0;i<divs.length;i++){
            if( divs[i].getAttribute("class") == "vip-color-picker-options")
            {
                return divs[i];
            }
        }
        return null;
    };

    //------------------------------------------------------------------------------------------------------------------------
    //监听 dvi按钮 class = bpx-player-video-btn-dm
    // 内部有变动的时候隐藏 颜色选择窗口
    var the_observer = null;
    var the_observer_target = null;
    function AddColourContainerWatcher(){

        const targetNode = get_color_tag_button()
        if( targetNode == null || the_observer!= null ){return}

        // 观察器的配置(需要观察什么变动)
        const config = { attributes: false, childList: true, subtree: true };

        // 当观察到变动时执行的回调函数
        const callback = function(mutationsList, observer) {
            for(let mutation of mutationsList) {

                the_observer_target = mutation.target;
                HideNode(the_observer_target,true);
            }

        };
        // 创建一个观察器实例并传入回调函数
        const observer = new MutationObserver(callback);
        // 以上述配置开始观察目标节点
        observer.observe(targetNode, config);
        the_observer = observer;
        // 之后,可停止观察
        //observer.disconnect();
    };

    function RemoveColourContainerWatcher(){
        MouseLeave(get_color_tag_button());
        if( the_observer != null || the_observer_target != null){
            the_observer.disconnect();
            HideNode(the_observer_target,false);
        }
    };

    //------------------------------------------------------------------------------------------------------------------------
    function get_background_node(){
        var divs = document.getElementsByTagName("div");
        for(var i=0;i<divs.length;i++){
            var temp_class = divs[i].getAttribute("class") ;
            if( temp_class == "bpx-player-mode-selection-container active" || temp_class == "bpx-player-mode-selection-container")
            {
                //divs[i].blur();
                return divs[i];
            }
        }
        return null;
    };
    //------------------------------------------------------------------------------------------------------------------------
    var try_num = 20;/// 某些视频没法选会员弹幕颜色
    var ColourContainerDisplay = false;
    function main_task(){
        AddColourContainerWatcher()// 添加事件监控器。监控DOM变化
        var color_tag_button = get_color_tag_button()
        if (color_tag_button == null) {
            if(try_num > 0){
                try_num = try_num - 1;
                console.log("Error : 找不到颜色tag按钮",try_num);
                return false;
            }else{
                console.log("Error : 找不到颜色tag按钮,任务终止");
                return true;
            }
        };// 寻找 颜色切换按钮,没有找到则返回
        //console.log("找到颜色切换的tag");
        var background = get_background_node()

        HideNode(color_tag_button,true);// 隐藏按钮。
        HideNode(background,true,"width");// 隐藏背景。

        if (ColourContainerDisplay == false){
            MouseEnter(color_tag_button);
            ColourContainerDisplay = true;
        }

        var final_button = get_colourful_button()
        if (final_button == null){
            try_num = try_num - 1;
            if( try_num <=0 ){
               console.log("找不到彩色按钮,任务终止");
               MouseLeave(color_tag_button);
               HideNode(color_tag_button,false);//显示tag

                setTimeout(()=>{
                    HideNode(background,false,"width");// 显示背景。
                },1000)

               return true;
            }
            console.log("找不到彩色按钮",try_num);
            return false;
        }
        console.log("找到彩色按钮")
        //ClickButton(final_button);
        if (Select_Colourful(final_button) == true || try_num <= 0){
            MouseLeave(color_tag_button);
            console.log("自动切会员弹幕颜色任务结束");
            RemoveColourContainerWatcher();
            HideNode(color_tag_button,false);//显示tag

            setTimeout(()=>{
                HideNode(background,false,"width");// 显示背景。
            },1000)

            return true;
        }else{
            console.log("点击彩色按钮失败,正在重试",try_num);
            try_num = try_num -1;
        }
        return false;
    };


    window.addEventListener("load", task_start_fn, false);
    function task_start_fn(){
        //alert("task start");
        var tempTask_onload = setInterval(function(){
                if(pageAcive() && document.readyState == "complete" && main_task() == true){
                    clearInterval(tempTask_onload);
                   // MainTimeOut_2()
                    //alert("task66")
                }
            },1000);
    };


})();