YT pause video on comment

Youtube video is paused if you have opened the url with link to comment

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name          YT pause video on comment
// @description   Youtube video is paused if you have opened the url with link to comment
// @author        MK
// @namespace     max44
// @homepage      https://greasyfork.org/en/users/309172-max44
// @match         *://*.youtube.com/*
// @match         *://*.youtu.be/*
// @icon          https://cdn.icon-icons.com/icons2/1488/PNG/512/5295-youtube-i_102568.png
// @version       1.0
// @license       MIT
// @require       https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @run-at        document-end
// ==/UserScript==

(function() {
  'use strict';

  var urlAtLastCheck = "";
  var divVideo0 = null;
  var divVideo1 = null;
  var waitVideo;

  //Check URL changes
  const rootCallback = function (mutationsList, observer) {
    if (urlAtLastCheck != document.location.href) {
      urlAtLastCheck = document.location.href;
      clearInterval(waitVideo);
      if (urlAtLastCheck.search("/watch?") > 0 && urlAtLastCheck.search("&lc=") > 0) {
        pauseVideo();
      }
    }
  }

  const rootNode = document.querySelector("body");
  if (rootNode != null) {
    const rootObserver = new MutationObserver(rootCallback);
    rootObserver.observe(rootNode, {childList: true, subtree: true, attributes: true, characterData: false});
  }

  function pauseVideo() {
    waitVideo = setInterval(function() {
      var vCount0 = 0;
      var vCount1 = 0;
      var i;

      divVideo0 = document.querySelectorAll("div.playing-mode video:not([paused-by-script])");
      if (divVideo0 != null) {
        for (i = 0; i < divVideo0.length; i++) {
          divVideo0[i].pause();
          divVideo0[i].setAttribute("paused-by-script", "i1");
          vCount0++;
        }
      }

      divVideo1 = document.querySelectorAll("div.playing-mode video[paused-by-script='i1']");
      if (divVideo1 != null) {
        for (i = 0; i < divVideo1.length; i++) {
          divVideo1[i].pause();
          vCount1++;
        }
      }

      if (vCount0 == 0 && vCount1 > 0) clearInterval(waitVideo);

    }, 100);
  }

})();