自动全屏Auto Fullscreen for xiaoxintv

给家里人用的自动全屏,Automatically enters fullscreen mode after 10 seconds on xiaoxintv video pages

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

// ==UserScript==
// @name         自动全屏Auto Fullscreen for xiaoxintv
// @namespace    http://tampermonkey.net/
// @version      0.107
// @description  给家里人用的自动全屏,Automatically enters fullscreen mode after 10 seconds on xiaoxintv video pages
// @author       zsanjin
// @match        https://xiaoxintv.cc/index.php/vod/play/id/*
// @license      BSD-2-Clause
// ==/UserScript==

 
(function() {
    'use strict';

    console.log('[Auto Fullscreen] Script started');
    
    // 注入辅助函数到页面
    function injectHelperFunction() {
        const script = document.createElement('script');
        script.textContent = `
            window.triggerFullscreen = function() {
                const button = document.querySelector('.yzmplayer-full-icon');
                if (button) {
                    button.click();
                    return true;
                }
                return false;
            }
        `;
        document.head.appendChild(script);
    }

    // 在iframe中注入helper函数
    function injectIntoIframe(iframe) {
        try {
            const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
            const script = iframeDoc.createElement('script');
            script.textContent = `
                window.triggerFullscreen = function() {
                    const button = document.querySelector('.yzmplayer-full-icon');
                    if (button) {
                        // 创建完整的事件序列
                        const events = [
                            new MouseEvent('mouseenter', {bubbles: true}),
                            new MouseEvent('mouseover', {bubbles: true}),
                            new MouseEvent('mousedown', {bubbles: true}),
                            new MouseEvent('mouseup', {bubbles: true}),
                            new MouseEvent('click', {bubbles: true})
                        ];
                        
                        events.forEach(event => button.dispatchEvent(event));
                        return true;
                    }
                    return false;
                }
            `;
            iframeDoc.head.appendChild(script);
            return true;
        } catch (e) {
            console.log('[Auto Fullscreen] Cannot access iframe:', e.message);
            return false;
        }
    }

    // 创建一个按钮
    function createTriggerButton() {
        const button = document.createElement('button');
        button.textContent = '点击自动全屏';
        button.style.cssText = `
            position: fixed;
            top: 10px;
            right: 10px;
            z-index: 999999;
            padding: 5px 10px;
            background: rgba(0, 0, 0, 0.7);
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        `;
        
        button.addEventListener('click', function() {
            console.log('[Auto Fullscreen] Trigger button clicked');
            
            // 尝试在所有iframe中触发全屏
            const iframes = document.querySelectorAll('iframe');
            iframes.forEach(iframe => {
                if (iframe.src.includes('player')) {
                    try {
                        iframe.contentWindow.triggerFullscreen();
                    } catch (e) {
                        console.log('[Auto Fullscreen] Cannot trigger in iframe:', e.message);
                    }
                }
            });
        });
        
        document.body.appendChild(button);
    }

    // 主函数
    function init() {
        console.log('[Auto Fullscreen] Initializing...');
        
        // 注入主页面的helper函数
        injectHelperFunction();
        
        // 监听iframe加载
        const observer = new MutationObserver((mutations) => {
            for (const mutation of mutations) {
                if (mutation.addedNodes.length) {
                    const iframes = document.querySelectorAll('iframe');
                    iframes.forEach(iframe => {
                        if (iframe.src.includes('player') && !iframe.dataset.injected) {
                            console.log('[Auto Fullscreen] Injecting into iframe:', iframe.src);
                            if (injectIntoIframe(iframe)) {
                                iframe.dataset.injected = 'true';
                            }
                        }
                    });
                }
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });

        // 创建触发按钮
        createTriggerButton();
    }

    // 当页面加载完成后初始化
    if (document.readyState === 'complete') {
        init();
    } else {
        window.addEventListener('load', init);
    }
})();