Auto-Rotate Video Fullscreen

Automatically rotate device when video goes fullscreen

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

  1. // ==UserScript==
  2. // @name Auto-Rotate Video Fullscreen
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description Automatically rotate device when video goes fullscreen
  6. // @match *://*/*
  7. // @grant window.orientation
  8. // @grant window.screen
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to check if an element is in full screen
  16. function isFullScreen(element) {
  17. return (
  18. document.fullscreenElement === element ||
  19. document.webkitFullscreenElement === element ||
  20. document.mozFullScreenElement === element ||
  21. document.msFullscreenElement === element ||
  22. element.webkitDisplayingFullscreen ||
  23. element.fullscreen
  24. );
  25. }
  26.  
  27. // Function to attempt screen rotation
  28. function rotateScreen(landscape) {
  29. try {
  30. // Method 1: Screen Orientation API
  31. if (screen.orientation && screen.orientation.lock) {
  32. screen.orientation.lock(landscape ? 'landscape-primary' : 'portrait-primary')
  33. .catch(() => {});
  34. }
  35.  
  36. // Method 2: Explicit window.orientation manipulation
  37. if (window.orientation !== undefined) {
  38. window.orientation = landscape ? 90 : 0;
  39. }
  40. } catch (error) {
  41. // Silently handle rotation errors
  42. }
  43. }
  44.  
  45. // Full screen change handler
  46. function handleFullscreenChange(event) {
  47. const target = event.target;
  48. if (target.tagName.toLowerCase() === 'video' ||
  49. target.tagName.toLowerCase() === 'iframe' ||
  50. target.querySelector('video, iframe')) {
  51. const isCurrentlyFullScreen = isFullScreen(target);
  52. rotateScreen(isCurrentlyFullScreen);
  53. }
  54. }
  55.  
  56. // Function to add fullscreen listeners to an element
  57. function addFullscreenListeners(element) {
  58. element.addEventListener('fullscreenchange', handleFullscreenChange);
  59. element.addEventListener('webkitfullscreenchange', handleFullscreenChange);
  60. element.addEventListener('mozfullscreenchange', handleFullscreenChange);
  61. element.addEventListener('MSFullscreenChange', handleFullscreenChange);
  62. }
  63.  
  64. // Initial setup function
  65. function init() {
  66. // Add global fullscreen listeners
  67. document.addEventListener('fullscreenchange', handleFullscreenChange);
  68. document.addEventListener('webkitfullscreenchange', handleFullscreenChange);
  69. document.addEventListener('mozfullscreenchange', handleFullscreenChange);
  70. document.addEventListener('MSFullscreenChange', handleFullscreenChange);
  71.  
  72. // Find and add listeners to existing video elements
  73. const videoElements = document.querySelectorAll('video, iframe');
  74. videoElements.forEach(video => {
  75. addFullscreenListeners(video);
  76. });
  77.  
  78. // Observe for dynamically added video elements
  79. const observer = new MutationObserver((mutations) => {
  80. mutations.forEach((mutation) => {
  81. if (mutation.type === 'childList') {
  82. mutation.addedNodes.forEach((node) => {
  83. if (node.nodeType === Node.ELEMENT_NODE) {
  84. const videos = node.querySelectorAll('video, iframe');
  85. if (videos.length > 0) {
  86. videos.forEach(video => {
  87. addFullscreenListeners(video);
  88. });
  89. }
  90. }
  91. });
  92. }
  93. });
  94. });
  95.  
  96. // Start observing the entire document
  97. observer.observe(document.documentElement, {
  98. childList: true,
  99. subtree: true
  100. });
  101. }
  102.  
  103. // Run initialization
  104. if (document.readyState === 'loading') {
  105. document.addEventListener('DOMContentLoaded', init);
  106. } else {
  107. init();
  108. }
  109. })();