Auto-Rotate Video Fullscreen

Automatically rotate device when video goes fullscreen

当前为 2025-03-05 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Auto-Rotate Video Fullscreen
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Automatically rotate device when video goes fullscreen
// @match        *://*/*
// @grant        window.orientation
// @grant        window.screen
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // Function to check if an element is in full screen
    function isFullScreen(element) {
        return (
            document.fullscreenElement === element ||
            document.webkitFullscreenElement === element ||
            document.mozFullScreenElement === element ||
            document.msFullscreenElement === element ||
            element.webkitDisplayingFullscreen ||
            element.fullscreen
        );
    }

    // Function to attempt screen rotation
    function rotateScreen(landscape) {
        try {
            // Method 1: Screen Orientation API
            if (screen.orientation && screen.orientation.lock) {
                screen.orientation.lock(landscape ? 'landscape-primary' : 'portrait-primary')
                    .catch(() => {});
            }

            // Method 2: Explicit window.orientation manipulation
            if (window.orientation !== undefined) {
                window.orientation = landscape ? 90 : 0;
            }
        } catch (error) {
            // Silently handle rotation errors
        }
    }

    // Full screen change handler
    function handleFullscreenChange(event) {
        const target = event.target;
        
        if (target.tagName.toLowerCase() === 'video' || 
            target.tagName.toLowerCase() === 'iframe' || 
            target.querySelector('video, iframe')) {
            
            const isCurrentlyFullScreen = isFullScreen(target);
            rotateScreen(isCurrentlyFullScreen);
        }
    }

    // Function to add fullscreen listeners to an element
    function addFullscreenListeners(element) {
        element.addEventListener('fullscreenchange', handleFullscreenChange);
        element.addEventListener('webkitfullscreenchange', handleFullscreenChange);
        element.addEventListener('mozfullscreenchange', handleFullscreenChange);
        element.addEventListener('MSFullscreenChange', handleFullscreenChange);
    }

    // Initial setup function
    function init() {
        // Add global fullscreen listeners
        document.addEventListener('fullscreenchange', handleFullscreenChange);
        document.addEventListener('webkitfullscreenchange', handleFullscreenChange);
        document.addEventListener('mozfullscreenchange', handleFullscreenChange);
        document.addEventListener('MSFullscreenChange', handleFullscreenChange);

        // Find and add listeners to existing video elements
        const videoElements = document.querySelectorAll('video, iframe');
        videoElements.forEach(video => {
            addFullscreenListeners(video);
        });

        // Observe for dynamically added video elements
        const observer = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                if (mutation.type === 'childList') {
                    mutation.addedNodes.forEach((node) => {
                        if (node.nodeType === Node.ELEMENT_NODE) {
                            const videos = node.querySelectorAll('video, iframe');
                            if (videos.length > 0) {
                                videos.forEach(video => {
                                    addFullscreenListeners(video);
                                });
                            }
                        }
                    });
                }
            });
        });

        // Start observing the entire document
        observer.observe(document.documentElement, {
            childList: true,
            subtree: true
        });
    }

    // Run initialization
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();