happy html keyboard player

allow forward video with keyboard

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name  happy html keyboard player
// @name:zh-TW  happy html keyboard player
// @namespace  http://gholk.github.io
// @description  allow forward video with keyboard
// @description:zh-TW  allow forward video with keyboard
// @match https://www.ptsplus.tv/season/*
// @version  1.0.0
// @grant  none
// ==/UserScript==

const config = {
    '公視勇者動畫系列': {
        regexp: /4b572dd5-bdc7-45a6-ba35-accfe9cda3df/,
        callback() {
            const list = document.querySelectorAll(
                'li.vjs-menu-item[role=menuitemradio]'
            )
            const button = Array.from(list).find(
                node => node.children[0]?.textContent == '480p'
            )
            if (button) {
                button.click()
                console.debug('click 480p')
                return 'done'
            }
        }
    }
}

function keepTry(callback) {
    let handleId
    handleId = setInterval(() => {
        if (callback() == 'done') clearInterval(handleId)
    }, 2)
}
function configSet() {
    for (const key in config) {
        const option = config[key]
        if (window.location.href.match(option.regexp)) {
            if (option.callback) keepTry(option.callback)
        }
    }
}

configSet()
let urlOld = window.location.href
keepTry(() => {
    const urlCurrent = window.location.href
    if (urlCurrent != urlOld) {
        urlOld = urlCurrent
        configSet()
    }
})

window.addEventListener('keydown', event => {
    const video = document.querySelector('video')
    const step = 5
    switch (event.key) {
    case 'ArrowRight':
    case 'Right':
        video.currentTime += step
        break
    case 'ArrowLeft':
    case 'Left':
        video.currentTime -= step
        break
    case ' ':
        if (video.paused) video.play()
        else video.pause()
        event.preventDefault()
        break
    }
})