youtube HTML5 Auto Loop

youtubeHTML5プレイヤー再生時に自動ループする

2017-02-11 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  1. // ==UserScript===
  2. // @name youtube HTML5 Auto Loop
  3. // @namespace youtube HTML5 Auto Loop
  4. // @grant none
  5. // @description youtubeHTML5プレイヤー再生時に自動ループする
  6. // @author TNB
  7. // @match *://*.youtube.com/*
  8. // @version 1.0
  9. // ==/UserScript==
  10.  
  11. var loop_off = {
  12. when_enable_next_video_autoplay: false,
  13. when_playlist: false,
  14. with_embedded_video: false
  15. };
  16.  
  17. var YoutubeHTML5AutoLoop = {
  18. loop: '',
  19. eventCache: {},
  20. isLoop: function() {
  21. var ele = {when_enable_next_video_autoplay:'input[type="checkbox"]:checked+label .unchecked', when_playlist:'#watch-appbar-playlist', with_embedded_video:'html[dir]'};
  22. for (var i in loop_off) {
  23. if (loop_off[i] && document.querySelector(ele[i])) {
  24. return false;
  25. }
  26. }
  27. return true;
  28. },
  29. init: function() {
  30. this.loop = this.isLoop();
  31. this.addListener();
  32. },
  33. loopOn: function() {
  34. var video = document.getElementsByTagName('video')[0];
  35. if (this.loop) {
  36. video.setAttribute('loop', '');
  37. } else {
  38. video.removeAttribute('loop');
  39. }
  40. },
  41. loopToggle: function() {
  42. this.loop = this.loop? false: true;
  43. this.loopOn();
  44. },
  45. loopDisplay: function() {
  46. var video = document.querySelector('video:hover');
  47. if (video) {
  48. var checkBox = document.querySelector('[aria-checked]');
  49. checkBox.setAttribute('aria-checked', this.loop);
  50. if (!this.eventCache.addLoopToggle) {
  51. checkBox.addEventListener('click', this, false);
  52. this.eventCache.addLoopToggle = true;
  53. }
  54. }
  55. },
  56. watchAjax: function() {
  57. var content = document.getElementById('page');
  58. if (content) {
  59. var ev = new CustomEvent('completedRequest'),
  60. mo = new MutationObserver(function(){window.dispatchEvent(ev)});
  61. mo.observe(content, {attributes:true, attributeFilter:['class']});
  62. }
  63. },
  64. addListener: function() {
  65. if (!this.eventCache.loadEvent) {
  66. window.addEventListener('load', this, false);
  67. window.addEventListener('contextmenu', this, false);
  68. window.addEventListener('completedRequest', this, false);
  69. this.eventCache.loadEvent = true;
  70. }
  71. if (loop_off.when_enable_next_video_autoplay) {
  72. var autoplay = document.getElementById('autoplay-checkbox');
  73. if (autoplay) {
  74. autoplay.addEventListener('click', this, false);
  75. }
  76. }
  77. },
  78. handleEvent: function(e) {
  79. switch(e.type) {
  80. case 'load':
  81. this.watchAjax();
  82. this.loopOn();
  83. break;
  84. case 'contextmenu':
  85. this.loopDisplay();
  86. break;
  87. case 'click':
  88. this.loopToggle();
  89. break;
  90. case 'completedRequest':
  91. this.init();
  92. this.loopOn();
  93. break;
  94. }
  95. }
  96. };
  97.  
  98. YoutubeHTML5AutoLoop.init();