// ==UserScript== // @name bilibili 倍速控制 // @namespace http://tampermonkey.net/ // @version 1.2 // @description 使用快捷键控制bilibili的倍速选择 // @author pipizhu // @match http*://www.bilibili.com/video/* // @match *://www.youtube.com/* // @icon https://www.google.com/s2/favicons?sz=64&domain=bilibili.com // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @license MIT // @downloadURL none // ==/UserScript== /* jshint esversion: 8 */ (function() { "use strict"; document.body.onload = init; })(); const speedList = [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 3]; const upKeyCode = "Period"; const downKeyCode = "Comma"; let customUpKey = GM_getValue("customUpKey"); let customDownKey = GM_getValue("customDownKey"); async function init() { // addActionBtns(); console.log("init keybindings"); GM_registerMenuCommand("打开设置面板", openSettings); try { bindKeys(); const cachedSpeed = GM_getValue("currentSpeed"); // await delay(1000); if (cachedSpeed) { console.log("cached speed", cachedSpeed); GM_registerMenuCommand("✅当前记忆倍速 X" + cachedSpeed, () => { }); setSpeed(cachedSpeed); } console.log("bind success!"); } catch (e) { console.log("error", e); } } function getCurrentSpeed() { const video = getCurrentVideo(); return video.playbackRate; } function bindKeys() { const keys = "0123456789"; document.addEventListener( "keydown", (e) => { if ( keys.indexOf(e.key) != -1 && !(e.isComposing || e.ctrlKey || e.altKey) ) { e.stopImmediatePropagation(); } if (e.key === "2") { setSpeed(2); return; } if (e.key === "5") { setSpeed(1.5); return; } if (e.key === "7") { setSpeed(1.75); return; } if (e.key === "1" || e.key === "z") { setSpeed(1); return; } if (e.key === "3") { setSpeed(3); return; } const video = getCurrentVideo(); const currentSpeed = video.playbackRate; const isCustom = customUpKey && customDownKey; let currentIndex = speedList.findIndex((s) => s === currentSpeed); if (isCustom) { if (e.key === customUpKey) { currentIndex = Math.min(currentIndex + 1, speedList.length - 1); } if (e.key === customDownKey) { currentIndex = Math.max(currentIndex - 1, 0); } } else { if (e.code === upKeyCode && e.shiftKey) { // increase speed currentIndex = Math.min(currentIndex + 1, speedList.length - 1); } else if (e.code === downKeyCode && e.shiftKey) { // decrease speed currentIndex = Math.max(currentIndex - 1, 0); } } const speed = speedList[currentIndex]; setSpeed(speed); }, true, ); } function setSpeed(speed) { const video = getCurrentVideo(); video.playbackRate = speed; } async function delay(time) { return new Promise((res) => setTimeout(() => res(), time)); } function getCurrentVideo() { const video = document.getElementsByTagName("video")[0]; return video; } function addStyle(styleString) { const style = document.createElement("style"); style.type = "text/css"; style.appendChild(document.createTextNode(styleString)); document.head.append(style); } function preserveSpeed() { const currentSpeed = getCurrentSpeed(); GM_setValue("currentSpeed", currentSpeed); } function clearSpeed() { GM_setValue("currentSpeed", ""); } function openSettings() { const speed = getCurrentSpeed(); const currentKey = { up: customUpKey || ">", down: customDownKey || "<", }; const html = `