pf / twfix

// ==UserScript==
// @name         twfix
// @namespace    usnspf
// @version      0.0.2.3
// @description  auto-pause bg twitch-player; +
// @author       pf
// @homepage     https://pastebin.com/LAtyWpTT
// @match        https://www.twitch.tv/*
// @grant        none
// @copyright 2021, pf (https://pastebin.com/LAtyWpTT)
// @license MIT
// @updateURL https://openuserjs.org/meta/pf/twfix.meta.js
// @downloadURL https://openuserjs.org/install/pf/twfix.min.user.js
// ==/UserScript==

(function() {
    'use strict';

    var tw_blocks = {
		"h_live" : {"set":false,"upd":false,},
		"h_car" : {"set":false,"upd":false,},
		"this_h" : "",
	},
        tw_f_rt=t=>{
            setTimeout(() => (function(){
                if(t){
                    document.title = t;
                }
            })() , 2000);
        };

    setInterval(() => (function(){
        var tw_here_p ;
		
		tw_blocks["h_live"].set = !!document.querySelector(".home-live-player-overlay");
		tw_blocks["h_car"].set = !!document.querySelector(".home-carousel-info");
		
		if(tw_blocks["h_live"].set){
			tw_blocks["this_h"] = "h_live";	}
		else if(tw_blocks["h_car"].set){
			tw_blocks["this_h"] = "h_car";}
		else {
			tw_blocks["this_h"] = "";}		
		
        if( tw_here_p = document.querySelector(".video-player video") ){


            if(tw_here_p.duration > 0 && !tw_here_p.paused ){
                if(tw_blocks["this_h"] && !tw_blocks[ tw_blocks["this_h"] ].upd ){
					
					tw_blocks[ tw_blocks["this_h"] ].upd = true;

                    tw_f_rt(document.title);
                    document.title = "⏸️ "+document.title;

                    tw_here_p.pause();
                }
            }
            else {
                if((tw_blocks["h_live"].upd || tw_blocks["h_car"].upd) && !tw_blocks["this_h"]) {
                    
					tw_blocks["h_live"].upd = false;
					tw_blocks["h_car"].upd = false;

                    tw_f_rt(document.title);
                    document.title = "▶️ "+document.title;

                    tw_here_p.play();
                }
            }
        }

    })() , 3000);

})();