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

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

目前為 2025-01-28 提交的版本,檢視 最新版本

// ==UserScript==
// @name         自动全屏,首先必须阅读描述中的使用方式,可在github留言反馈或者自行增加视频平台,理论上所有视频网页都使用
// @namespace    http://tampermonkey.net/
// @version      1.0
// @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);
        });
    }
 
    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();
                    
                    try {
                        await new Promise(resolve => setTimeout(resolve, 500));
                        await document.documentElement.requestFullscreen();
                        return true;
                    } catch (e) {
                        log('全屏请求失败,尝试其他方法', 'warn');
                        const methods = [
                            'webkitRequestFullscreen',
                            'mozRequestFullScreen',
                            'msRequestFullscreen'
                        ];
                        
                        for (const method of methods) {
                            if (videoElement[method]) {
                                try {
                                    await videoElement[method]();
                                    return true;
                                } catch (err) {
                                    log(`${method} 失败: ${err.message}`, 'warn');
                                }
                            }
                        }
                    }
                }
            } 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();
            
            // 监听F键
            document.addEventListener('keydown', (e) => {
                if (e.key === 'f' || e.key === 'F') {
                    log('检测到F键被按下');
                    handleFullscreenAction();
                }
            });
            
            // 8秒后自动触发全屏
            setTimeout(() => {
                log('8秒时间到,自动触发全屏');
                handleFullscreenAction();
            }, 8000);
            
            log('脚本初始化完成');
        } catch (error) {
            log(`初始化失败: ${error.message}`, 'error');
        }
    }
 
    // 等待页面加载完成后初始化
    if (document.readyState === 'complete') {
        init();
    } else {
        window.addEventListener('load', init);
    }
})();