動畫瘋鍵盤快捷鍵

使用鍵盤快捷鍵觀賞動畫瘋

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         動畫瘋鍵盤快捷鍵
// @version      0.0.1
// @description  使用鍵盤快捷鍵觀賞動畫瘋
// @author       windicevista
// @match        https://ani.gamer.com.tw/animeVideo.php?sn=*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=gamer.com.tw
// @grant        none
// @namespace https://greasyfork.org/users/579627
// ==/UserScript==


document.addEventListener('keydown', function (event) {
  // K鍵播放/暫停
  if (event.key === 'k' && !event.ctrlKey && !event.altKey && !event.metaKey) {
    togglePlayVideo();
  }
  // M鍵靜音
  if (event.key === 'm' && !event.ctrlKey && !event.altKey && !event.metaKey) {
    toggleMute();
  }
  // F鍵全螢幕
  if (event.key === 'f' && !event.ctrlKey && !event.altKey && !event.metaKey) {
    event.preventDefault(); // 阻止浏览器默认行为
    toggleFullScreen();
  }
  // SHIFT鍵+>加速和SHIFT鍵+<减速
  if (event.shiftKey) {
    if (event.key === '>') {
      changeSpeedRate(0.25); // 加速播放速度
    } else if (event.key === '<') {
      changeSpeedRate(-0.25); // 减速播放速度
    }
  }
});

function togglePlayVideo() {
  // 播放/暫停
  const video = document.querySelector('video');
  if (video.paused) {
    video.play();
  } else {
    video.pause();
  }
}
// 開啟/關閉靜音
function toggleMute() {
  const video = document.querySelector('video');
  video.muted = !video.muted;
}
// 加速/減速
function changeSpeedRate(speedChange) {
  const video = document.querySelector('video');
  if (video) {
    video.playbackRate += speedChange;
    // 限制最大速度为2
    if (video.playbackRate > 2) {
      video.playbackRate = 2;
    }
    // 限制最小速度为0.25
    if (video.playbackRate < 0.25) {
      video.playbackRate = 0.25;
    }
  }
}
// 開啟/關閉全螢幕
function toggleFullScreen() {
  const video = document.querySelector('video');
  if (video) {
    if (!document.fullscreenElement) {
      video.requestFullscreen();
    } else {
      document.exitFullscreen();
    }
  }
}