happy html keyboard player

allow forward video with keyboard

当前为 2021-08-26 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name happy html keyboard player
  3. // @name:zh-TW happy html keyboard player
  4. // @namespace http://gholk.github.io
  5. // @description allow forward video with keyboard
  6. // @description:zh-TW allow forward video with keyboard
  7. // @match https://www.ptsplus.tv/season/*
  8. // @version 1.0.0
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. const config = {
  13. '公視勇者動畫系列': {
  14. regexp: /4b572dd5-bdc7-45a6-ba35-accfe9cda3df/,
  15. callback() {
  16. const list = document.querySelectorAll(
  17. 'li.vjs-menu-item[role=menuitemradio]'
  18. )
  19. const button = Array.from(list).find(
  20. node => node.children[0]?.textContent == '480p'
  21. )
  22. if (button) {
  23. button.click()
  24. console.debug('click 480p')
  25. return 'done'
  26. }
  27. }
  28. }
  29. }
  30.  
  31. function keepTry(callback) {
  32. let handleId
  33. handleId = setInterval(() => {
  34. if (callback() == 'done') clearInterval(handleId)
  35. }, 2)
  36. }
  37. function configSet() {
  38. for (const key in config) {
  39. const option = config[key]
  40. if (window.location.href.match(option.regexp)) {
  41. if (option.callback) keepTry(option.callback)
  42. }
  43. }
  44. }
  45.  
  46. configSet()
  47. let urlOld = window.location.href
  48. keepTry(() => {
  49. const urlCurrent = window.location.href
  50. if (urlCurrent != urlOld) {
  51. urlOld = urlCurrent
  52. configSet()
  53. }
  54. })
  55.  
  56. window.addEventListener('keydown', event => {
  57. const video = document.querySelector('video')
  58. const step = 5
  59. switch (event.key) {
  60. case 'ArrowRight':
  61. case 'Right':
  62. video.currentTime += step
  63. break
  64. case 'ArrowLeft':
  65. case 'Left':
  66. video.currentTime -= step
  67. break
  68. case ' ':
  69. if (video.paused) video.play()
  70. else video.pause()
  71. event.preventDefault()
  72. break
  73. }
  74. })