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

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

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

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

(function() {
    'use strict';
 
    console.log('[Auto Fullscreen] Script started');
    console.log('[Auto Fullscreen] Current URL:', window.location.href);
 
    // 最大尝试次数
    const MAX_ATTEMPTS = 60; // 增加尝试次数
    let attempts = 0;
 
    // 检查按钮是否存在的函数
    function checkForButton() {
        console.log(`[Auto Fullscreen] Checking for button (Attempt ${attempts + 1}/${MAX_ATTEMPTS})`);
        
        // 先检查所有iframe
        const iframes = document.querySelectorAll('iframe');
        for (const iframe of iframes) {
            try {
                console.log('[Auto Fullscreen] Checking iframe:', iframe.src);
                const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
                
                // 在iframe中查找按钮
                const button = iframeDoc.querySelector('.yzmplayer-full-icon') || 
                             iframeDoc.querySelector('button[data-balloon="全屏"]') ||
                             iframeDoc.querySelector('.yzmplayer-full button');
                
                if (button) {
                    console.log('[Auto Fullscreen] Button found in iframe!');
                    console.log('[Auto Fullscreen] Button details:', button); // 打印按钮的详细信息
                    return {button, context: iframeDoc};
                }
            } catch (e) {
                console.log('[Auto Fullscreen] Cannot access iframe content:', e.message);
            }
        }
 
        // 如果iframe中没找到,检查主页面
        const mainButton = document.querySelector('.yzmplayer-full-icon') || 
                         document.querySelector('button[data-balloon="全屏"]') ||
                         document.querySelector('.yzmplayer-full button');
        
        if (mainButton) {
            console.log('[Auto Fullscreen] Button found in main page!');
            console.log('[Auto Fullscreen] Button details:', mainButton); // 打印按钮的详细信息
            return {button: mainButton, context: document};
        }
 
        // 打印当前DOM结构以供调试
        console.log('[Auto Fullscreen] Current main page structure:', document.body.innerHTML);
        return null;
    }
 
    // 主要逻辑
    function tryFullscreen() {
        const result = checkForButton();
        
        if (result) {
            const {button, context} = result;
            console.log('[Auto Fullscreen] Attempting to click button...');
            try {
                // 模拟用户点击
                button.click();
                console.log('[Auto Fullscreen] Button clicked!');
                
                // 如果还是不能触发全屏,可以延迟执行,模拟更多的用户交互
                setTimeout(() => {
                    if (document.documentElement.requestFullscreen) {
                        document.documentElement.requestFullscreen();
                    } else if (document.documentElement.mozRequestFullScreen) { // Firefox
                        document.documentElement.mozRequestFullScreen();
                    } else if (document.documentElement.webkitRequestFullscreen) { // Chrome, Safari, Opera
                        document.documentElement.webkitRequestFullscreen();
                    } else if (document.documentElement.msRequestFullscreen) { // IE/Edge
                        document.documentElement.msRequestFullscreen();
                    }
                }, 5000); // 延迟100ms再请求全屏
                
                return true;
            } catch (error) {
                console.error('[Auto Fullscreen] Error clicking button:', error);
            }
        }
        
        attempts++;
        
        if (attempts >= MAX_ATTEMPTS) {
            console.error('[Auto Fullscreen] Error: Maximum attempts reached. Button not found!');
            return true;
        }
        
        return false;
    }
 
    // 等待播放器加载
    function waitForPlayer() {
        console.log('[Auto Fullscreen] Waiting for player to load...');
        
        setTimeout(() => {
            console.log('[Auto Fullscreen] Starting button search process...');
            
            // 每半秒检查一次
            const intervalId = setInterval(() => {
                if (tryFullscreen()) {
                    clearInterval(intervalId);
                }
            }, 500); // 缩短检查间隔
            
        }, 100); // 等待10秒
    }
 
    // 监听DOM变化
    const observer = new MutationObserver((mutations) => {
        for (const mutation of mutations) {
            if (mutation.addedNodes.length) {
                // 如果发现新增了iframe或视频播放器相关元素
                if (document.querySelector('iframe') || 
                    document.querySelector('.yzmplayer-video-wrap')) {
                    console.log('[Auto Fullscreen] Player detected, starting wait sequence...');
                    observer.disconnect();
                    waitForPlayer();
                    break;
                }
            }
        }
    });
 
    // 开始监听DOM变化
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();