youtube HTML5 Auto Loop

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

目前為 2018-10-31 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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.4.1
// ==/UserScript==

'use strict';

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

var YoutubeHTML5AutoLoop = {
  loop: true,
  clickEventCache: false,
  init: function() {
    this.initLoop();
    this.addListener();
  },
  isLoop: function() {
    var ele = {
      when_enable_next_video_autoplay: '#improved-toggle[aria-pressed="true"]',
      when_playlist: '#playlist:not([hidden])',
      with_embedded_video: 'html[data-cast-api-enabled]'
    };
    for (var i in loop_off) {
      var target = document.querySelector(ele[i]);
      if (loop_off[i]) {
        if (i == 'when_enable_next_video_autoplay') {
          var toggle = document.querySelector('#improved-toggle');
          if (toggle) toggle.addEventListener('click', this, false);
        }
        if (target) return false;
      }
    }
    return true;
  },
  initLoop: function() {
    this.loop = this.isLoop();
    this.loopOn();
  },
  loopOn: function() {
    var video = document.getElementsByTagName('video')[0];
    if (video) {
      if (this.loop) {
        video.setAttribute('loop', '');
      } else {
        video.removeAttribute('loop');
      }
    }
  },
  loopToggle: function() {
    this.loop = this.loop? false: true;
    this.loopOn();
  },
  loopDisplay: function() {
    var video = document.querySelector('video:hover');
    if (video) {
      var checkBox = document.querySelector('.ytp-contextmenu [aria-checked]');
      if (checkBox) {
        checkBox.setAttribute('aria-checked', this.loop);
        if (!this.clickEventCache) {
          checkBox.addEventListener('click', this, false);
          this.clickEventCache = true;
        }
      }
    }
  },
  watchAjax: function() {
    if (window != window.parent) return;
    var flg = false,
        mm = new MutationObserver(() => {
      var video = document.getElementsByTagName('video')[0];
      if (video && !flg) {
        flg = true;
        var mo = new MutationObserver(() => {this.initLoop();});
        mo.observe(video, {attributes: true, attributeFilter: ['style', 'src']});
        mm.disconnect();
      }
    });
    mm.observe(document.body, {childList: true, subtree: true});
  },
  addListener: function() {
    window.addEventListener('load', this, false);
    window.addEventListener('contextmenu', this, false);
  },
  handleEvent: function(e) {
    switch (e.type) {
      case 'load':
        this.watchAjax();
        break;
      case 'contextmenu':
        this.loopDisplay();
        break;
      case 'click':
        this.loopToggle();
        break;
    }
  }
};

YoutubeHTML5AutoLoop.init();