LatBel

Userscript for audio background (YouTube and YouTube music)

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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);
})();