Greasy Fork is available in English.

YouTube Wait for Me - Pause and Start Player in Background Tabs

Pause the video in YouTube tabs opened in the background, play on activation 2016-08-18 (I know, it doesn't pause fast enough)

  1. // ==UserScript==
  2. // @name YouTube Wait for Me - Pause and Start Player in Background Tabs
  3. // @author Jefferson "jscher2000" Scher
  4. // @namespace JeffersonScher
  5. // @version 0.7
  6. // @copyright Copyright 2016 Jefferson Scher
  7. // @license BSD 3-clause
  8. // @description Pause the video in YouTube tabs opened in the background, play on activation 2016-08-18 (I know, it doesn't pause fast enough)
  9. // @include http*://www.youtube.com/*
  10. // @grant none
  11. // ==/UserScript==
  12. // See: https://developer.mozilla.org/docs/Web/API/Page_Visibility_API
  13.  
  14. var videoElement = document.querySelector('#movie_player video');
  15. if (videoElement){
  16. if (document["hidden"]) { // background tab
  17. // Mute the player ASAP
  18. videoElement.muted = true;
  19. // Pause the player
  20. videoElement.pause();
  21. // Seek to the beginning
  22. videoElement.currentTime = 0;
  23. // Set up event handler to watch for tab becoming visible
  24. document.addEventListener("visibilitychange", handleVisibilityChange, false);
  25. }
  26. }
  27.  
  28. function handleVisibilityChange() {
  29. if (document["hidden"]) {
  30. // Pause the player
  31. videoElement.pause();
  32. } else {
  33. // Resume the video after a quarter second
  34. window.setTimeout(function(){videoElement.play()}, 250);
  35. videoElement.muted = false;
  36. // We're not going to pause again, so remove event listener
  37. document.removeEventListener("visibilitychange", handleVisibilityChange, false);
  38. }
  39. }