youtube HTML5 Auto Loop

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

2023-01-27 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name           youtube HTML5 Auto Loop
// @namespace      youtube HTML5 Auto Loop
// @grant          none
// @description    youtube再生時に自動ループする
// @author         TNB
// @match          https://www.youtube.com/*
// @version        1.5.2
// @run-at         document-start
// ==/UserScript==

'use strict';

const loop_off = {
  when_enable_next_video_autoplay: false,
  when_playlist: false,
  with_embedded_video: false
};

const YoutubeHTML5AutoLoop = {
  loop: true,
  video: null,
  prevSrc: null,
  button: null,
  eventcache: false,
  cancelInit: false,
  ele: {
    when_enable_next_video_autoplay: '.ytp-button:not([style*="display"]) .ytp-autonav-toggle-button[aria-checked="true"]',
    when_playlist: '#secondary-inner > #playlist:not([hidden])',
    with_embedded_video: 'html[data-cast-api-enabled="true"]'
  },
  init: function() {
    this.addListener();
  },
  isLoop: function() {
    return !Object.keys(loop_off).some(a => loop_off[a] && document.querySelector(this.ele[a]));
  },
  goLoop: function() {
    this.video.currentTime = 0;
    this.video.play;
  },
  loopOn: function() {
    if (this.loop) this.video.setAttribute('loop', '');
    else this.video.removeAttribute('loop');
  },
  initLoop: function() {
    this.loop = this.isLoop();
    this.loopOn();
  },
  loopToggle: function() {
    this.loop = !this.loop;
    this.loopOn();
  },
  loopDisplay: function() {
    const video = document.querySelector('video:hover');
    if (!video) return;
    const checkBox = document.querySelector('.ytp-contextmenu [aria-checked]');
    checkBox.setAttribute('aria-checked', this.loop);
    if (!this.eventcache) {
      checkBox.addEventListener('click', this, true);
      this.eventcache = true;
    }
  },
  watchAjax: function() {
    if (window != window.parent && document.getElementById('chat')) return;
    const mm = new MutationObserver(() => {
      const player = document.getElementById('movie_player');
      if (!player) return;
      this.video = player.querySelector('video');
      const mo = new MutationObserver(() => {
        if (this.video.src != this.prevSrc) this.cancelInit = false;
        if (!this.cancelInit) this.initLoop();
        if (this.loop && (player.classList.contains('ended-mode') || player.querySelector('.html5-endscreen:not([style*="display"])'))) this.goLoop();
        this.prevSrc = this.video.src;
        this.video = player.querySelector('video');
      });
      mo.observe(player, {attributes: true, attributeFilter: ['class']});
      if (loop_off.when_enable_next_video_autoplay) this.addToggleEvent();
      this.initLoop();
      mm.disconnect();
    });
    mm.observe(document.body, {childList: true, subtree: true});
  },
  addToggleEvent: function() {
    this.button = document.querySelector('.ytp-autonav-toggle-button');
    if (!this.button) return;
    this.button.addEventListener('click', () => {
      this.loop = JSON.parse(this.button.getAttribute('aria-checked'));
      this.cancelInit = true;
    });
  },
  addListener: function() {
    window.addEventListener('DOMContentLoaded', this);
    window.addEventListener('contextmenu', this);
  },
  handleEvent: function(e) {
    switch (e.type) {
      case 'DOMContentLoaded':
        this.watchAjax();
        break;
      case 'contextmenu':
        this.loopDisplay();
        break;
      case 'click':
        this.loopToggle();
        document.body.click();
        if (this.button && !document.querySelector('#secondary-inner > #playlist:not([hidden])')) this.button.click();
        this.cancelInit = true
        e.stopPropagation();
        break;
    }
  }
};

YoutubeHTML5AutoLoop.init();