🏠 返回首頁 

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. (function() {
  16. 'use strict';
  17. var urlAtLastCheck = "";
  18. var divVideo0 = null;
  19. var divVideo1 = null;
  20. var waitVideo;
  21. //Check URL changes
  22. const rootCallback = function (mutationsList, observer) {
  23. if (urlAtLastCheck != document.location.href) {
  24. urlAtLastCheck = document.location.href;
  25. clearInterval(waitVideo);
  26. if (urlAtLastCheck.search("/watch?") > 0 && urlAtLastCheck.search("&lc=") > 0) {
  27. pauseVideo();
  28. }
  29. }
  30. }
  31. const rootNode = document.querySelector("body");
  32. if (rootNode != null) {
  33. const rootObserver = new MutationObserver(rootCallback);
  34. rootObserver.observe(rootNode, {childList: true, subtree: true, attributes: true, characterData: false});
  35. }
  36. function pauseVideo() {
  37. waitVideo = setInterval(function() {
  38. var vCount0 = 0;
  39. var vCount1 = 0;
  40. var i;
  41. divVideo0 = document.querySelectorAll("div.playing-mode video:not([paused-by-script])");
  42. if (divVideo0 != null) {
  43. for (i = 0; i < divVideo0.length; i++) {
  44. divVideo0[i].pause();
  45. divVideo0[i].setAttribute("paused-by-script", "i1");
  46. vCount0++;
  47. }
  48. }
  49. divVideo1 = document.querySelectorAll("div.playing-mode video[paused-by-script='i1']");
  50. if (divVideo1 != null) {
  51. for (i = 0; i < divVideo1.length; i++) {
  52. divVideo1[i].pause();
  53. vCount1++;
  54. }
  55. }
  56. if (vCount0 == 0 && vCount1 > 0) clearInterval(waitVideo);
  57. }, 100);
  58. }
  59. })();