LatBel

Userscript for audio background (YouTube and YouTube music)

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

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

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