Play Single Video Only

Pause your background video when you play another video in another tab.

  1. // ==UserScript==
  2. // @name Play Single Video Only
  3. // @namespace UserScript
  4. // @match https://www.youtube.com/*
  5. // @match https://www.facebook.com/*
  6. // @version 1.2
  7. // @license MIT
  8. // @author CY Fung
  9. // @description Pause your background video when you play another video in another tab.
  10. // @run-at document-start
  11. // @grant GM_addValueChangeListener
  12. // @grant GM.setValue
  13. // ==/UserScript==
  14.  
  15. (() => {
  16.  
  17. const ways = {
  18. 'www.youtube.com': {
  19. findVideo: () => {
  20.  
  21. return document.querySelector('#movie_player video');
  22.  
  23. },
  24. testVideo: (video) => {
  25.  
  26. if (video.volume > 1e-5 && video.muted === false) {
  27.  
  28. } else {
  29. return false;
  30. }
  31.  
  32. if (HTMLElement.prototype.matches.call(video, '#movie_player video')) return true;
  33.  
  34. }
  35. },
  36. 'www.facebook.com': {
  37. findVideo: () => {
  38.  
  39. return [...document.querySelectorAll('#watch_feed video')].filter(elm => elm.paused === false)[0]
  40.  
  41. },
  42. testVideo: (video) => {
  43.  
  44. if (video.volume > 1e-5 && video.muted === false) {
  45.  
  46. } else {
  47. return false;
  48. }
  49.  
  50. if (HTMLElement.prototype.matches.call(video, '#watch_feed video')) return true;
  51.  
  52. }
  53. }
  54.  
  55. };
  56.  
  57. const { findVideo, testVideo } = ways[location.hostname];
  58.  
  59. if (typeof findVideo !== 'function' || typeof testVideo !== 'function') return;
  60.  
  61.  
  62. GM_addValueChangeListener("t9opC", function (key, oldValue, newValue, remote) {
  63. if (remote) {
  64. let video = findVideo();
  65. if (video && video.paused === false && video.volume > 1e-5 && video.muted === false && testVideo(video)) {
  66. HTMLVideoElement.prototype.pause.call(video);
  67. }
  68. }
  69. });
  70.  
  71. document.addEventListener('play', function (evt) {
  72.  
  73. if (!evt || !evt.isTrusted) return;
  74. const target = (evt || 0).target;
  75. if (target instanceof HTMLVideoElement) {
  76. if (testVideo(target)) {
  77. GM.setValue('t9opC', Date.now());
  78. }
  79. }
  80. }, true);
  81.  
  82. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址