Greasy Fork 还支持 简体中文。

Youtube Faster Fullscreen

Tries to replace player with embed player so we get faster fullscreen and no scrolling/comments/description while in fullscreen.

  1. // ==UserScript==
  2. // @name Youtube Faster Fullscreen
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.20
  5. // @description Tries to replace player with embed player so we get faster fullscreen and no scrolling/comments/description while in fullscreen.
  6. // @author Filipe Henriques
  7. // @match https://www.youtube.com/*
  8. // @icon https://cdn-icons-png.flaticon.com/512/124/124015.png
  9. // @grant none
  10. // @run-at document-start
  11. // @noframes
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. // 'use strict';
  16.  
  17. // Only run the userscript outside iframes
  18. // if ( !(window.location !== window.parent.location) ) {
  19.  
  20. // Keyboard shortcut support for the iframe
  21. (window.opera ? document.body : document).addEventListener('keydown', function(e) {
  22. console.log(e);
  23. if (e.srcElement.nodeName == "INPUT" || e.srcElement.id == "contenteditable-root") {return false;}
  24. // Fullscreen
  25. if (e.key === "f" && !e.ctrlKey) {
  26. e.cancelBubble = true;
  27. e.stopImmediatePropagation();
  28.  
  29. // we are inside the iframe. else -> outside iframe
  30. if(!document.querySelector(".ytp-fullscreen-button.ytp-button")){
  31. let button = document.getElementById("myFrame").contentWindow.document.getElementsByClassName("ytp-fullscreen-button ytp-button")[0];
  32. button.click();
  33. } else { document.querySelector(".ytp-fullscreen-button.ytp-button").click(); }
  34. }
  35. // Pause
  36. if (e.code === "Space" || e.code == "KeyK") {
  37. e.cancelBubble = true;
  38. e.stopImmediatePropagation();
  39. e.preventDefault();
  40. // outside iframe
  41. if(!document.querySelector(".ytp-fullscreen-button.ytp-button")){
  42. let video = document.getElementById("myFrame").contentWindow.document.querySelector("video");
  43. if (video.paused) {video.play();}
  44. else {video.pause();}
  45. }
  46. }
  47. // Mute
  48. if (e.code === 'KeyM') {
  49. e.cancelBubble = true;
  50. e.stopImmediatePropagation();
  51. let mute = document.querySelector('.ytp-mute-button.ytp-button');
  52. // we are inside the iframe. else -> outside iframe
  53. if(!mute){
  54. let button = document.getElementById("myFrame").contentWindow.document.getElementsByClassName('ytp-mute-button ytp-button')[0];
  55. button.click();
  56. } else { mute.click(); }
  57. }
  58. // Captions
  59. if (e.code === 'KeyC') {
  60. e.cancelBubble = true;
  61. e.stopImmediatePropagation();
  62. let captions = document.querySelector('.ytp-subtitles-button.ytp-button');
  63. // we are inside the iframe. else -> outside iframe
  64. if(!captions){
  65. let button = document.getElementById("myFrame").contentWindow.document.getElementsByClassName(' ytp-subtitles-button ytp-button')[0];
  66. button.click();
  67. } else { captions.click(); }
  68. }
  69. // TODO ADD SEEKING WITH ARROW KEYS OUTSIDE IFRAMEm
  70. return false;
  71. }, !window.opera);
  72.  
  73. // ######################################################################################################
  74.  
  75. // Global observer waits until elements we want appear
  76. var href = document.location.href;
  77. var ytd_found = false; var ytd = null;
  78. var player_found = false; var player = null;
  79. var orig_found = false; var original_player = null;
  80. var skeleton_found = false; var skeleton_theater = null;
  81. var watch_on_yt_fnd = false; var watch_on_yt_bt = null;
  82. var player_replaced = false;
  83. var observer = new MutationObserver(function (mutations, me) {
  84. // Check if we are navigating somewhere else and force page reload
  85. if (href != document.location.href) {
  86. let oldref = href;
  87. href = document.location.href;
  88. if ( oldref.includes('watch') && !document.location.href.includes('watch')) {
  89. let frame = document.getElementById("myFrame");
  90. frame.contentWindow.document.querySelector("video").pause();
  91. frame.parentElement.remove();
  92. frame.remove();
  93. } if ( !oldref.includes('watch') && !document.location.href.includes('watch')) {
  94. // do nothing
  95. } else {document.location.href = href;}
  96. }
  97. // First original player in page
  98. if (!orig_found) {original_player = document.querySelector("video");}
  99. if (original_player && !orig_found) {
  100.  
  101. orig_found = true;
  102. original_player.pause();
  103. original_player.addEventListener('play', (event) => {
  104. event.target.pause();
  105. // }, { once: true });
  106. });
  107. }
  108. // Intercept first appearance of skeleton theater
  109. if (!skeleton_found) {skeleton_theater = document.querySelector('#player.skeleton.theater');}
  110. if (skeleton_theater && !skeleton_found) {
  111.  
  112. skeleton_found = true;
  113. replacePlayer(skeleton_theater);
  114. let o = new MutationObserver(function (mutations, me) {
  115. mutations[0].target.removeAttribute("hidden");
  116. document.getElementById("myFrame").style = 'width: 100%; height: 100%; position: relative';
  117. });
  118. o.observe(skeleton_theater, {attributes: true});
  119.  
  120. }
  121. // Remove player theater which is the final original player as soon as it appears
  122. if (!player_found) { player = document.getElementById('player-theater-container');}
  123. if (player && !player_found) {
  124. player_found = true;
  125. player.firstChild.remove();
  126. }
  127. // Remove Watch on Youtube button
  128. if (!watch_on_yt_fnd) { watch_on_yt_bt = document.getElementById("myFrame").contentWindow.document.querySelector(".ytp-youtube-button.ytp-button.yt-uix-sessionlink");}
  129. if ( watch_on_yt_bt && !watch_on_yt_fnd) {
  130. watch_on_yt_bt.remove();
  131. }
  132. return;
  133. });
  134. observer.observe(document, {
  135. childList: true,
  136. subtree: true
  137. });
  138.  
  139.  
  140. // Replace player with an iframe embed player
  141. function replacePlayer(element) {
  142. let video_id = window.location.search.split('v=')[1];
  143. let ampersandPosition = video_id.indexOf('&');
  144. if(ampersandPosition != -1) {video_id = video_id.substring(0, ampersandPosition);}
  145. let start_time = 0;
  146. let t = window.location.search.split('t=')[1];
  147. if (t) { start_time = t.slice(0, -1);}
  148.  
  149. let iframe = document.createElement('iframe');
  150. iframe.src = 'https://www.youtube.com/embed/' + video_id + '?autoplay=1&enablejsapi=1&start=' + start_time;
  151. iframe.title = 'Youtube video player';
  152. iframe.style = 'width: inherit; height: inherit; position: fixed';
  153. iframe.frameborder = '0';
  154. iframe.allow = 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture';
  155. iframe.setAttribute('allowFullScreen', '');
  156. iframe.id = 'myFrame';
  157. element.prepend(iframe);
  158. }
  159.  
  160.  
  161. // }
  162. }) ();