Greasy Fork

Greasy Fork is available in English.

视频倍速快捷键

speed up/down video

目前为 2020-02-26 提交的版本。查看 最新版本

// ==UserScript==
// @name         视频倍速快捷键
// @version      0.2
// @description  speed up/down video
// @author       BlueSky
// @match        *://*.netflix.com/*
// @match        *://*.bilibili.com/*
// @grant        none
// @namespace http://greasyfork.icu/users/447360
// ==/UserScript==

(function() {
    'use strict';
    let title = ''
    let rate = 1

    window.addEventListener('keyup', (e) => {
        if (e.key === '=' && rate < 4) {
            rate += 0.5
        } else if (e.key === '-' && rate > 0.5) {
            rate -= 0.5
        } else if (e.key === ']' && rate < 4) {
            rate += 0.25
        } else if (e.key === '[' && rate > 0.25) {
            rate -= 0.25
        } else if (e.key === '0') {
            rate = 0.5
        } else if (e.key === '1') {
            rate = 1
        } else if (e.key === '2') {
            rate = 2
        } else if (e.key === '3') {
            rate = 3
        } else if (e.key === '4') {
            rate = 4
        } else {
            return
        }
        setVideoRate()
        setVideoTitle()
    })

    function setVideoRate() {
        console.log('speed at', rate)
        document.querySelector('video').playbackRate = rate
    }

    function setVideoTitle() {
        let selectors = []
        if (/bilibili/.test(location.href)) {
            selectors = [
                '.bilibili-player-video-top-title',
                'h1 .tit'
            ]
        } else if (/netflix/.test(location.href)) {
            selectors = ['.ellipsize-text h4']
        }
        selectors.forEach((selector) => {
            const el = document.querySelector(selector)
            if (el) {
              if (!title || !new RegExp(title).test(el.innerHTML)) {
                  title = el.innerHTML
              }
              el.innerHTML = `[${rate}x] ${title}`
            }
        })
    }
})();