自动全屏,首先必须阅读描述中的使用方式,可在github留言反馈或者自行增加视频平台,理论上所有视频网页都使用

给家里人用的自动全屏,但是浏览器安全策略无法在油猴脚本自动全屏,只能手动键入F全屏,所以需要搭配python,实现自动按下F,python脚本链接:github.com/zsanjin-p/listen-to-f 运行python脚本,然后安装本油猴JS脚本,访问视频链接后,加载10秒后自动全屏,如果没有,可能是网页加载过慢了,可在github留言反馈或者自行增加视频平台,理论上所有视频网页都使用

// ==UserScript==
// @name         自动全屏,首先必须阅读描述中的使用方式,可在github留言反馈或者自行增加视频平台,理论上所有视频网页都使用
// @namespace    http://tampermonkey.net/
// @version      1.02
// @description  给家里人用的自动全屏,但是浏览器安全策略无法在油猴脚本自动全屏,只能手动键入F全屏,所以需要搭配python,实现自动按下F,python脚本链接:github.com/zsanjin-p/listen-to-f   运行python脚本,然后安装本油猴JS脚本,访问视频链接后,加载10秒后自动全屏,如果没有,可能是网页加载过慢了,可在github留言反馈或者自行增加视频平台,理论上所有视频网页都使用
// @author       zsanjin
// @match        https://xiaoxintv.cc/index.php/vod/play/id/*
// @license      BSD-2-Clause
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
 
    // 添加请求计数器
    let requestCount = 0;
    const MAX_REQUESTS = 1;
 
    function log(message, type = 'info') {
        const timestamp = new Date().toISOString();
        const prefix = `[Fullscreen Script ${timestamp}]`;
        switch(type) {
            case 'error':
                console.error(prefix, message);
                break;
            case 'warn':
                console.warn(prefix, message);
                break;
            default:
                console.log(prefix, message);
        }
    }
 
    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.onmouseout = () => button.style.background = '#4CAF50';
        
        button.onclick = () => handleFullscreenAction();
 
        document.body.appendChild(button);
    }
 
    async function sendFKeySignal() {
        if (requestCount >= MAX_REQUESTS) {
            log(`已达到最大请求次数限制 (${MAX_REQUESTS}次)`, 'warn');
            return null;
        }
 
        log(`准备发送F键信号 (第 ${requestCount + 1}/${MAX_REQUESTS} 次)`);
        
        try {
            const response = await fetch('http://localhost:2716/trigger-f', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Accept': 'application/json',
                    'Origin': window.location.origin
                },
                body: JSON.stringify({
                    action: 'press_f',
                    timestamp: new Date().toISOString()
                })
            });
 
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
 
            requestCount++;
            const data = await response.json();
            log(`服务器响应成功: ${JSON.stringify(data)}`);
            return data;
        } catch (error) {
            log(`发送F键信号失败: ${error.message}`, 'error');
            requestCount++;
            return null;
        }
    }
 
    function simulateFKeyPress() {
        log('模拟按下F键');
        const events = ['keydown', 'keypress', 'keyup'];
        events.forEach(eventType => {
            const event = new KeyboardEvent(eventType, {
                key: 'f',
                code: 'KeyF',
                keyCode: 70,
                which: 70,
                bubbles: true,
                cancelable: true
            });
            document.dispatchEvent(event);
        });
    }
 
    function handlePlayPause() {
        log('处理播放/暂停');
        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');
                
                if (videoElement) {
                    if (videoElement.paused) {
                        videoElement.play();
                        log('视频开始播放');
                    } else {
                        videoElement.pause();
                        log('视频已暂停');
                    }
                    return true;
                }
            } catch (e) {
                log(`播放/暂停操作失败: ${e}`, 'error');
            }
        }
        return false;
    }
 
    async function tryFullscreen() {
        log('尝试进入全屏模式');
        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) {
                    log('找到视频元素和全屏按钮');
                    
                    // 点击全屏按钮
                    fullscreenButton.click();
                    
                    // 等待更长时间让视频播放器的全屏逻辑完成
                    await new Promise(resolve => setTimeout(resolve, 1000));
                    
                    // 检查是否已经进入全屏模式
                    const isFullscreen = document.fullscreenElement || 
                                       document.webkitFullscreenElement || 
                                       document.mozFullScreenElement || 
                                       document.msFullscreenElement;
                    
                    // 只有在没有任何元素处于全屏状态时才尝试请求文档全屏
                    if (!isFullscreen) {
                        log('视频全屏失败,尝试文档全屏');
                        try {
                            if (videoElement.requestFullscreen) {
                                await videoElement.requestFullscreen();
                            } else if (videoElement.webkitRequestFullscreen) {
                                await videoElement.webkitRequestFullscreen();
                            } else if (videoElement.mozRequestFullScreen) {
                                await videoElement.mozRequestFullScreen();
                            } else if (videoElement.msRequestFullscreen) {
                                await videoElement.msRequestFullscreen();
                            }
                        } catch (err) {
                            log(`视频元素全屏失败,尝试文档全屏: ${err.message}`, 'warn');
                            // 最后才尝试文档全屏
                            try {
                                await document.documentElement.requestFullscreen();
                            } catch (docErr) {
                                log(`文档全屏也失败了: ${docErr.message}`, 'error');
                            }
                        }
                    }
                    return true;
                }
            } catch (e) {
                log(`在iframe中请求全屏失败: ${e}`, 'error');
            }
        }
        return false;
    }
 
    async function handleFullscreenAction() {
        if (requestCount >= MAX_REQUESTS) {
            log('已达到最大请求次数限制,不再执行全屏操作', 'warn');
            return;
        }
 
        const button = document.getElementById('easy-fullscreen-button');
        if (button) {
            button.innerHTML = '⌛ 正在设置全屏...';
        }
        
        try {
            await sendFKeySignal();
            simulateFKeyPress();
            await tryFullscreen();
            
            if (button) {
                setTimeout(() => {
                    button.style.opacity = '0';
                    setTimeout(() => button.style.display = 'none', 1000);
                }, 3000);
            }
        } catch (error) {
            log(`全屏设置失败: ${error.message}`, 'error');
            if (button) {
                button.innerHTML = '❌ 全屏设置失败,请重试';
                button.style.background = '#ff4444';
                
                setTimeout(() => {
                    button.innerHTML = '📺 点击这里/或按F全屏播放,按空格播放/暂停,退出按Esc';
                    button.style.background = '#4CAF50';
                }, 3000);
            }
        }
    }
 
    function init() {
        log('脚本初始化开始');
        try {
            createEnhancedButton();
            
            // 监听键盘事件
            document.addEventListener('keydown', (e) => {
                // 防止事件冒泡和默认行为
                if (e.key === ' ' || e.key === 'f' || e.key === 'F') {
                    e.preventDefault();
                }
                
                if (e.key === 'f' || e.key === 'F') {
                    log('检测到F键被按下');
                    handleFullscreenAction();
                } else if (e.key === ' ') {
                    log('检测到空格键被按下');
                    handlePlayPause();
                }
            });
            
            // 8秒后自动触发全屏
            setTimeout(() => {
                log('8秒时间到,自动触发全屏');
                handleFullscreenAction();
            }, 8000);
            
            log('脚本初始化完成');
        } catch (error) {
            log(`初始化失败: ${error.message}`, 'error');
        }
    }
 
    // 等待页面加载完成后初始化
    if (document.readyState === 'complete') {
        init();
    } else {
        window.addEventListener('load', init);
    }
})();