Greasy Fork is available in English.

youtube HTML5 Auto Loop

youtube再生時に自動ループする

Pada tanggal 14 April 2019. Lihat %(latest_version_link).

  1. // ==UserScript==
  2. // @name youtube HTML5 Auto Loop
  3. // @namespace youtube HTML5 Auto Loop
  4. // @grant none
  5. // @description youtube再生時に自動ループする
  6. // @author TNB
  7. // @match https://www.youtube.com/*
  8. // @version 1.4.5
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. 'use strict';
  13.  
  14. const loop_off = {
  15. when_enable_next_video_autoplay: false,
  16. when_playlist: false,
  17. with_embedded_video: false
  18. };
  19.  
  20. const YoutubeHTML5AutoLoop = {
  21. loop: true,
  22. clickEventCache: false,
  23. init: function() {
  24. this.addListener();
  25. this.initLoop();
  26. },
  27. isLoop: function() {
  28. const ele = {
  29. when_enable_next_video_autoplay: '#improved-toggle[aria-pressed="true"]',
  30. when_playlist: '#playlist:not([hidden])',
  31. with_embedded_video: 'html[data-cast-api-enabled]'
  32. };
  33. for (let i in loop_off) {
  34. const target = document.querySelector(ele[i]);
  35. if (loop_off[i]) {
  36. if (i == 'when_enable_next_video_autoplay') {
  37. const toggle = document.querySelector('#improved-toggle');
  38. if (toggle) toggle.addEventListener('click', this, false);
  39. }
  40. if (target) return false;
  41. }
  42. }
  43. return true;
  44. },
  45. initLoop: function() {
  46. this.loop = this.isLoop();
  47. this.loopOn();
  48. },
  49. loopOn: function() {
  50. const video = document.querySelector('#movie_player video');
  51. console.log(video)
  52. if (video) {
  53. if (this.loop) {
  54. video.setAttribute('loop', '');
  55. } else {
  56. video.removeAttribute('loop');
  57. }
  58. }
  59. },
  60. loopToggle: function() {
  61. this.loop = this.loop? false: true;
  62. this.loopOn();
  63. },
  64. loopDisplay: function() {
  65. const video = document.querySelector('video:hover');
  66. if (video) {
  67. const checkBox = document.querySelector('.ytp-contextmenu [aria-checked]');
  68. if (checkBox) {
  69. checkBox.setAttribute('aria-checked', this.loop);
  70. if (!this.clickEventCache) {
  71. checkBox.addEventListener('click', this, false);
  72. this.clickEventCache = true;
  73. }
  74. }
  75. }
  76. },
  77. watchAjax: function() {
  78. const mm = new MutationObserver(() => {
  79. const video = document.querySelector('#movie_player video');
  80. if (video) {
  81. const mo = new MutationObserver(() => {
  82. this.initLoop();
  83. });
  84. mo.observe(video, {attributes: true, attributeFilter: ['src']});
  85. mm.disconnect();
  86. this.initLoop();
  87. }
  88. });
  89. mm.observe(document.body, {childList: true, subtree: true});
  90. },
  91. addListener: function() {
  92. window.addEventListener('DOMContentLoaded', this, false);
  93. window.addEventListener('contextmenu', this, false);
  94. },
  95. handleEvent: function(e) {
  96. switch (e.type) {
  97. case 'DOMContentLoaded':
  98. this.watchAjax();
  99. break;
  100. case 'contextmenu':
  101. this.loopDisplay();
  102. break;
  103. case 'click':
  104. this.loopToggle();
  105. break;
  106. }
  107. }
  108. };
  109.  
  110. YoutubeHTML5AutoLoop.init();