LatBel

Userscript for audio background (YouTube and YouTube music)

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==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);
})();