Greasy Fork

Greasy Fork is available in English.

bilibili 自动播放

最开始的三个开关autoPlay表示点进视频2秒后自动开始播放,keyboardControl表示点进视频后可用键盘控制视频(空格,↑,↓,←,→),listPlay表示视频结束后跳过5秒等待直接下一part

当前为 2019-06-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         bilibili 自动播放
// @namespace    www.papercomment.tech
// @version      0.6
// @description  最开始的三个开关autoPlay表示点进视频2秒后自动开始播放,keyboardControl表示点进视频后可用键盘控制视频(空格,↑,↓,←,→),listPlay表示视频结束后跳过5秒等待直接下一part
// @author       c4r
// @match        https://www.bilibili.com/bangumi/*
// @match        https://www.bilibili.com/video/*
// @match        https://www.bilibili.com/watchlater/*
// @grant        none
// @license      MPL-2.0
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    let autoPlay = true
    let keyboardControl = true
    let listPlay = true

    function playPause(){
        //console.warn('UI', 'playPause')
        let target = document.getElementsByTagName('video')[0]
        if (target.paused){
            target.play()
        }
        else{
            target.pause()
        }
    }


    let observePlayNow = new MutationObserver((list, obs)=>{
        //console.log('UI', list)
        list.forEach((mutation, index)=>{
            if(mutation.target.matches('div.bilibili-player-video-toast-bottom')&&
               mutation.addedNodes.length!==0&&
               mutation.addedNodes[0].querySelector('.bilibili-player-video-toast-item-jump').textContent=='立即播放'){
                //console.log('UI',mutation)
                mutation.addedNodes[0].querySelector('.bilibili-player-video-toast-item-jump').click()
            }
        })
    })

    let observePlayer = new MutationObserver((list, obs)=>{
        //console.warn('NUI', document.getElementById('bilibiliPlayer').querySelector('div.bilibili-player-video-wrap'))
        document.getElementById('bilibiliPlayer').querySelector('div.bilibili-player-video-wrap').click()
        obs.disconnect()
    })

    let traceAnchor, bofqiAnchor, tracePlayer, bilibiliPlayerAnchor

    traceAnchor = setInterval(()=>{
        // console.warn('UI', 'tik tok')
        if(bofqiAnchor = document.getElementById('bofqi')){
            clearInterval(traceAnchor)
            // console.warn('UI', anchor)
            if(listPlay){
                observePlayNow.observe(bofqiAnchor, {childList:true, subtree:true})
            }
            if(autoPlay){
                setTimeout(playPause, 2000);
            }
        }
    }, 200)

    tracePlayer = setInterval(()=>{
        if(bilibiliPlayerAnchor = document.getElementById('bilibiliPlayer')){
            clearInterval(tracePlayer)
            if(keyboardControl){
                observePlayer.observe(bilibiliPlayerAnchor, {childList: true, subtree: true})
            }
        }
    }, 200)
})();