自动全屏,但是目前因为浏览器安全限制(只能手动。。。)

给家里人用的自动全屏,但是浏览器安全策略无法在油猴脚本自动全屏,只能手动键入F全屏

目前为 2025-01-27 提交的版本。查看 最新版本

// ==UserScript==
// @name         自动全屏,但是目前因为浏览器安全限制(只能手动。。。)
// @namespace    http://tampermonkey.net/
// @version      0.132
// @description  给家里人用的自动全屏,但是浏览器安全策略无法在油猴脚本自动全屏,只能手动键入F全屏
// @author       zsanjin
// @match        https://xiaoxintv.cc/index.php/vod/play/id/*
// @license      BSD-2-Clause
// @grant        none
// ==/UserScript==

(function() {
'use strict';
 
function createEnhancedButton() {
    const button = document.createElement('button');
    button.id = 'easy-fullscreen-button';
    button.innerHTML = '📺 点击这里/或按F全屏播放,退出按Esc';
    button.style.cssText = `
        position: fixed;
        top: 20px;
        right: 20px;
        z-index: 999999;
        padding: 15px 25px;
        background: #4CAF50;
        color: white;
        border: none;
        border-radius: 8px;
        cursor: pointer;
        font-size: 18px;
        font-weight: bold;
        box-shadow: 0 4px 8px rgba(0,0,0,0.2);
        transition: all 0.3s ease;
    `;
 
    button.onmouseover = () => {
        button.style.background = '#45a049';
        button.style.transform = 'scale(1.05)';
    };
    button.onmouseout = () => {
        button.style.background = '#4CAF50';
        button.style.transform = 'scale(1)';
    };
 
    button.onmousedown = () => {
        button.style.transform = 'scale(0.95)';
    };
    button.onmouseup = () => {
        button.style.transform = 'scale(1)';
    };
 
    button.onclick = async () => {
        button.innerHTML = '⌛ 正在设置全屏...';
        await tryFullscreen();
        setTimeout(() => {
            button.style.opacity = '0';
            setTimeout(() => button.style.display = 'none', 1000);
        }, 3000);
    };
 
    document.body.appendChild(button);
}
 
async function tryFullscreen() {
    const iframes = Array.from(document.querySelectorAll('iframe'))
        .filter(iframe => iframe.src.includes('player'));
 
    for (const iframe of iframes) {
        try {
            const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
            const videoElement = iframeDoc.querySelector('.yzmplayer-video');
            const fullscreenButton = iframeDoc.querySelector('.yzmplayer-full-icon');
 
            if (videoElement && fullscreenButton) {
                // 模拟点击播放器的全屏按钮
                fullscreenButton.click();
 
                // 使用 setTimeout 延迟模拟用户点击全屏按钮
                setTimeout(async () => {
                    try {
                        await videoElement.requestFullscreen();
                    } catch (e) {
                        console.log('全屏请求失败,尝试其他方法');
                        if (videoElement.webkitRequestFullscreen) {
                            await videoElement.webkitRequestFullscreen();
                        } else if (videoElement.mozRequestFullScreen) {
                            await videoElement.mozRequestFullScreen();
                        } else if (videoElement.msRequestFullscreen) {
                            await videoElement.msRequestFullscreen();
                        }
                    }
                }, 1000); // 延迟1秒尝试全屏
 
                return true;
            }
        } catch (e) {
            console.log('在iframe中请求全屏失败:', e);
        }
    }
    return false;
}
 
// 模拟用户行为请求全屏
function autoFullscreen() {
    setTimeout(async () => {
        const success = await tryFullscreen();
        if (!success) {
            console.log('自动全屏失败,重试中...');
            autoFullscreen(); // 重试
        }
    }, 10000); // 延迟10秒后尝试
}
 
// 初始化
function init() {
    createEnhancedButton();
 
    // 页面加载后10秒自动全屏
    autoFullscreen();
 
    // 添加键盘快捷键支持
    document.addEventListener('keydown', (e) => {
        if (e.key === 'f' || e.key === 'F') {
            tryFullscreen();
        }
    });
}
 
// 当页面加载完成后初始化
if (document.readyState === 'complete') {
    init();
} else {
    window.addEventListener('load', init);
}
})();