B站_播放器自动化

播放时自动点击网页全屏、播放完成后自动退出全屏。

As of 2023-01-25. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name               B站_播放器自动化
// @name:zh-CN         B站_播放器自动化
// @name:en-US         BILI_Player Automation
// @description        播放时自动点击网页全屏、播放完成后自动退出全屏。
// @version            1.0.7
// @author             LiuliPack
// @license            WTFPL
// @namespace          https://gitlab.com/LiuliPack/UserScript
// @include            /www\.bilibili\.com\/(video|bangumi/play|festival)\/*/
// @run-at             document-end
// ==/UserScript==

'use strict';

// 定义元素快捷选择器($(元素定位符))、元素存在检测($$(元素定位符))和切换网页全屏(ToggleFull())函数
let $ = ele => document.querySelector(ele);
function $$(ele) {
    return new Promise(resolve => {
        if ($(ele)) {
            return resolve($(ele));
        }
        const observer = new MutationObserver(mutations => {
            if ($(ele)) {
                resolve($(ele));
                observer.disconnect();
            }
        });
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    });
}
function ToggleFull() {
    // 如果是剧集播放页面,当网页全屏元素存在就点击;否则如果普通播放页网页全屏元素存在,就点击;否则如果是节日专题页面网页全屏元素存在,就点击。
    (/bangumi\/play/.test(location.pathname)) ? $$('.squirtle-video-pagefullscreen').then(() => {$('.squirtle-video-pagefullscreen').click()}) : ($('.bpx-player-ctrl-btn[aria-label="网页全屏"]')) ? $('.bpx-player-ctrl-btn[aria-label="网页全屏"]').click() : ($('.bpx-player-ctrl-web')) ? $('.bpx-player-ctrl-web').click() : '' ; ; ;
}

// 等待元素存在,防止出版音像节目页面运行错误
$$('video').then(() => {
    // 监听视频播放
    $('video').addEventListener("play", () => {
        // 如果非全屏
        if(!/web|full/.test($('.bpx-player-container').getAttribute('data-screen'))) {
            // 点击网页全屏按钮
            ToggleFull();
        }
    });
    // 监听视频暂停
    $('video').addEventListener("pause", vid => {
        // 防止进度获取异常,再次定义视频参数
        vid = $('video');
        // 如果全屏状态且播放完成
        if(/web|full/.test($('.bpx-player-container').getAttribute('data-screen')) && parseInt(vid.currentTime) === parseInt(vid.duration)) {
            // 点击网页全屏按钮
            ToggleFull();
        }
    });
});