YouTube: Play Single Video Only

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

当前为 2023-07-11 提交的版本,查看 最新版本

// ==UserScript==
// @name        YouTube: Play Single Video Only
// @namespace   UserScript
// @match       https://www.youtube.com/*
// @version     1.0
// @license     MIT
// @author      CY Fung
// @description Pause your background video when you play another video in another tab.
// @run-at      document-start
// @grant       GM_addValueChangeListener
// @grant       GM.setValue
// ==/UserScript==

(() => {

  let listenerId = GM_addValueChangeListener("t9opC", function (key, oldValue, newValue, remote) {
    if (remote) {
      let video = document.querySelector('#movie_player video');
      if (video && video.paused === false) {
        HTMLVideoElement.prototype.pause.call(video);
      }
    }
  });


  document.addEventListener('play', function (evt) {

    if (!evt || !evt.isTrusted) return;
    const target = (evt || 0).target;
    if (target instanceof HTMLVideoElement) {
      if (HTMLElement.prototype.matches.call(target, '#movie_player video')) {
        GM.setValue('t9opC', Date.now());
      }
    }
  }, true);

})();