Greasy Fork is available in English.

b站多倍速调节(支持视频)已支持自动变速

b站多倍速调节(支持视频)已支持自动变速🤞🤞🤞~~~

// ==UserScript==
// @name         b站多倍速调节(支持视频)已支持自动变速
// @namespace    lgldlk
// @version      0.8
// @description  b站多倍速调节(支持视频)已支持自动变速🤞🤞🤞~~~
// @author       lgldlk
// @include      *://*.bilibili.com/video/*
// @include      *://*.bilibili.tv/video/*
// @include      *://*.bilibili.com/bangumi/*
// @include      *://*.bilibili.tv/bangumi/*
// @run-at       document-start
// @grant        none
// @license MIT
// ==/UserScript==
let cacheRate = 1,
    cacheFlag = true,
    rateArr = [5, 4.5, 4, 3.5, 3, 2.5, 2, 1.5, 1, 0.5, 0.1]

function waitForNode(nodeSelector, callback) {
    var node = nodeSelector();
    if (node) {
        callback(node);
    } else {
        setTimeout(function() { waitForNode(nodeSelector, callback); }, 100);
    }
}


function debounce(func, wait) {
    let timer;
    return function() {
        let args = arguments;
        if (timer) clearTimeout(timer);
        timer = setTimeout(() => {
            func.apply(this, args)
        }, wait)
    }
}

function deleteChild(e) {
    var child = e.lastElementChild;
    while (child) {
        e.removeChild(child);
        child = e.lastElementChild;
    }
}
const key="lgldl_rate_key"

function setRate(video, rate) {
    video.playbackRate=rate;
    localStorage.setItem(key, rate);
}

function setRateText(rate) {
    (document.querySelector(".bpx-player-ctrl-playbackrate-result")||document.querySelector("div.bpx-player-ctrl-playbackrate-result")).innerText = `${rate}x`;
}


const initRateBody = function(callBack) {
    waitForNode(() => document.querySelector('ul.bpx-player-ctrl-playbackrate-menu')||document.querySelector("ul.bpx-player-ctrl-playbackrate-menu"),
        (node) => {
        cacheRate=Number(localStorage.getItem(key)||1)
        console.log(cacheRate);
             var oV = document.querySelector("video")?document.querySelector("video"):document.querySelector("bwp-video")
            if (oV == undefined) {
                alert("清空缓存后刷新即可使用")
                return;
            }
            deleteChild(node)
            for (let i of rateArr) {

                var tmpLi = document.createElement('li');
                tmpLi.classList = "bpx-player-ctrl-playbackrate-menu-item  ";
                tmpLi.innerText = `${ i}x`;
                tmpLi.style.height = "30px"
                tmpLi.style["font-size"] = "16px"
                tmpLi.style["line-height"] = "30px"
                tmpLi.addEventListener("click", function(k) {
                    return function(e) {
                         e.stopPropagation();
                       e.preventDefault();
                        cacheRate = k
                        setRate(oV, k)
                        setRateText(k)
                    }
                }(i));
                node.appendChild(tmpLi);
            }
        const cacheApply=()=>{
                if (cacheRate !== oV.playbackRate) {
                        setRate(oV, cacheRate);
                        setRateText(cacheRate)

                }
        }
        cacheApply()
            oV.addEventListener('playing', cacheApply);
            callBack && callBack();
        });
};
window.onload = initRateBody(null);
window.onhashchange = function() {
    initRateBody(setCacheRate);
}