LatBel

Userscript for audio background (YouTube and YouTube music)

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         LatBel
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  Userscript for audio background (YouTube and YouTube music)
// @author       dehacking_guy
// @license      MIT
// @match        *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // 1. Block tab switching detection completely
    Object.defineProperty(document, 'hidden', {value: false, writable: false});
    Object.defineProperty(document, 'visibilityState', {value: 'visible', writable: false});

    const blockEvent = (event) => event.stopImmediatePropagation();
    ['visibilitychange', 'webkitvisibilitychange', 'pagehide', 'blur'].forEach(evt => {
        window.addEventListener(evt, blockEvent, true);
    });

    // 2. Aggressive Playback Enforcement
    document.addEventListener('pause', (event) => {
        const target = event.target;
        
        if (target && target.tagName === 'VIDEO') {
            if (!document.hasFocus()) {
                
                let attempts = 0;
                // Try to force play every 500ms
                const forcePlay = setInterval(() => {
                    attempts++;
                    target.play().then(() => {
                        // If successful, stop the loop
                        clearInterval(forcePlay); 
                    }).catch(err => {
                        console.warn('Screen-off auto-play blocked by OS:', err);
                    });

                    // Give up after 10 attempts (5 seconds) to prevent crashing the page
                    if (attempts >= 10) {
                        clearInterval(forcePlay);
                    }
                }, 500);
            }
        }
    }, true);
})();