MyBilibili

b站主页面视频推荐挂载脚本

目前為 2022-05-30 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         MyBilibili
// @namespace    http://tampermonkey.net/
// @version      0.3.0
// @description  b站主页面视频推荐挂载脚本
// @author       N-cat
// @match        https://api.bilibili.com/x/web-interface/dynamic/region?ps=*&rid=*
// @match        *://www.bilibili.com/
// @match        *://www.bilibili.com/video/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        GM_xmlhttpRequest
// @connect      *
// ==/UserScript==

(function() {
    'use strict';
    // api及参数说明来自 https://zhuanlan.zhihu.com/p/210779665
    const style = `<style>
    .add-main{
        margin:auto;
        width:1100px;
    }
    .add-div{
        width: 206px;
        height: 206px;
        display:inline-block;
        padding:7px 7px 7px 7px;
        box-sizing:content-box;
        position:relative;
    }
    .add-a{
        color: #212121;
        margin: 10px 0 8px;
        height: 40px;
        text-decoration: none;
        width: 206px;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        font-size: 14px;
        line-height: 20px;
        overflow: hidden;
        text-overflow: ellipsis;
        transition: color .3s;
    }
    .add-a:hover{
        color: #00a1d6;
        transition: color .3s;
    }
    .add-img{
        width: 206px;
        height: 116px;
        border-radius: 2px;
    }
    .add-author{
        color:#999;
        text-decoration: none;
        transition: color .3s;
        font-size: 12px;
    }
    .add-author:hover{
        color: #00a1d6;
        transition: color .3s;
    }
    .re{
        position:fixed;
        right:80px;
        bottom:80px;
        background-color:#00a1d6;
        color:white;
        height:50px;
        width:50px;
        border-radius: 10px;
        font-size:15px;
        border:solid 3px #FA5A57;
        cursor:pointer;
        outline: none;
    }
    .top{
        position:fixed;
        right:80px;
        bottom:150px;
        background-color:#00a1d6;
        color:white;
        height:50px;
        width:50px;
        border-radius: 10px;
        font-size:15px;
        border:solid 3px #FA5A57;
        cursor:pointer;
        outline: none;
    }
    .add-bds{
        position: absolute;
        height: 22px;
        width: 206px;
        top: 101px;
        color: white;
        font-size: 14px;
        background-color: rgba(0,0,0,.6);
        // text-align: center;
        border-radius: 2px;
    }
    </style>`;
    var ps = 50; // 页数
    var rid = 1; // 类型(1:综合)
    // var cookie = document.cookie;
    // console.log(cookie)
    var data
    // api删除原数据
    var pre = document.getElementsByTagName('pre')
    if(pre.length != 0){
        pre[0].remove();
    }
    // 主站隐藏原数据(旧)
    var olddiv = document.getElementsByClassName('b-wrap');
    for(let i in olddiv){
        if(i > 1){
            olddiv[i].style.display = "none";
        }
    }
    olddiv = document.getElementsByClassName('international-footer');
    if(olddiv.length != 0){
        olddiv[0].style.display = "none";
    }
    // 主站隐藏原数据(新)
    var newdiv = document.getElementsByClassName('bili-layout');
    if(newdiv.length != 0){
        newdiv[0].style.display = "none";
    }
    // 请求推荐api
    function getvideo(){
        GM_xmlhttpRequest({
            url:"https://api.bilibili.com/x/web-interface/dynamic/region?ps=" + ps + "&rid=" + rid,
            method:"get",
            // cookie:cookie,
            onload:function(xhr){
                data = JSON.parse(xhr.response)
                console.log(data.data.archives)
                // 添加元素
                let div = document.createElement("div");
                div.classList.add('add-main');
                for(let i of data.data.archives){
                    //div.innerHTML += '<div class="add-div">'
                    //div.innerHTML += '<a href="https://www.bilibili.com/video/' + i.bvid + '" target="_blank"><img class="add-img" src="' + i.pic + '" /></a><br>'
                    //div.innerHTML += '<a class="add-a" href="https://www.bilibili.com/video/' + i.bvid + '" target="_blank">' + i.title + '</a></div>';
                    // 视频总div
                    let id = document.createElement("div");
                    div.appendChild(id);
                    id.classList.add('add-div');
                    // 图片链接
                    let a1 = document.createElement("a");
                    id.appendChild(a1);
                    a1.innerHTML = '<img class="add-img" src="' + i.pic + '" />'
                    a1.setAttribute('href',"https://www.bilibili.com/video/" + i.bvid );
                    a1.setAttribute('target',"_blank");
                    // 文字链接
                    let a2 = document.createElement("a");
                    id.appendChild(a2);
                    a2.classList.add('add-a');
                    a2.setAttribute('href',"https://www.bilibili.com/video/" + i.bvid );
                    a2.setAttribute('target',"_blank");
                    // 文字
                    let txt = document.createTextNode(i.title);
                    a2.appendChild(txt);
                    // 作者
                    let a3 = document.createElement("a");
                    id.appendChild(a3);
                    a3.classList.add('add-author');
                    a3.setAttribute('href',"https://space.bilibili.com/" + i.owner.mid );
                    a3.setAttribute('target',"_blank");
                    let author = document.createTextNode(i.owner.name);
                    a3.appendChild(author);
                    // 播放量,点赞,时常
                    let bds = document.createElement("div");
                    bds.classList.add('add-bds');
                    let b = i.stat.view;
                    if(i.stat.view >= 10000){
                        b = (i.stat.view/10000).toFixed(1) + '万'
                    }
                    let d = i.stat.like;
                    if(i.stat.like >= 10000){
                        d = (i.stat.like/10000).toFixed(1) + '万'
                    }
                    let s = i.duration;
                    if(i.duration >= 3600){
                        let mm = parseInt((i.duration%3600)/60);
                        if(mm < 10){
                            mm = '0' + mm;
                        }
                        let ss = i.duration%60;
                        if(ss < 10){
                            ss = '0' + ss;
                        }
                        s = parseInt(i.duration/3600) + ':' + mm + ':' + ss;
                    } else if(i.duration <= 3600 && i.duration >= 60){
                        let mm = parseInt(i.duration/60);
                        if(mm < 10){
                            mm = '0' + mm;
                        }
                        let ss = i.duration%60;
                        if(ss < 10){
                            ss = '0' + ss;
                        }
                        s = mm + ':' + ss;
                    } else {
                        let ss = i.duration%60;
                        if(ss < 10){
                            ss = '0' + ss;
                        }
                        s = '00:' + ss
                    }
                    bds.innerHTML = '<span style="padding-left:10px;float:left;line-height:22px"> ▶ ' + b + ' ❤ ' + d + '</span><span style="padding-right:10px;float:right;line-height:22px">' + s + '</span>';
                    id.appendChild(bds);
                }
                div.innerHTML += style;
                // 设置添加位置
                var header = document.getElementsByClassName('bili-layout');
                if(header.length != 0){
                    header[0].parentNode.insertBefore(div, header[0]); // 新
                } else {
                    document.body.append(div); // 旧
                }
            }
        })
    }
    getvideo();
    var url = window.location.href;
    var btn = document.createElement("input");
    if(url.split('/')[3] == 'video'){
        // 下载按钮
        btn.setAttribute("type", "button");
        btn.setAttribute("value", "下载");
        btn.classList.add('re');
        document.body.append(btn);
        btn.onclick = function(){
            var bv = url.split('/')[4].split('?')[0];
            // alert(bv);
            GM_xmlhttpRequest({
                url:'https://api.bilibili.com/x/web-interface/view/detail?bvid='+bv,
                method:"get",
                // cookie:cookie,
                onload:function(xhr){
                    data = JSON.parse(xhr.response)
                    console.log(data.data.View);
                    var name = data.data.View.title;
                    var cid = data.data.View.cid;
                    GM_xmlhttpRequest({
                        url:'https://api.bilibili.com/x/player/playurl?bvid='+bv+'&cid='+cid+'&qn=80',
                        method:"get",
                        // cookie:cookie,
                        onload:function(xhr){
                            data = JSON.parse(xhr.response)
                            console.log(data);
                            var vurl = data.data.durl[0].url;
                            var con = confirm('下载视频:'+name);
                            if(con == true){
                                /*
                                // 文件流可以自定义文件名,但是反馈太慢了,暂不采用
                                var x = new XMLHttpRequest()
                                x.open("GET", vurl, true)
                                x.responseType = 'blob'
                                x.onload=function(e) {
                                    //会创建一个 DOMString,其中包含一个表示参数中给出的对象的URL。这个 URL 的生命周期和创建它的窗口中的 document 绑定。这个新的URL 对象表示指定的 File 对象或 Blob 对象。
                                    var url = window.URL.createObjectURL(x.response)
                                    var a = document.createElement('a')
                                    a.href = url
                                    a.download = name+'.flv';
                                    a.click()
                                }
                                x.send()
                                */
                                // 普通新页面下载
                                window.open(vurl,'_blank');
                            }
                        }
                    });
                }
            });
        }
    }else{
        // 刷新按钮
        btn.setAttribute("type", "button");
        btn.setAttribute("value", "刷新");
        btn.classList.add('re');
        document.body.append(btn);
        btn.onclick = function(){
            document.body.scrollTop = document.documentElement.scrollTop = 0;
            var dd = document.getElementsByClassName('add-main');
            for(let j of dd){
                j.style.display = "none";
            }
            getvideo(); // 请求后刷新也会请求一次,这里待优化
        }
    }
    // 顶部按钮
    var top = document.createElement("input");
    top.setAttribute("type", "button");
    top.setAttribute("value", "顶部");
    top.classList.add('top');
    document.body.append(top);
    top.onclick = function(){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    }
    // 下拉刷新
    let timeout = null;
    window.onscroll = function() {
      const scrollH = document.documentElement.scrollHeight;// 文档的完整高度
      const scrollT = document.documentElement.scrollTop || document.body.scrollTop; // 当前滚动条的垂直偏移
      const screenH = window.screen.height; // 屏幕可视高度
      if ((scrollH - scrollT - screenH) < 500) { // 只是一个相对值,可以让页面再接近底面的时候就开始请求
          timeout && clearTimeout(timeout); // 判断timeout是否在执行
          timeout = setTimeout(() => {
              console.log('下拉刷新')
              getvideo();
          }, 3000); // api在3s内请求返回重复内容
      }
};
})();