Twitch Auto Pause/Play Toggle for Tab Switch

Pauses / Plays Video When Mouse Leaves Focus of Tab

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name          Twitch Auto Pause/Play Toggle for Tab Switch
// @namespace     http://userstyles.org
// @description   Pauses / Plays Video When Mouse Leaves Focus of Tab
// @author        636597
// @include       *://*.twitch.tv/*
// @run-at        document-start
// @version       0.6
// ==/UserScript==


var PLAYING = true;
var PAUSE_PLAY_BUTTON_ELEMENT = false;
var CHECKING_FOCUS = true;
var BUTTON_ELEMENT = false;

function toggleCheckFocus() {
	if ( CHECKING_FOCUS ) {
		console.log( "Stopping Observing the Focus" );
		clearObserver();
		if ( !PLAYING ) {
			PAUSE_PLAY_BUTTON_ELEMENT.click();
			PLAYING = true;
		}
		BUTTON_ELEMENT.innerHTML = "Observe Focus";
	}
	else {
		console.log( "Starting Observing the Focus" );
		loadObserver();
		BUTTON_ELEMENT.innerHTML = "Stop Observing Focus";
	}
	CHECKING_FOCUS = !CHECKING_FOCUS;
}
function addToggleCheckFocusButton() {
	//var title_element = document.querySelector( 'h2[data-a-target="stream-title"]' );
	var title_element = document.querySelector( 'div[data-test-selector="chat-input-buttons-container"' )
	if ( !title_element ) { return; }
	var id = "toggle-check-focus";
	var element_string = '<button id="' + id + '">Stop Observing Focus</button>';
	var template = document.createElement( "template" );
	template.innerHTML = element_string;
	title_element.insertBefore( template.content ,  title_element.childNodes[ title_element.childNodes.length - 1 ] );
	BUTTON_ELEMENT = document.body.querySelector( "#" + id );
	BUTTON_ELEMENT.addEventListener( "click" , function( event ) {
		toggleCheckFocus();
	});
}

// function checkFocus() {
// 	if ( document.hasFocus() ) {
// 		// console.log( "tab enter" );
// 		// console.log( "PLAYING === " + PLAYING );
// 		if ( !PLAYING ) {
// 			console.log( "Tab Entered: Toggling Play State" );
// 			PAUSE_PLAY_BUTTON_ELEMENT.click();
// 			PLAYING = true;
// 		}
// 	} else {
// 		// console.log( "tab leave" );
// 		// console.log( "PLAYING === " + PLAYING );
// 		if ( PLAYING ) {
// 			console.log( "Tab Left: Toggling Play State" );
// 			PAUSE_PLAY_BUTTON_ELEMENT.click();
// 			PLAYING = false;
// 		}
// 	}
// }

// Maybe It Be Converted to Observer Somehow
var observer_interval = false;
function clearObserver() {
	//clearInterval( observer_interval );
	document.removeEventListener( "mouseout" , mouseout_event_listener );
}
function mouseout_event_listener( e ) {
	e = e ? e : window.event;
	var from = e.relatedTarget || e.toElement;
	if ( !from || from.nodeName == "HTML" ) {
		if ( PLAYING ) {
			console.log( "Mouse Out: Toggling Play State" );
			PAUSE_PLAY_BUTTON_ELEMENT.click();
			PLAYING = false;
		}
	}
	else {
		if ( !PLAYING ) {
			console.log( "Mouse Entered: Toggling Play State" );
			PAUSE_PLAY_BUTTON_ELEMENT.click();
			PLAYING = true;
		}
	}
}
function loadObserver() {
	console.log( PAUSE_PLAY_BUTTON_ELEMENT );
	if ( !PAUSE_PLAY_BUTTON_ELEMENT ) { return; }
	//observer_interval = setInterval( checkFocus , 1000 );
	document.addEventListener( "mouseout" , mouseout_event_listener );
}

// Init
(function() {
	var ready = setInterval(function(){
		PAUSE_PLAY_BUTTON_ELEMENT = document.querySelector( 'button[data-a-target="player-play-pause-button"]' );
		if ( !PAUSE_PLAY_BUTTON_ELEMENT ) { return; }
		clearInterval( ready );
		addToggleCheckFocusButton();
		loadObserver();
	} , 2 );
	setTimeout( function() {
		clearInterval( ready );
	} , 10000 );
})();