Bilibili 动态页显示当前所有直播

显示当前所有直播

当前为 2021-03-07 提交的版本,查看 最新版本

// ==UserScript==
// @name        Bilibili 动态页显示当前所有直播
// @description 显示当前所有直播
// @version     1.1
// @author       Myitian
// @namespace   myitian.bili.tPage-showMoreLives
// @grant       none
// @include     https://t.bilibili.com/*
// @include     http://t.bilibili.com/*
// ==/UserScript==

var getJSON = function (url) {
    return new Promise(function (resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.open('get', url, true);
        xhr.withCredentials = true;
        xhr.responseType = 'json';
        xhr.onload = function () {
            var status = xhr.status;
            if (status == 200) {
                resolve(xhr.response);
            } else {
                reject(status);
            }
        };
        xhr.send();
    });
}; //GET请求

function moreBtn() {
    var base = document.createElement('div');
    base.id = 'sml-base';

    var mask = document.createElement('div');
    mask.id = 'sml-mask';
    mask.onclick = function showMoreLivesPageX() {
        exitBtn();
    }
    mask.style = 'display:block;position:fixed;top:0;left:0;width:100%;height:100%;background:#000;filter:alpha(opacity=65);-ms-filter:"alpha(opacity = 65)";opacity:.65;z-index:10000;';

    var mainbox = document.createElement('div');
    mainbox.style = 'display:block;position:fixed;top:50%;left:50%;width:1000px;height:700px;margin-left:-500px;margin-top:-350px;border-radius:4px;background-color:#fff;z-index:10001;'

    var title = document.createElement('div');
    title.style = 'position:relative;padding:0 20px;height:50px;line-height:50px;font-size:16px;color:#222;border-bottom:1px solid #e5e9ef;'

    var titlesp1 = document.createElement('span');
    titlesp1.innerText = '正在直播';

    var exit = document.createElement('a');
    exit.className = 'close'
    exit.style = 'position:absolute;right:20px;line-height:50px;width:12px;height:50px;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANBAMAAACAxflPAAAAIVBMVEUAAACao6qZpa2bpKqapaqZo6qboqqapKuZoqqao6qZoqqZADmtAAAACnRSTlMA6kFUMM2Eat+9b+FOdgAAAENJREFUCNdjgAFOBSDhysDAJMTAwCIMZCsqMDgaMIAEQFyQQKEBmGZfBtHhmAjUAlLMJATmGgC1gLgQM1hBijXgxgMACcIFDbl0pdMAAAAASUVORK5CYII=) no-repeat 50%;cursor:pointer;';
    exit.onclick = function showMoreLivesPageX() {
        exitBtn();
    }

    var titlesp2 = document.createElement('span');
    titlesp2.style = 'color:#99a2aa;letter-spacing:0;font-size:14px;';
    titlesp2.innerText = '(0)';

    getJSON('https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/w_live_users?size=-1').then(function (jsondata) {


        titlesp2.innerText = '(' + jsondata.data.count + ')';

        if (jsondata.data.count > 0) {

            var livepanel = document.createElement('table');
            livepanel.style = 'display:block;width:1000px;overflow:auto;';

            var lines = (jsondata.data.count - jsondata.data.count % 4) / 4 + 1;
            var count = 0;
            for (var i = 0; i < lines; i++) {
                if (count == jsondata.data.count) {
                    break;
                } else {
                    var tr = document.createElement('tr');
                    tr.style = 'display:block;width:976px;';

                    for (var j = 0; j < 4; j++) {
                        if (count == jsondata.data.count) {
                            break;
                        } else {
                            var td = document.createElement('td');
                            td.style = 'display:inline-flex;flex-grow:0;flex-shrink:0;position:relative;margin-bottom:20px;margin-top:10px;margin-left:16px;height:44px;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;width:224px;';

                            var a1 = document.createElement('a');
                            a1.style = 'display:block;width:38px;height:38px;border-radius:22px;position:relative;background-color:#fff;margin:1px;margin-right:11px;-ms-flex-negative:0;flex-shrink:0;background-size:cover;background-color:#ddd;-webkit-box-shadow:0 0 0 1px #f25d8e;box-shadow:0 0 0 1px #f25d8e;border:1px solid #fff;background-image:url("' + jsondata.data.items[count].face + '@50w_50h.png");';
                            a1.href = jsondata.data.items[count].link;
                            a1.target = '_blank';

                            var a2 = document.createElement('a');
                            a2.href = jsondata.data.items[count].link;
                            a2.target = '_blank';
                            a2.style = 'display:block;text-overflow:ellipsis;overflow:hidden;word-break:keep-all;max-width:176px;padding-right:16px;letter-spacing:0;';

                            var upname = document.createElement('div');
                            upname.innerText = jsondata.data.items[count].uname;
                            upname.style = 'display:inline-block;margin-bottom:4px;font-size:14px;color:#222;line-height:20px;word-break:break-all;word-break:break-word;text-overflow:ellipsis;max-height:20px;display:-webkit-box;-webkit-line-clamp:1;-webkit-box-orient:vertical;overflow:hidden;';

                            var livename = document.createElement('div');
                            livename.innerText = jsondata.data.items[count].title;
                            livename.style = 'display:inline-block;font-size:12px;color:#6d757a;line-height:16px;word-break:break-all;word-break:break-word;text-overflow:ellipsis;max-height:32px;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden;';

                            a2.appendChild(upname);
                            a2.appendChild(livename);

                            td.appendChild(a1);
                            td.appendChild(a2);

                            tr.appendChild(td);
                            count++;
                        }
                    }
                    livepanel.appendChild(tr);
                }
            }

            mainbox.appendChild(livepanel);

        } else {
            var err = document.createElement('p');
            err.innerText = "当前无直播";
            err.style = "text-align:center;";
            mainbox.appendChild(err);
        }
    }, function (status) { //error detection....
        var err = document.createElement('p');
        err.innerText = "直播数据获取失败!";
        err.style = "text-align:center;";
        mainbox.appendChild(err);
    });

    base.appendChild(mask);
    base.appendChild(mainbox);
    title.appendChild(titlesp1);
    title.appendChild(titlesp2);
    title.appendChild(exit);
    mainbox.appendChild(title);
    document.getElementsByTagName('body')[0].appendChild(base);
}

function exitBtn() {
    document.getElementsByTagName('body')[0].removeChild(document.getElementById('sml-base'));
}

window.onclick = function showMoreLivesPage1() {
    var moreBtnEle = document.getElementsByClassName('live-panel')[0].childNodes[1].childNodes[0];

    moreBtnEle.setAttribute('href', 'javascript:;');
    moreBtnEle.setAttribute('target', '');
    moreBtnEle.onclick = function showMoreLivesPage2() {
        moreBtn();
    };
};

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址