B站直播自动抢辣条二代by十六夜

我的我的 都是我的

As of 2019-08-22. See the latest version.

// ==UserScript==
// @name         B站直播自动抢辣条二代by十六夜
// @namespace    http://tampermonkey.net/
// @version      2.3
// @description  我的我的 都是我的
// @author       逆回十六夜
// @match        https://live.bilibili.com/*
// @grant        none
// ==/UserScript==
let time_flash = 10000;// 跳转到下个直播间的时间单位毫秒


$(document).ready(function () {//页面加载完毕执行
    StartPlunder();
});

if (getUrlParam('close')) {
    window.close();//关闭弹窗测试的窗口
}

function StartPlunder() {
    'use strict';
    let index, nowIndex;
    let LIVE_PLAYER_STATUS = window.localStorage["LIVE_PLAYER_STATUS"];

    let href = location.href;
    let id = /\/\d+/.exec(href);
    if (!id) {
        return
    }
    if (id == '/6498960') {
        if (LIVE_PLAYER_STATUS.indexOf("flash") >= 0) {
            window.localStorage["LIVE_PLAYER_STATUS"] = window.localStorage["LIVE_PLAYER_STATUS"].replace("flash", 'html5');
            window.location.reload();
            return
        }

        if (!getUrlParam('win') && !getUrlParam('close')) {
            window.open('https://live.bilibili.com/6498960?close=1', '_blank');// 弹窗测试
        }

        setInterval(function () {//打开链接时钟
            let link = $(".msg-content .link");
            let numberList = [];
            let linkList = [];
            if (link.length > 0) {
                link.each(function () {
                    let e_link = $(this);
                    let link_href = e_link.attr('href');
                    if (link_href) {
                        link_href = link_href.split('?')[0];
                        let number, delay;
                        let myDate = new Date();
                        if (link_href.match(/\/\d+/) == null || e_link.text().indexOf('第一名') >= 0) {
                            e_link.text('这个不是礼物~' + myDate.toLocaleString());
                        } else {//是礼物
                            number = /\/\d+/.exec(link_href).toString();//取房间号
                            delay = 55;
                            if (e_link.text().indexOf('小电视飞船') >= 0) {
                                delay = 115;//小电视延迟
                            }
                            number = number + ',' + delay;//直播间号和延迟
                            e_link.attr('number', number);
                            if (!isInArray(numberList, number)) {
                                numberList.push(number);
                                linkList.push(e_link);//加入需要打开的直播间列表
                                e_link.text('等待抽奖...');
                            } else {
                                e_link.text('同直播间同延迟礼物省略');
                            }
                        }
                        e_link.removeAttr('href');
                        e_link.removeAttr('class');
                    }
                });
            }
            //检测任意门
            let a = $('.bilibili-live-player-video-operable-container div div div div a');
            let text = a.text();
            if (text.indexOf('任意门') >= 0) {
                let number = /\/\d+/.exec(a.attr('href')).toString();//取房间号
                let div = $("<div>");
                let aa = $("<a>");
                aa.text('等待...');
                aa.attr('number', number + ',70');
                div.text('任意门');
                div.append(aa);
                $('.chat-history-list').append(div);
                a.remove();
                linkList.push(aa);
            }

            if (linkList.length > 0) {
                for (let l of linkList) {//延迟打开直播间
                    let count = l.attr('number').split(',')[1];
                    let index = setInterval(function () {
                        l.text('等待抽奖...' + count.toString() + 'S');
                        count--;
                        if (count <= 0) {
                            clearTimeout(index);//清除定时器
                            window.open('https://live.bilibili.com' + l.attr('number').split(',')[0] + '?mode=Izayoi', '_blank');
                            let myDate = new Date();
                            l.text('辣条已抢~' + myDate.toLocaleString());
                        }
                    }, 1000);
                }
                console.log(linkList, numberList, 'list');
            }
        }, 10000);
        setInterval(function () {//重置直播间
            let href = location.href;
            if (href.indexOf('win') < 0) {
                if (href.indexOf('?') >= 0) {
                    href += '&win=off';
                } else {
                    href += '?win=off';
                }
            }
            location.href = href;
        }, 7200000);
    } else {
        if (LIVE_PLAYER_STATUS.indexOf("html5") >= 0) {
            window.localStorage["LIVE_PLAYER_STATUS"] = window.localStorage["LIVE_PLAYER_STATUS"].replace("html5", 'flash');
            window.location.reload();
            return
        }
        if (getUrlParam("mode")) {//弹出网页抽奖模式
            let count = 0;
            setInterval(function () {//点击抽奖时钟
                let old_btn = $(".main");
                let btn = $(".draw-full-cntr .function-bar");
                if (old_btn.length > 0 || btn.length > 0) {
                    count++;
                }
                old_btn.click();//点击抽奖
                btn.click();//点击抽奖
                console.log(old_btn.length, btn.length, count)
                if (old_btn.length <= 0 && btn.length <= 0 && count > 0) {//已经抽过礼物并且没有了
                    setTimeout(function () {
                        window.close();//礼物抢完关闭直播间
                    }, 800);
                }
            }, 100);
            setTimeout(function () {
                let old_btn = $(".main");
                let btn = $(".draw-full-cntr .function-bar");
                if (old_btn.length <= 0 && btn.length <= 0) {
                    window.close();//10s后如果没有礼物则关闭直播间
                }
            }, 10000);
            setTimeout(function () {
                window.close();//20s超时关闭直播间
            }, 20000);
        } else {
            mode_old();
        }
    }

    function mode_old() {
        try {
            index = getUrlParam("index");
            nowIndex = parseInt(index) + 1;
            if (nowIndex == 12) {
                nowIndex = 0;
            }
            if (isNaN(nowIndex)) nowIndex = 0;
        } catch (error) {
            nowIndex = 0;
        }
        setInterval(function () {
            $(".main").click();//点击抽奖
            $(".draw-full-cntr .function-bar").click();//点击抽奖
        }, 100);
        setInterval(function () {
            goTop(nowIndex);//跳转到下一个直播间
        }, time_flash);
    }

    function goTop(index) {
        $.get("https://api.live.bilibili.com/rankdb/v1/Rank2018/getTop?type=master_realtime_hour&type_id=areaid_realtime_hour", function (data) {
            let list = data.data.list;// [{id: ,link:}]
            let link = list[index].link;
            if (!link) {
                link = '/55356';
            }
            let href = location.href;
            //
            if (href.match(/\/\d+/) != null) {
                href = href.replace(/\/\d+/, link);
            } else {
                href = 'https://live.bilibili.com' + link;
            }
            //
            if (href.indexOf('index') >= 0) {
                href = href.replace(/index=\d+/, 'index=' + nowIndex);
            } else {
                if (href.indexOf('?') >= 0) {
                    href += '&index=' + nowIndex;
                } else {
                    href += '?index=' + nowIndex;
                }
            }
            //
            location.href = href;
        });
    }
}

function isInArray(arr, value) {
    for (let i of arr) {
        if (i == value) {
            return true;
        }
    }
    return false;
}

function getUrlParam(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}