Hami Hoykey

add hotkey for HamiVideo. f=fullscreen, Left/Right=TimeControl, space=play/pause, shift+>=speedup, shift+<=speeddown

  1. // ==UserScript==
  2. // @name Hami Hoykey
  3. // @name:zh-TW Hami 快捷鍵
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.7
  6. // @description add hotkey for HamiVideo. f=fullscreen, Left/Right=TimeControl, space=play/pause, shift+>=speedup, shift+<=speeddown
  7. // @description:zh-tw 幫HamiVideo增加快捷鍵, f=全螢幕, Left/Right=時間控制, space=開始/暫停, shift+>=加速, shift+<=減速
  8. // @author Long
  9. // @match https://hamivideo.hinet.net/play/*
  10. // @icon https://www.google.com/s2/favicons?domain=hinet.net
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. "use strict";
  16.  
  17. function isNumeric(str) {
  18. if (typeof str != "string") return false // we only process strings!
  19. return !isNaN(str) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
  20. !isNaN(parseFloat(str)) // ...and ensure strings of whitespace fail
  21. }
  22.  
  23. function getPlaybackRate() {
  24. let videoId = window.location.href.split('/')[4];
  25. if(isNumeric(videoId) && sessionStorage) {
  26. return sessionStorage.getItem(`hamihotkey_playbackrate_${videoId}`);
  27. }
  28. return null;
  29. }
  30.  
  31. function setPlaybackRate($video, rate) {
  32. let maxRate = 2;
  33. let minRate = 0.75;
  34. rate = Math.min(maxRate, Math.max(minRate, rate));
  35. $video.playbackRate = rate;
  36. let videoId = window.location.href.split('/')[4];
  37. if(isNumeric(videoId) && sessionStorage) {
  38. sessionStorage.setItem(`hamihotkey_playbackrate_${videoId}`, rate);
  39. }
  40. }
  41.  
  42. function initHotkey($video) {
  43. $video.addEventListener('keydown', (e) => {
  44. // console.log(e);
  45. if(e.key === 'ArrowLeft') {
  46. $video.currentTime = Math.max(0, $video.currentTime - 5);
  47. } else if(e.key === 'ArrowRight') {
  48. $video.currentTime = Math.min($video.duration, $video.currentTime + 5);
  49. } else if (e.key === ' ') {
  50. if($video.paused) {
  51. $video.play();
  52. } else {
  53. $video.pause();
  54. }
  55. } else if (e.shiftKey && e.key === '<') {
  56. setPlaybackRate($video, $video.playbackRate - 0.25);
  57. } else if (e.shiftKey && e.key === '>') {
  58. setPlaybackRate($video, $video.playbackRate + 0.25);
  59. } else if(e.key === 'f' || e.key === 'F') {
  60. document.querySelector('.vjs-fullscreen-control').click();
  61. }
  62. });
  63.  
  64. let cachePlaybackRate = getPlaybackRate();
  65. if(cachePlaybackRate) {
  66. window.setTimeout(function() {
  67. $video.playbackRate = cachePlaybackRate;
  68. }, 500);
  69. console.log("[Hami Hotkey] set cache PlaybackRate to:", cachePlaybackRate);
  70. }
  71. $video.focus();
  72. console.log("[Hami Hotkey] hotkey loaded.", $video);
  73. };
  74.  
  75. // handle dom created event
  76. let observer = new MutationObserver(mutations => {
  77. for(let mutation of mutations) {
  78. for(let node of mutation.addedNodes) {
  79. if (!(node instanceof HTMLElement)) continue;
  80.  
  81. if (node.matches('#h5video_html5_api')) {
  82. initHotkey(node);
  83. }
  84. }
  85. }
  86. });
  87. observer.observe(document.getElementById('player'), {
  88. childList: true,
  89. attributes: true,
  90. attributeFilter: ['id']
  91. });
  92. })();