autoplay

为bilibili添加播放自动推荐功能

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         autoplay
// @version      1.0.0
// @description    为bilibili添加播放自动推荐功能
// @compatible   chrome
// @license      MIT
// @grant    unsafeWindow
// @match    https?://www.bilibili.com/video/*
// @namespace https://greasyfork.org/users/249131
// ==/UserScript==

let enableAutoPlay = unsafeWindow._enableAutoPlay = true;
let video = document.querySelector('video');
video.addEventListener("canplay", function () {
    video.play();
});
//让网站资源先解析,不然会报错
setTimeout(function () {
    let reco_list = document.querySelector('#reco_list');

    if (!reco_list) {
        alert("本页面没有推荐列表,不适合执行此脚本");
        return;
    }

    let buttonContainer = reco_list.querySelector(".rec-title");
    buttonContainer.appendChild(createEnableButton());

    let listURL = reco_list.querySelectorAll("a");
    let firstURl = listURL[0];
    //enableAutoPlay
    video.addEventListener('ended', function () {
        if (enableAutoPlay) {
            unsafeWindow.location.assign(firstURl);
        }
    });
}, 5000);

let reco_list = document.querySelector('#reco_list');
//处理ajax
reco_list.addEventListener("click", function (event) {
    //event.target.tagName
    if (event.target.tagName.toLowerCase() == "img" || event.target.tagName.toLowerCase() == "a") {
        setTimeout(function () {
            let video = document.querySelector('video');
            let reco_list = document.querySelector('#reco_list');
            if (!reco_list) {
                alert("本页面没有推荐列表,不适合执行此脚本");
                return;
            }

            let listURL = reco_list.querySelectorAll("a");
            let firstURl = listURL[0];

            video.addEventListener('ended', function () {
                if (enableAutoPlay) {
                    location.assign(firstURl);
                }
            });
        }, 2000);
    }
});

function createEnableButton() {
    let enableButton = document.createElement("button");
    enableButton.style.width = 150;
    enableButton.style.height = 30;
    enableButton.className = "bi-btn gz";
    enableButton.innerHTML = "取消自动播放";
    enableButton.addEventListener("click", function () {
        if (!enableAutoPlay) {
            enableAutoPlay = true;
            enableButton.innerHTML = "取消自动播放";
        } else {
            enableAutoPlay = false;
            enableButton.innerHTML = "自动播放";
        }
    });
    return enableButton;
}