Play Single Video Only

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Play Single Video Only
// @namespace   UserScript
// @match       https://www.youtube.com/*
// @match       https://www.facebook.com/*
// @version     1.2
// @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==

(() => {

  const ways = {
    'www.youtube.com': {
      findVideo: () => {

        return document.querySelector('#movie_player video');

      },
      testVideo: (video) => {

        if (video.volume > 1e-5 && video.muted === false) {

        } else {
          return false;
        }

        if (HTMLElement.prototype.matches.call(video, '#movie_player video')) return true;

      }
    },
    'www.facebook.com': {
      findVideo: () => {

        return [...document.querySelectorAll('#watch_feed video')].filter(elm => elm.paused === false)[0]

      },
      testVideo: (video) => {

        if (video.volume > 1e-5 && video.muted === false) {

        } else {
          return false;
        }

        if (HTMLElement.prototype.matches.call(video, '#watch_feed video')) return true;

      }
    }

  };

  const { findVideo, testVideo } = ways[location.hostname];

  if (typeof findVideo !== 'function' || typeof testVideo !== 'function') return;


  GM_addValueChangeListener("t9opC", function (key, oldValue, newValue, remote) {
    if (remote) {
      let video = findVideo();
      if (video && video.paused === false && video.volume > 1e-5 && video.muted === false && testVideo(video)) {
        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 (testVideo(target)) {
        GM.setValue('t9opC', Date.now());
      }
    }
  }, true);

})();