Pause/Mute HTML5 Audio/Video On Leaving Tab

Pause or mute HTML5 audio/video on leaving a tab and restore them back on returning.

As of 2020-03-27. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Pause/Mute HTML5 Audio/Video On Leaving Tab
// @namespace   PauseMuteHTML5AudioVideoAudioOnLeavingTab
// @version     1.0.3
// @license     AGPL v3
// @author      jcunews
// @description Pause or mute HTML5 audio/video on leaving a tab and restore them back on returning.
// @website     https://greatest.deepsurf.us/en/users/85671-jcunews
// @include     *://*/*
// @grant       none
// ==/UserScript==

(() => {

  //=== Configuration Start ===

  var muteInsteadOfPause = false; //set to `true` to mute instead of pause

  //=== Configuration End ===
  
  var sHidden, sVisibilityChange, elements;
  
  if ("undefined" !== typeof document.hidden) {
    sHidden = "hidden";
    sVisibilityChange = "visibilitychange";
  } else if ("undefined" !== typeof document.webkitHidden) {
    sHidden = "webkitHidden";
    sVisibilityChange = "webkitvisibilitychange";
  } else if ("undefined" !== typeof document.msHidden) {
    sHidden = "msHidden";
    sVisibilityChange = "msvisibilitychange";
  }
  
  function checkStatus() {
    if (!document[sHidden]) {
      if (elements) {
        elements.forEach(function(v) {
          if (muteInsteadOfPause) {
            v.muted = false;
          } else v.play();
        });
      }
    } else {
      elements = Array.prototype.slice.call(document.querySelectorAll("audio, video")).filter(
        function(v) {
          if (muteInsteadOfPause) {
            if (!v.muted) {
              v.muted = true;
              return true;
            }
          } else if (!v.paused) {
            v.pause();
            return true;
          }
          return false;
        }
      );
    }
  }
  
  function init() {
    document.removeEventListener(sVisibilityChange, checkStatus);
    document.addEventListener(sVisibilityChange, checkStatus);
  }
  
  //Support for Structured Page Fragments. For e.g. YouTube
  addEventListener("spfdone", init);
  
  init();
})();