Bilibili Auto Play Next

合集、收藏夹连播,懂得都懂

当前为 2023-12-08 提交的版本,查看 最新版本

// ==UserScript==
// @name         Bilibili Auto Play Next
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  合集、收藏夹连播,懂得都懂
// @author       wsure
// @match        *://*.bilibili.com/*
// @icon         https://www.google.com/s2/favicons?domain=google.cn
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_addElement
// @grant        GM_xmlhttpRequest
// @require      https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js
// @run-at document-end
// @license MIT
// ==/UserScript==


let playNext = GM_getValue('playNext');

// 一段自动设置播放速度和画质的代码,写着玩的没啥用了
function setQualityAndSpeed(){
    const video = document.querySelector("video");
    if (window.location.href.includes('BV')){
        let flag = false;
        video.addEventListener("play", (event) => {
            if(!flag){
                $('div.bpx-player-ctrl-btn.bpx-player-ctrl-quality > ul > li:nth-child(3)').click();
                setTimeout(() => {$('div.bpx-player-ctrl-btn.bpx-player-ctrl-playbackrate > ul > li:nth-child(2)').click();},1000)
                flag = true;
                console.log('set player quality')
            }
        });
    }
}

function isVideoList(){
    return $(".base-video-sections-v1").length >0 // 合集
    || $('div.action-list-container').length >0  // 收藏
    || $('#multi_page > div.head-con').length >0  // 分P
    ;
}

//在视频结束事件里加入“播放下一个”的操作
function setNextPlay(){
    const video = document.querySelector("video");
    if(isVideoList()){
        video.addEventListener("ended", (event) => {
            if (playNext)
                setTimeout(function () {
                    // 点下一个按钮
                    if($('.bpx-player-ctrl-next').length >0){
                        $('.bpx-player-ctrl-next').click();
                    }
                    else if($('div.video-section-list.section-0 > div:nth-child(1)').length >0){  //合集末尾没有按钮,回到首个视频,不喜欢这个功能可以直接删掉这个else if
                        $('div.video-section-list.section-0 > div:nth-child(1)').click()
                    }
                    else if($('#multi_page > div.head-con').length >0){   //分P合集末尾没有按钮,回到首个视频,不喜欢这个功能可以直接删掉这个else if
                        $('#multi_page > div.cur-list > ul > li:nth-child(1) > a > div > div.link-content > span.part').click()
                    }
                }, 1000);
        })
    }
}

function initButton(){
        // Create the floating "B" button
    let buttonB = $('<button>', {
        id:'my-next-btn',
        text: 'Next',
        css: {
            backgroundColor: (playNext?'#fb7299':'gray'),
            color: 'white',
            borderRadius: '8px',
            padding: '10px',
            border: '2px solid white'
        },
        click: function () {
            setPlayNext(!playNext)
        }
    });

    //合集
    if($(".base-video-sections-v1").length >0){
        $('div.second-line_left').append(buttonB);
    }
    //收藏夹
    if($("div.action-list-container").length >0){
        $('div.header-left').append(buttonB);
    }
    if($('#multi_page > div.head-con').length >0){
        $('div.head-con').append(buttonB);
    }
}

function setPlayNext(v){
    if(v){
        playNext = true;
        GM_setValue('playNext',true)
        document.querySelector('#my-next-btn').style.backgroundColor='#fb7299'
    } else{
        playNext = false;
        GM_setValue('playNext',false)
        document.querySelector('#my-next-btn').style.backgroundColor='gray'

    }
}

function afterListInit() {
  setTimeout(function () {
      //仅在符合`合集`或者`收藏夹`或者`分p合集`,并且没有找到我们的按钮的情况下初始化按钮
    if(isVideoList() && $('#my-next-btn').length ==0){
      initButton();
    } else {
      afterListInit();
    }
  },5000); // 5秒刷一次,免得列表更新按钮没了
}

(function() {
    'use strict';
    // Your code here...

    $(document).ready(function () {
        afterListInit();
        setNextPlay();
    });
})();