Use similar controls as on YouTube when watching Showtime (`f` for full screen, `k` to play/pause, `c` for captions)
当前为
// ==UserScript==
// @name YouTube-style keyboard controls on Showtime
// @namespace showtime.keyboard
// @version 0.1
// @description Use similar controls as on YouTube when watching Showtime (`f` for full screen, `k` to play/pause, `c` for captions)
// @match https://showtime.com/*
// @match https://www.showtime.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// change these constants if you prefer to use a different key
// (or change the letter to uppercase if you want the shortcut to require the use of Shift)
const FULL_SCREEN_KEY = 'f';
const PLAY_PAUSE_KEY = 'k';
const CLOSED_CAPTIONS_KEY = 'c';
function clickButton(className) {
const buttons = document.querySelectorAll('button.' + className);
if (buttons && buttons.length == 1) {
buttons[0].click();
} else {
console.error('Button not found!');
}
}
function isPlaying(videoElement) {
return !!(videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended && videoElement.readyState > 2);
}
addEventListener("keypress", function(e) {
if (e.ctrlKey || e.altKey) { // return early if any modifier key like Control or Alt is part of the key press
return;
}
if (e.key == FULL_SCREEN_KEY) {
if (window.innerHeight == screen.height) {
clickButton('exit-fullscreen');
} else {
clickButton('enter-fullscreen');
}
} else if (e.key == PLAY_PAUSE_KEY) {
const videos = document.getElementsByTagName('video');
if (videos && videos.length == 1 && isPlaying(videos[0])) {
clickButton('pause');
} else {
clickButton('play');
}
} else if (e.key == CLOSED_CAPTIONS_KEY) {
const ccContainer = document.querySelectorAll('div.player-closed-captioning-container > div');
if (ccContainer && ccContainer.length == 1) {
if (ccContainer[0].classList.contains('closed-captioning-enabled')) { // already enabled
clickButton('closed-captioning-disable');
} else {
clickButton('closed-captioning-enable');
}
} else {
console.error('No CC container found');
}
}
});
})();