Twitch Auto Pause/Play Toggle for Tab Switch

Pauses / Plays Video When Mouse Leaves Focus of Tab

  1. // ==UserScript==
  2. // @name Twitch Auto Pause/Play Toggle for Tab Switch
  3. // @namespace http://userstyles.org
  4. // @description Pauses / Plays Video When Mouse Leaves Focus of Tab
  5. // @author 636597
  6. // @include *://*.twitch.tv/*
  7. // @run-at document-start
  8. // @version 0.6
  9. // ==/UserScript==
  10.  
  11.  
  12. var PLAYING = true;
  13. var PAUSE_PLAY_BUTTON_ELEMENT = false;
  14. var CHECKING_FOCUS = true;
  15. var BUTTON_ELEMENT = false;
  16.  
  17. function toggleCheckFocus() {
  18. if ( CHECKING_FOCUS ) {
  19. console.log( "Stopping Observing the Focus" );
  20. clearObserver();
  21. if ( !PLAYING ) {
  22. PAUSE_PLAY_BUTTON_ELEMENT.click();
  23. PLAYING = true;
  24. }
  25. BUTTON_ELEMENT.innerHTML = "Observe Focus";
  26. }
  27. else {
  28. console.log( "Starting Observing the Focus" );
  29. loadObserver();
  30. BUTTON_ELEMENT.innerHTML = "Stop Observing Focus";
  31. }
  32. CHECKING_FOCUS = !CHECKING_FOCUS;
  33. }
  34. function addToggleCheckFocusButton() {
  35. //var title_element = document.querySelector( 'h2[data-a-target="stream-title"]' );
  36. var title_element = document.querySelector( 'div[data-test-selector="chat-input-buttons-container"' )
  37. if ( !title_element ) { return; }
  38. var id = "toggle-check-focus";
  39. var element_string = '<button id="' + id + '">Stop Observing Focus</button>';
  40. var template = document.createElement( "template" );
  41. template.innerHTML = element_string;
  42. title_element.insertBefore( template.content , title_element.childNodes[ title_element.childNodes.length - 1 ] );
  43. BUTTON_ELEMENT = document.body.querySelector( "#" + id );
  44. BUTTON_ELEMENT.addEventListener( "click" , function( event ) {
  45. toggleCheckFocus();
  46. });
  47. }
  48.  
  49. // function checkFocus() {
  50. // if ( document.hasFocus() ) {
  51. // // console.log( "tab enter" );
  52. // // console.log( "PLAYING === " + PLAYING );
  53. // if ( !PLAYING ) {
  54. // console.log( "Tab Entered: Toggling Play State" );
  55. // PAUSE_PLAY_BUTTON_ELEMENT.click();
  56. // PLAYING = true;
  57. // }
  58. // } else {
  59. // // console.log( "tab leave" );
  60. // // console.log( "PLAYING === " + PLAYING );
  61. // if ( PLAYING ) {
  62. // console.log( "Tab Left: Toggling Play State" );
  63. // PAUSE_PLAY_BUTTON_ELEMENT.click();
  64. // PLAYING = false;
  65. // }
  66. // }
  67. // }
  68.  
  69. // Maybe It Be Converted to Observer Somehow
  70. var observer_interval = false;
  71. function clearObserver() {
  72. //clearInterval( observer_interval );
  73. document.removeEventListener( "mouseout" , mouseout_event_listener );
  74. }
  75. function mouseout_event_listener( e ) {
  76. e = e ? e : window.event;
  77. var from = e.relatedTarget || e.toElement;
  78. if ( !from || from.nodeName == "HTML" ) {
  79. if ( PLAYING ) {
  80. console.log( "Mouse Out: Toggling Play State" );
  81. PAUSE_PLAY_BUTTON_ELEMENT.click();
  82. PLAYING = false;
  83. }
  84. }
  85. else {
  86. if ( !PLAYING ) {
  87. console.log( "Mouse Entered: Toggling Play State" );
  88. PAUSE_PLAY_BUTTON_ELEMENT.click();
  89. PLAYING = true;
  90. }
  91. }
  92. }
  93. function loadObserver() {
  94. console.log( PAUSE_PLAY_BUTTON_ELEMENT );
  95. if ( !PAUSE_PLAY_BUTTON_ELEMENT ) { return; }
  96. //observer_interval = setInterval( checkFocus , 1000 );
  97. document.addEventListener( "mouseout" , mouseout_event_listener );
  98. }
  99.  
  100. // Init
  101. (function() {
  102. var ready = setInterval(function(){
  103. PAUSE_PLAY_BUTTON_ELEMENT = document.querySelector( 'button[data-a-target="player-play-pause-button"]' );
  104. if ( !PAUSE_PLAY_BUTTON_ELEMENT ) { return; }
  105. clearInterval( ready );
  106. addToggleCheckFocusButton();
  107. loadObserver();
  108. } , 2 );
  109. setTimeout( function() {
  110. clearInterval( ready );
  111. } , 10000 );
  112. })();

QingJ © 2025

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