Resize YT To Window Size

Moves the YouTube video to the top of the website and fill the window with the video player.

当前为 2018-08-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Resize YT To Window Size
  3. // @description Moves the YouTube video to the top of the website and fill the window with the video player.
  4. // @author Chris H (Zren / Shade)
  5. // @license MIT
  6. // @icon https://s.ytimg.com/yts/img/favicon_32-vflOogEID.png
  7. // @homepageURL https://github.com/Zren/ResizeYoutubePlayerToWindowSize/
  8. // @namespace http://xshade.ca
  9. // @version 119
  10. // @include http*://*.youtube.com/*
  11. // @include http*://youtube.com/*
  12. // @include http*://*.youtu.be/*
  13. // @include http*://youtu.be/*
  14. // @grant none
  15. // ==/UserScript==
  16.  
  17. // Github: https://github.com/Zren/ResizeYoutubePlayerToWindowSize
  18. // GreasyFork: https://gf.qytechs.cn/scripts/811-resize-yt-to-window-size
  19. // OpenUserJS.org: https://openuserjs.org/scripts/zren/Resize_YT_To_Window_Size
  20. // Userscripts.org: http://userscripts-mirror.org/scripts/show/153699
  21.  
  22. (function (window) {
  23. "use strict";
  24.  
  25. //--- Settings
  26. var playerHeight = '100vh';
  27.  
  28. //--- Imported Globals
  29. // yt
  30. // ytcenter
  31. // html5Patched (Youtube+)
  32. // ytplayer
  33. var uw = window;
  34.  
  35. //--- Already Loaded?
  36. // GreaseMonkey loads this script twice for some reason.
  37. if (uw.ytwp) return;
  38.  
  39. //--- Is iframe?
  40. function inIframe () {
  41. try {
  42. return window.self !== window.top;
  43. } catch (e) {
  44. return true;
  45. }
  46. }
  47. if (inIframe()) return;
  48.  
  49. //--- Utils
  50. function isStringType(obj) { return typeof obj === 'string'; }
  51. function isArrayType(obj) { return obj instanceof Array; }
  52. function isObjectType(obj) { return typeof obj === 'object'; }
  53. function isUndefined(obj) { return typeof obj === 'undefined'; }
  54. function buildVenderPropertyDict(propertyNames, value) {
  55. var d = {};
  56. for (var i in propertyNames)
  57. d[propertyNames[i]] = value;
  58. return d;
  59. }
  60. function observe(selector, config, callback) {
  61. var observer = new MutationObserver(function(mutations) {
  62. mutations.forEach(function(mutation){
  63. callback(mutation);
  64. });
  65. });
  66. var target = document.querySelector(selector);
  67. if (!target) {
  68. return null;
  69. }
  70. observer.observe(target, config);
  71. return observer;
  72. }
  73.  
  74. //--- Stylesheet
  75. var JSStyleSheet = function(id) {
  76. this.id = id;
  77. this.stylesheet = '';
  78. };
  79.  
  80. JSStyleSheet.prototype.buildRule = function(selector, styles) {
  81. var s = "";
  82. for (var key in styles) {
  83. s += "\t" + key + ": " + styles[key] + ";\n";
  84. }
  85. return selector + " {\n" + s + "}\n";
  86. };
  87.  
  88. JSStyleSheet.prototype.appendRule = function(selector, k, v) {
  89. if (isArrayType(selector))
  90. selector = selector.join(',\n');
  91. var newStyle;
  92. if (!isUndefined(k) && !isUndefined(v) && isStringType(k)) { // v can be any type (as we stringify it).
  93. var d = {};
  94. d[k] = v;
  95. newStyle = this.buildRule(selector, d);
  96. } else if (!isUndefined(k) && isUndefined(v) && isObjectType(k)) {
  97. newStyle = this.buildRule(selector, k);
  98. } else {
  99. // Invalid Arguments
  100. console.log('Illegal arguments', arguments);
  101. return;
  102. }
  103.  
  104. this.stylesheet += newStyle;
  105. };
  106.  
  107. JSStyleSheet.injectIntoHeader = function(injectedStyleId, stylesheet) {
  108. var styleElement = document.getElementById(injectedStyleId);
  109. if (!styleElement) {
  110. styleElement = document.createElement('style');
  111. styleElement.type = 'text/css';
  112. styleElement.id = injectedStyleId;
  113. document.getElementsByTagName('head')[0].appendChild(styleElement);
  114. }
  115. styleElement.appendChild(document.createTextNode(stylesheet));
  116. };
  117.  
  118. JSStyleSheet.prototype.injectIntoHeader = function(injectedStyleId, stylesheet) {
  119. JSStyleSheet.injectIntoHeader(this.id, this.stylesheet);
  120. };
  121.  
  122. //--- History
  123. var HistoryEvent = function() {}
  124. HistoryEvent.listeners = []
  125.  
  126. HistoryEvent.dispatch = function(state, title, url) {
  127. var stack = this.listeners
  128. for (var i = 0, l = stack.length; i < l; i++) {
  129. stack[i].call(this, state, title, url)
  130. }
  131. }
  132. HistoryEvent.onPushState = function(state, title, url) {
  133. HistoryEvent.dispatch(state, title, url)
  134. return HistoryEvent.origPushState.apply(window.history, arguments)
  135. }
  136. HistoryEvent.onReplaceState = function(state, title, url) {
  137. HistoryEvent.dispatch(state, title, url)
  138. return HistoryEvent.origReplaceState.apply(window.history, arguments)
  139. }
  140. HistoryEvent.inject = function() {
  141. if (!HistoryEvent.injected) {
  142. HistoryEvent.origPushState = window.history.pushState
  143. HistoryEvent.origReplaceState = window.history.replaceState
  144.  
  145. window.history.pushState = HistoryEvent.onPushState
  146. window.history.replaceState = HistoryEvent.onReplaceState
  147. HistoryEvent.injected = true
  148. }
  149. }
  150.  
  151. HistoryEvent.timerId = 0
  152. HistoryEvent.onTick = function() {
  153. var currentPage = window.location.pathname + window.location.search
  154. if (HistoryEvent.lastPage != currentPage) {
  155. HistoryEvent.dispatch({}, document.title, window.location.href)
  156. HistoryEvent.lastPage = currentPage
  157. }
  158. }
  159. HistoryEvent.startTimer = function() {
  160. HistoryEvent.lastPage = window.location.pathname + window.location.search
  161. HistoryEvent.timerId = setInterval(HistoryEvent.onTick, 500)
  162. }
  163. HistoryEvent.stopTimer = function() {
  164. clearInterval(HistoryEvent.timerId)
  165. }
  166. window.ytwpHistoryEvent = HistoryEvent
  167.  
  168.  
  169. //--- Constants
  170. var scriptShortName = 'ytwp'; // YT Window Player
  171. var scriptStyleId = scriptShortName + '-style'; // ytwp-style
  172. var scriptBodyClassId = scriptShortName + '-window-player'; // .ytwp-window-player
  173. var viewingVideoClassId = scriptShortName + '-viewing-video'; // .ytwp-viewing-video
  174. var topOfPageClassId = scriptShortName + '-scrolltop'; // .ytwp-scrolltop
  175. var scriptBodyClassSelector = 'body.' + scriptBodyClassId; // body.ytwp-window-player
  176.  
  177. var videoContainerId = 'player';
  178. var videoContainerPlacemarkerId = scriptShortName + '-placemarker'; // ytwp-placemarker
  179.  
  180. var transitionProperties = ["transition", "-ms-transition", "-moz-transition", "-webkit-transition", "-o-transition"];
  181. var transformProperties = ["transform", "-ms-transform", "-moz-transform", "-webkit-transform", "-o-transform"];
  182.  
  183. //--- YTWP
  184. var ytwp = uw.ytwp = {
  185. scriptShortName: scriptShortName, // YT Window Player
  186. log_: function(logger, args) { logger.apply(console, ['[' + this.scriptShortName + '] '].concat(Array.prototype.slice.call(args))); return 1; },
  187. log: function() { return this.log_(console.log, arguments); },
  188. error: function() { return this.log_(console.error, arguments); },
  189.  
  190. initialized: false,
  191. pageReady: false,
  192. isWatchPage: false,
  193. };
  194.  
  195. ytwp.isWatchUrl = function (url) {
  196. if (!url)
  197. url = uw.location.href;
  198. return url.match(/https?:\/\/(www\.)?youtube.com\/watch\?/)
  199. || url.match(/https?:\/\/(www\.)?youtube.com\/(c|channel)\/[^\/]+\/live/);
  200. };
  201.  
  202. ytwp.enterTheaterMode = function() {
  203. // ytwp.log('enterTheaterMode')
  204. var watchElement = document.querySelector('ytd-watch:not([hidden])') || document.querySelector('ytd-watch-flexy:not([hidden])')
  205. if (watchElement) {
  206. if (!watchElement.hasAttribute('theater')) {
  207. var sizeButton = watchElement.querySelector('button.ytp-size-button')
  208. if (sizeButton) {
  209. sizeButton.click()
  210. }
  211. }
  212. watchElement.canFitTheater_ = true // When it's too small, it disables the theater mode.
  213. } else if (watchElement = document.querySelector('#page.watch')) {
  214. if (!watchElement.classList.contains('watch-stage-mode')) {
  215. var sizeButton = watchElement.querySelector('button.ytp-size-button')
  216. if (sizeButton) {
  217. sizeButton.click()
  218. }
  219. }
  220. }
  221. }
  222. ytwp.enterTheaterMode();
  223. uw.addEventListener('resize', ytwp.enterTheaterMode);
  224.  
  225. ytwp.init = function() {
  226. ytwp.log('init');
  227. if (!ytwp.initialized) {
  228. ytwp.isWatchPage = ytwp.isWatchUrl();
  229. if (ytwp.isWatchPage) {
  230. ytwp.removeSearchAutofocus();
  231. if (!document.getElementById(scriptStyleId)) {
  232. ytwp.event.initStyle();
  233. }
  234. ytwp.initScroller();
  235. ytwp.initialized = true;
  236. ytwp.pageReady = false;
  237. }
  238. }
  239. ytwp.event.onWatchInit();
  240. if (ytwp.isWatchPage) {
  241. ytwp.html5PlayerFix();
  242. }
  243. }
  244.  
  245. ytwp.initScroller = function() {
  246. // Register listener & Call it now.
  247. uw.addEventListener('scroll', ytwp.onScroll, false);
  248. uw.addEventListener('resize', ytwp.onScroll, false);
  249. ytwp.onScroll();
  250. }
  251.  
  252. ytwp.onScroll = function() {
  253. var viewportHeight = document.documentElement.clientHeight;
  254.  
  255. // topOfPageClassId
  256. if (ytwp.isWatchPage && uw.scrollY == 0) {
  257. document.body.classList.add(topOfPageClassId);
  258. //var player = document.getElementById('movie_player');
  259. //if (player)
  260. // player.focus();
  261. } else {
  262. document.body.classList.remove(topOfPageClassId);
  263. }
  264.  
  265. // viewingVideoClassId
  266. if (ytwp.isWatchPage && uw.scrollY <= viewportHeight) {
  267. document.body.classList.add(viewingVideoClassId);
  268. } else {
  269. document.body.classList.remove(viewingVideoClassId);
  270. }
  271. }
  272.  
  273. ytwp.event = {
  274. initStyle: function() {
  275. ytwp.log('initStyle');
  276. ytwp.style = new JSStyleSheet(scriptStyleId);
  277. ytwp.event.buildStylesheet();
  278. // Duplicate stylesheet targeting data-spf-name if enabled.
  279. if (uw.spf) {
  280. var temp = scriptBodyClassSelector;
  281. scriptBodyClassSelector = 'body[data-spf-name="watch"]';
  282. ytwp.event.buildStylesheet();
  283. ytwp.style.appendRule('body[data-spf-name="watch"]:not(.ytwp-window-player) #masthead-positioner', {
  284. 'position': 'absolute',
  285. 'top': playerHeight + ' !important'
  286. });
  287. }
  288. ytwp.style.injectIntoHeader();
  289. },
  290. buildStylesheet: function() {
  291. ytwp.log('buildStylesheet');
  292. //--- Browser Scrollbar
  293. ytwp.style.appendRule(scriptBodyClassSelector + '::-webkit-scrollbar', {
  294. 'width': '0',
  295. 'height': '0',
  296. });
  297.  
  298. //--- Video Player
  299. var d;
  300. d = buildVenderPropertyDict(transitionProperties, 'left 0s linear, padding-left 0s linear');
  301. d['padding'] = '0 !important';
  302. d['margin'] = '0 !important';
  303. ytwp.style.appendRule([
  304. scriptBodyClassSelector + ' #player',
  305. scriptBodyClassSelector + '.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible #player',
  306. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player',
  307. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player-legacy',
  308. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #watch7-main-container',
  309. ], d);
  310. //
  311. d = buildVenderPropertyDict(transitionProperties, 'width 0s linear, left 0s linear');
  312.  
  313. // Bugfix for Firefox
  314. // Parts of the header (search box) are hidden under the player.
  315. // Firefox doesn't seem to be using the fixed header+guide yet.
  316. d['float'] = 'initial';
  317.  
  318. // Skinny mode
  319. d['left'] = 0;
  320. d['margin-left'] = 0;
  321.  
  322. ytwp.style.appendRule(scriptBodyClassSelector + ' #player-api', d);
  323.  
  324. // Theatre mode
  325. ytwp.style.appendRule(scriptBodyClassSelector + ' .watch-stage-mode #player .player-api', {
  326. 'left': 'initial !important',
  327. 'margin-left': 'initial !important',
  328. });
  329.  
  330. // Hide the cinema/wide mode button since it's useless.
  331. //ytwp.style.appendRule(scriptBodyClassSelector + ' #movie_player .ytp-size-button', 'display', 'none');
  332.  
  333. // !important is mainly for simplicity, but is needed to override the !important styling when the Guide is open due to:
  334. // .sidebar-collapsed #watch7-video, .sidebar-collapsed #watch7-main, .sidebar-collapsed .watch7-playlist { width: 945px!important; }
  335. // Also, Youtube Center resizes #player at element level.
  336. // Don't resize if Youtube+'s html.floater is detected.
  337. // Dont' resize if Youtube+ (Iridium/Material)'s html.iri-always-visible is detected.
  338. ytwp.style.appendRule(
  339. [
  340. scriptBodyClassSelector + ' #player',
  341. scriptBodyClassSelector + ' #player-api',
  342. 'html:not(.floater):not(.iri-always-visible) ' + scriptBodyClassSelector + ' #movie_player',
  343. scriptBodyClassSelector + ' #player-mole-container',
  344. 'html:not(.floater):not(.iri-always-visible) ' + scriptBodyClassSelector + ' .html5-video-container',
  345. 'html:not(.floater):not(.iri-always-visible) ' + scriptBodyClassSelector + ' .html5-main-video',
  346. scriptBodyClassSelector + ' ytd-watch-flexy[theater] #player-theater-container.ytd-watch-flexy',
  347. ],
  348. {
  349. 'width': '100% !important',
  350. 'min-width': '100% !important',
  351. 'max-width': '100% !important',
  352. 'height': playerHeight + ' !important',
  353. 'min-height': playerHeight + ' !important',
  354. 'max-height': playerHeight + ' !important',
  355. }
  356. );
  357.  
  358. ytwp.style.appendRule(
  359. [
  360. scriptBodyClassSelector + ' #player',
  361. scriptBodyClassSelector + ' .html5-main-video',
  362. ],
  363. {
  364. 'top': '0 !important',
  365. 'right': '0 !important',
  366. 'bottom': '0 !important',
  367. 'left': '0 !important',
  368. }
  369. );
  370. // Resize #player-unavailable, #player-api
  371. // Using min/max width/height will keep
  372. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-width', 'width', '100% !important');
  373. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height', 'height', '100% !important');
  374.  
  375. // Fix video overlays
  376. ytwp.style.appendRule([
  377. scriptBodyClassSelector + ' .html5-video-player .ad-container-single-media-element-annotations', // Ad
  378. scriptBodyClassSelector + ' .html5-video-player .ytp-upnext', // Autoplay Next Video
  379. ], 'top', '0');
  380.  
  381.  
  382. //--- Move Video Player
  383. ytwp.style.appendRule(scriptBodyClassSelector + ' #player', {
  384. 'position': 'absolute',
  385. // Already top:0; left: 0;
  386. });
  387. ytwp.style.appendRule(scriptBodyClassSelector, { // body
  388. 'margin-top': playerHeight,
  389. });
  390.  
  391. // Fix the top right avatar button
  392. ytwp.style.appendRule(scriptBodyClassSelector + ' button.ytp-button.ytp-cards-button', 'top', '0');
  393.  
  394.  
  395. //--- Sidebar
  396. // Remove the transition delay as you can see it moving on page load.
  397. d = buildVenderPropertyDict(transitionProperties, 'margin-top 0s linear, padding-top 0s linear');
  398. d['margin-top'] = '0 !important';
  399. d['top'] = '0 !important';
  400. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch7-sidebar', d);
  401.  
  402. ytwp.style.appendRule(scriptBodyClassSelector + '.cardified-page #watch7-sidebar-contents', 'padding-top', '0');
  403.  
  404. //--- Absolutely position the fixed header.
  405. // Masthead
  406. d = buildVenderPropertyDict(transitionProperties, 'top 0s linear !important');
  407. ytwp.style.appendRule(scriptBodyClassSelector + '.hide-header-transition #masthead-positioner', d);
  408. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  409. 'position': 'absolute',
  410. 'top': playerHeight + ' !important'
  411. });
  412. // Lower masthead below Youtube+'s html.floater
  413. ytwp.style.appendRule('html.floater ' + scriptBodyClassSelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  414. 'z-index': '5',
  415. });
  416. // Autocomplete popup
  417. ytwp.style.appendRule(scriptBodyClassSelector + ' .sbdd_a', {
  418. 'top': '56px',
  419. });
  420. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' .sbdd_a', {
  421. 'top': 'calc(' + playerHeight + ' + 56px) !important',
  422. 'position': 'absolute !important',
  423. });
  424.  
  425. // Guide
  426. // When watching the video, we need to line it up with the masthead.
  427. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #appbar-guide-menu', {
  428. 'display': 'initial',
  429. 'position': 'absolute',
  430. 'top': '100% !important' // Masthead height
  431. });
  432. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #page.watch #guide', {
  433. 'display': 'initial',
  434. 'margin': '0',
  435. 'position': 'initial'
  436. });
  437.  
  438.  
  439. //---
  440. // MiniPlayer-Bar
  441. ytwp.style.appendRule(scriptBodyClassSelector + ' #miniplayer-bar #player', {
  442. 'position': 'static',
  443. });
  444. ytwp.style.appendRule(
  445. [
  446. scriptBodyClassSelector + ' #miniplayer-bar #player',
  447. scriptBodyClassSelector + ' #miniplayer-bar #player-api',
  448. 'html:not(.floater):not(.iri-always-visible) ' + scriptBodyClassSelector + ' #miniplayer-bar #movie_player',
  449. scriptBodyClassSelector + ' #player-mole-container',
  450. 'html:not(.floater):not(.iri-always-visible) ' + scriptBodyClassSelector + ' #miniplayer-bar .html5-video-container',
  451. 'html:not(.floater):not(.iri-always-visible) ' + scriptBodyClassSelector + ' #miniplayer-bar .html5-main-video',
  452. ],
  453. {
  454. 'width': '252px !important',
  455. 'min-width': '252px !important',
  456. 'max-width': '252px !important',
  457. 'height': '142px !important',
  458. 'min-height': '142px !important',
  459. 'max-height': '142px !important',
  460. }
  461. );
  462.  
  463. //---
  464. // Hide Scrollbars
  465. ytwp.style.appendRule(scriptBodyClassSelector + '.' + topOfPageClassId, 'overflow-x', 'hidden');
  466.  
  467.  
  468. //--- Fix Other Possible Style Issues
  469. ytwp.style.appendRule(scriptBodyClassSelector + ' #placeholder-player', 'display', 'none');
  470. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch-sidebar-spacer', 'display', 'none');
  471. ytwp.style.appendRule(scriptBodyClassSelector + ' .skip-nav', 'display', 'none');
  472.  
  473. //--- Whitespace Leftover From Moving The Video
  474. ytwp.style.appendRule(scriptBodyClassSelector + ' #page.watch', 'padding-top', '0');
  475. ytwp.style.appendRule(scriptBodyClassSelector + ' .player-branded-banner', 'height', '0');
  476.  
  477. //--- Youtube+ Compatiblity
  478. ytwp.style.appendRule(scriptBodyClassSelector + ' #body-container', 'position', 'static');
  479. ytwp.style.appendRule('.part_static_size:not(.content-snap-width-skinny-mode) ' + scriptBodyClassSelector + ' .watch-non-stage-mode #player-playlist', 'width', '1066px');
  480.  
  481. //--- Playlist Bar
  482. ytwp.style.appendRule([
  483. scriptBodyClassSelector + ' #placeholder-playlist',
  484. scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist',
  485. ], {
  486. 'height': '540px !important',
  487. 'max-height': '540px !important',
  488. });
  489.  
  490. d = buildVenderPropertyDict(transitionProperties, 'transform 0s linear');
  491. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch-appbar-playlist', d);
  492. d = buildVenderPropertyDict(transformProperties, 'translateY(0px)');
  493. d['margin-left'] = '0';
  494. d['top'] = 'calc(' + playerHeight + ' + 60px)';
  495. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist', d);
  496. ytwp.style.appendRule(scriptBodyClassSelector + ' .playlist-videos-list', {
  497. 'max-height': '470px !important',
  498. 'height': 'initial !important',
  499. });
  500.  
  501. // Old layout `&disable_polymer=true`
  502. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist', {
  503. 'left': 'calc((100vw - 1066px)/2 + 640px + 10px)',
  504. 'width': '416px',
  505. });
  506. ytwp.style.stylesheet += '@media screen and (min-height: 630px) and (min-width: 1294px) {\n';
  507. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist', {
  508. 'left': 'calc((100vw - 1280px)/2 + 854px + 10px)',
  509. });
  510. ytwp.style.stylesheet += '}\n @media screen and (min-width: 1720px) and (min-height:980px) {\n';
  511. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist', {
  512. 'left': 'calc((100vw - 1706px)/2 + 1280px + 10px)',
  513. });
  514. ytwp.style.stylesheet += '}\n';
  515.  
  516. //---
  517. // Material UI
  518. ytwp.style.appendRule(scriptBodyClassSelector + '.ytwp-scrolltop #extra-buttons', 'display', 'none !important');
  519. // ytwp.style.appendRule('body > #player:not(.ytd-watch)', 'display', 'none');
  520. // ytwp.style.appendRule('body.ytwp-viewing-video #content:not(app-header-layout) ytd-page-manager', 'margin-top', '0 !important');
  521. // ytwp.style.appendRule('.ytd-watch-0 #content-separator.ytd-watch', 'margin-top', '0');
  522. ytwp.style.appendRule('ytd-app', 'position', 'static !important');
  523. ytwp.style.appendRule('ytd-watch #top', 'margin-top', '71px !important'); // 56px (topnav height) + 15px (margin)
  524. ytwp.style.appendRule('ytd-watch #container', 'margin-top', '0 !important');
  525. ytwp.style.appendRule('ytd-watch #content-separator', 'margin-top', '0 !important');
  526. ytwp.style.appendRule(scriptBodyClassSelector + '.ytwp-viewing-video ytd-app #masthead-container.ytd-app', {
  527. 'position': 'absolute',
  528. 'top': playerHeight,
  529. 'z-index': 0,
  530. });
  531. ytwp.style.appendRule(scriptBodyClassSelector + '.ytwp-viewing-video ytd-watch #masthead-positioner', {
  532. 'top': playerHeight + ' !important',
  533. });
  534. ytwp.style.appendRule(scriptBodyClassSelector + ' .ytp-cued-thumbnail-overlay', 'z-index', '10');
  535.  
  536. //---
  537. // Flexy UI
  538. ytwp.style.appendRule(scriptBodyClassSelector + ' ytd-watch-flexy[theater] #player-theater-container.ytd-watch-flexy', {
  539. 'position': 'absolute',
  540. 'top': '0',
  541. });
  542. ytwp.style.appendRule(scriptBodyClassSelector + ' ytd-watch-flexy', 'padding-top', '71px'); // 56px (topnav height) + 15px (margin)
  543. },
  544. onWatchInit: function() {
  545. ytwp.log('onWatchInit');
  546. if (!ytwp.initialized) return;
  547. if (ytwp.pageReady) return;
  548.  
  549. ytwp.event.addBodyClass();
  550. ytwp.pageReady = true;
  551. },
  552. onDispose: function() {
  553. ytwp.log('onDispose');
  554. ytwp.initialized = false;
  555. ytwp.pageReady = false;
  556. ytwp.isWatchPage = false;
  557. },
  558. addBodyClass: function() {
  559. // Insert CSS Into the body so people can style around the effects of this script.
  560. document.body.classList.add(scriptBodyClassId);
  561. ytwp.log('Applied ' + scriptBodyClassSelector);
  562. },
  563. };
  564.  
  565. ytwp.html5PlayerFix = function() {
  566. ytwp.log('html5PlayerFix');
  567. return;
  568.  
  569. try {
  570. if (!uw.ytcenter // Youtube Center
  571. && !uw.html5Patched // Youtube+
  572. && (!ytwp.html5.app)
  573. && (uw.ytplayer && uw.ytplayer.config)
  574. && (uw.yt && uw.yt.player && uw.yt.player.Application && uw.yt.player.Application.create)
  575. ) {
  576. ytwp.html5.app = ytwp.html5.getPlayerInstance();
  577. }
  578.  
  579. ytwp.html5.update();
  580. ytwp.html5.autohideControls();
  581. } catch (e) {
  582. ytwp.error(e);
  583. }
  584. }
  585.  
  586. ytwp.fixMasthead = function() {
  587. ytwp.log('fixMasthead');
  588. var el = document.querySelector('#masthead-positioner-height-offset');
  589. if (el) {
  590. ytwp.fixMastheadElement(el);
  591. }
  592. }
  593. ytwp.fixMastheadElement = function(el) {
  594. ytwp.log('fixMastheadElement', el);
  595. if (el.style.height) { // != ""
  596. setTimeout(function(){
  597. el.style.height = ""
  598. document.querySelector('#appbar-guide-menu').style.marginTop = "";
  599. }, 0);
  600. }
  601. }
  602.  
  603. JSStyleSheet.injectIntoHeader(scriptStyleId + '-focusfix', 'input#search[autofocus] { display: none; }');
  604. ytwp.removeSearchAutofocus = function() {
  605. var e = document.querySelector('input#search');
  606. // ytwp.log('removeSearchAutofocus', e)
  607. if (e) {
  608. e.removeAttribute('autofocus')
  609. }
  610. }
  611.  
  612. ytwp.registerMastheadFix = function() {
  613. ytwp.log('registerMastheadFix');
  614. // Fix the offset when closing the Share widget (element.style.height = ~275px).
  615.  
  616. observe('#masthead-positioner-height-offset', {
  617. attributes: true,
  618. }, function(mutation) {
  619. console.log(mutation.type, mutation)
  620. if (mutation.attributeName === 'style') {
  621. var el = mutation.target;
  622. if (el.style.height) { // != ""
  623. setTimeout(function(){
  624. el.style.height = ""
  625. document.querySelector('#appbar-guide-menu').style.marginTop = "";
  626. }, 0);
  627. }
  628.  
  629. }
  630. });
  631. }
  632.  
  633. //--- Material UI
  634. ytwp.materialPageTransition = function() {
  635. ytwp.log('materialPageTransition')
  636. ytwp.init();
  637.  
  638. if (ytwp.isWatchUrl()) {
  639. ytwp.removeSearchAutofocus();
  640. ytwp.event.addBodyClass();
  641. // if (!ytwp.html5.app) {
  642. if (!ytwp.initialized) {
  643. ytwp.log('materialPageTransition !ytwp.html5.app', ytwp.html5.app)
  644. setTimeout(ytwp.materialPageTransition, 100);
  645. }
  646. var playerApi = document.querySelector('#player-api')
  647. if (playerApi) {
  648. playerApi.click()
  649. }
  650. } else {
  651. ytwp.event.onDispose();
  652. document.body.classList.remove(scriptBodyClassId);
  653. }
  654. ytwp.onScroll();
  655. ytwp.fixMasthead();
  656. ytwp.attemptToUpdatePlayer();
  657. };
  658.  
  659. //--- Listeners
  660. ytwp.registerListeners = function() {
  661. ytwp.registerMaterialListeners();
  662. ytwp.registerMastheadFix();
  663. };
  664.  
  665. ytwp.registerMaterialListeners = function() {
  666. // For Material UI
  667. HistoryEvent.listeners.push(ytwp.materialPageTransition);
  668. HistoryEvent.startTimer();
  669. // HistoryEvent.inject();
  670. // HistoryEvent.listeners.push(console.log.bind(console));
  671. };
  672.  
  673. ytwp.main = function() {
  674. ytwp.registerListeners();
  675. ytwp.init();
  676. ytwp.fixMasthead();
  677. };
  678.  
  679. ytwp.main();
  680.  
  681. // ytwp.updatePlayerTimerId = 0;
  682. ytwp.updatePlayerAttempts = -1;
  683. ytwp.updatePlayerMaxAttempts = 150; // 60fps = 2.5sec
  684. ytwp.attemptToUpdatePlayer = function() {
  685. console.log('ytwp.attemptToUpdatePlayer')
  686. if (0 <= ytwp.updatePlayerAttempts && ytwp.updatePlayerAttempts < ytwp.updatePlayerMaxAttempts) {
  687. ytwp.updatePlayerAttempts = 0;
  688. } else {
  689. ytwp.updatePlayerAttempts = 0;
  690. ytwp.attemptToUpdatePlayerTick();
  691. }
  692. // setTimeout(ytwp.updatePlayer, 10000); /// Just in case it's not caught
  693. }
  694. ytwp.attemptToUpdatePlayerTick = function() {
  695. console.log('ytwp.attemptToUpdatePlayerTick', ytwp.updatePlayerAttempts)
  696. if (ytwp.updatePlayerAttempts < ytwp.updatePlayerMaxAttempts) {
  697. ytwp.updatePlayerAttempts += 1;
  698. ytwp.updatePlayer();
  699. // ytwp.updatePlayerTimerId = setTimeout(ytwp.attemptToUpdatePlayerTick, 200);
  700. requestAnimationFrame(ytwp.attemptToUpdatePlayerTick);
  701. }
  702. }
  703.  
  704. ytwp.updatePlayer = function() {
  705. ytwp.removeSearchAutofocus();
  706. ytwp.enterTheaterMode();
  707. }
  708.  
  709. ytwp.materialPageTransition()
  710. setInterval(ytwp.updatePlayer, 2500);
  711.  
  712. })(window);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址