Greasy Fork is available in English.

YT pause video on comment

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

  1. // ==UserScript==
  2. // @name YT pause video on comment
  3. // @description Youtube video is paused if you have opened the url with link to comment
  4. // @author MK
  5. // @namespace max44
  6. // @homepage https://greasyfork.org/en/users/309172-max44
  7. // @match *://*.youtube.com/*
  8. // @match *://*.youtu.be/*
  9. // @icon https://cdn.icon-icons.com/icons2/1488/PNG/512/5295-youtube-i_102568.png
  10. // @version 1.0
  11. // @license MIT
  12. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  13. // @run-at document-end
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18.  
  19. var urlAtLastCheck = "";
  20. var divVideo0 = null;
  21. var divVideo1 = null;
  22. var waitVideo;
  23.  
  24. //Check URL changes
  25. const rootCallback = function (mutationsList, observer) {
  26. if (urlAtLastCheck != document.location.href) {
  27. urlAtLastCheck = document.location.href;
  28. clearInterval(waitVideo);
  29. if (urlAtLastCheck.search("/watch?") > 0 && urlAtLastCheck.search("&lc=") > 0) {
  30. pauseVideo();
  31. }
  32. }
  33. }
  34.  
  35. const rootNode = document.querySelector("body");
  36. if (rootNode != null) {
  37. const rootObserver = new MutationObserver(rootCallback);
  38. rootObserver.observe(rootNode, {childList: true, subtree: true, attributes: true, characterData: false});
  39. }
  40.  
  41. function pauseVideo() {
  42. waitVideo = setInterval(function() {
  43. var vCount0 = 0;
  44. var vCount1 = 0;
  45. var i;
  46.  
  47. divVideo0 = document.querySelectorAll("div.playing-mode video:not([paused-by-script])");
  48. if (divVideo0 != null) {
  49. for (i = 0; i < divVideo0.length; i++) {
  50. divVideo0[i].pause();
  51. divVideo0[i].setAttribute("paused-by-script", "i1");
  52. vCount0++;
  53. }
  54. }
  55.  
  56. divVideo1 = document.querySelectorAll("div.playing-mode video[paused-by-script='i1']");
  57. if (divVideo1 != null) {
  58. for (i = 0; i < divVideo1.length; i++) {
  59. divVideo1[i].pause();
  60. vCount1++;
  61. }
  62. }
  63.  
  64. if (vCount0 == 0 && vCount1 > 0) clearInterval(waitVideo);
  65.  
  66. }, 100);
  67. }
  68.  
  69. })();