Bilibili 自动切P

为什么要单独写个脚本呢?因为自动连播就是个傻逼……

安裝腳本?
作者推薦腳本

您可能也會喜歡 Bilibili 哔哩哔哩视频点踩

安裝腳本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bilibili 自动切P
// @namespace    https://github.com/Tsuk1ko
// @version      1.0.6
// @description  为什么要单独写个脚本呢?因为自动连播就是个傻逼……
// @author       神代綺凛
// @license      GPL-3.0
// @match        https://www.bilibili.com/video/*
// @icon         https://www.bilibili.com/favicon.ico
// @grant        unsafeWindow
// @run-at       document-start
// ==/UserScript==

(() => {
    'use strict';

    const log = (...args) => console.log('[BilibiliAutoNext]', ...args);

    const playerVideoEndedEvents = ['video_media_ended', 'MEDIA_ENDED'];
    const playerDestroyEvents = ['video_destroy'];

    const win = typeof unsafeWindow === 'undefined' ? window : unsafeWindow;

    const get = (obj, paths) => paths.reduce((prev, path) => (typeof prev === 'undefined' ? undefined : prev[path]), obj);

    const initPlayer = () => {
        const { player } = win;
        if (!player) return;
        playerVideoEndedEvents.forEach(event => {
            player.on(event, onVideoEnded);
        });
        playerDestroyEvents.forEach(event => {
            player.on(event, () => setTimeout(initPlayer));
        });
        log('initialized successfully');
    };

    const onVideoEnded = () => {
        const curP = get(win, ['__INITIAL_STATE__', 'p']);
        const totalP = get(win, ['__INITIAL_STATE__', 'videoData', 'videos']);
        log(`video ended ${curP}/${totalP}`);
        if (curP < totalP) {
            log('goto next video');
            setTimeout(() => player.next(), 100);
        }
    };

    Object.defineProperty(win, 'player', {
        configurable: true,
        set: player => {
            delete win.player;
            win.player = player;
            initPlayer();
        },
    });
})();