Resize YT To Window Size

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

目前为 2019-01-30 提交的版本。查看 最新版本

  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 122
  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://greasyfork.org/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.  
  176. var scriptHtmlSelector = 'html:not([fullscreen="true"])';
  177. var scriptBodySelector = 'body.' + scriptBodyClassId; // body.ytwp-window-player
  178. var scriptSelector = scriptHtmlSelector + ' ' + scriptBodySelector;
  179.  
  180. var videoContainerId = 'player';
  181. var videoContainerPlacemarkerId = scriptShortName + '-placemarker'; // ytwp-placemarker
  182.  
  183. var transitionProperties = ["transition", "-ms-transition", "-moz-transition", "-webkit-transition", "-o-transition"];
  184. var transformProperties = ["transform", "-ms-transform", "-moz-transform", "-webkit-transform", "-o-transform"];
  185.  
  186. //--- YTWP
  187. var ytwp = uw.ytwp = {
  188. scriptShortName: scriptShortName, // YT Window Player
  189. log_: function(logger, args) { logger.apply(console, ['[' + this.scriptShortName + '] '].concat(Array.prototype.slice.call(args))); return 1; },
  190. log: function() { return this.log_(console.log, arguments); },
  191. error: function() { return this.log_(console.error, arguments); },
  192.  
  193. initialized: false,
  194. pageReady: false,
  195. isWatchPage: false,
  196. };
  197.  
  198. ytwp.isWatchUrl = function (url) {
  199. if (!url)
  200. url = uw.location.href;
  201. return url.match(/https?:\/\/(www\.)?youtube.com\/watch\?/)
  202. || url.match(/https?:\/\/(www\.)?youtube.com\/(c|channel)\/[^\/]+\/live/);
  203. };
  204.  
  205. ytwp.enterTheaterMode = function() {
  206. // ytwp.log('enterTheaterMode')
  207. var watchElement = document.querySelector('ytd-watch:not([hidden])') || document.querySelector('ytd-watch-flexy:not([hidden])')
  208. if (watchElement) {
  209. if (!watchElement.hasAttribute('theater')) {
  210. var sizeButton = watchElement.querySelector('button.ytp-size-button')
  211. if (sizeButton) {
  212. sizeButton.click()
  213. }
  214. }
  215. watchElement.canFitTheater_ = true // When it's too small, it disables the theater mode.
  216. } else if (watchElement = document.querySelector('#page.watch')) {
  217. if (!watchElement.classList.contains('watch-stage-mode')) {
  218. var sizeButton = watchElement.querySelector('button.ytp-size-button')
  219. if (sizeButton) {
  220. sizeButton.click()
  221. }
  222. }
  223. }
  224. }
  225. ytwp.enterTheaterMode();
  226. uw.addEventListener('resize', ytwp.enterTheaterMode);
  227.  
  228. ytwp.init = function() {
  229. ytwp.log('init');
  230. if (!ytwp.initialized) {
  231. ytwp.isWatchPage = ytwp.isWatchUrl();
  232. if (ytwp.isWatchPage) {
  233. ytwp.removeSearchAutofocus();
  234. if (!document.getElementById(scriptStyleId)) {
  235. ytwp.event.initStyle();
  236. }
  237. ytwp.initScroller();
  238. ytwp.initialized = true;
  239. ytwp.pageReady = false;
  240. }
  241. }
  242. ytwp.event.onWatchInit();
  243. if (ytwp.isWatchPage) {
  244. ytwp.html5PlayerFix();
  245. }
  246. }
  247.  
  248. ytwp.initScroller = function() {
  249. // Register listener & Call it now.
  250. uw.addEventListener('scroll', ytwp.onScroll, false);
  251. uw.addEventListener('resize', ytwp.onScroll, false);
  252. ytwp.onScroll();
  253. }
  254.  
  255. ytwp.onScroll = function() {
  256. var viewportHeight = document.documentElement.clientHeight;
  257.  
  258. // topOfPageClassId
  259. if (ytwp.isWatchPage && uw.scrollY == 0) {
  260. document.body.classList.add(topOfPageClassId);
  261. //var player = document.getElementById('movie_player');
  262. //if (player)
  263. // player.focus();
  264. } else {
  265. document.body.classList.remove(topOfPageClassId);
  266. }
  267.  
  268. // viewingVideoClassId
  269. if (ytwp.isWatchPage && uw.scrollY <= viewportHeight) {
  270. document.body.classList.add(viewingVideoClassId);
  271. } else {
  272. document.body.classList.remove(viewingVideoClassId);
  273. }
  274. }
  275.  
  276. ytwp.event = {
  277. initStyle: function() {
  278. ytwp.log('initStyle');
  279. ytwp.style = new JSStyleSheet(scriptStyleId);
  280. ytwp.event.buildStylesheet();
  281. // Duplicate stylesheet targeting data-spf-name if enabled.
  282. if (uw.spf) {
  283. var temp = scriptBodySelector;
  284. scriptBodySelector = 'body[data-spf-name="watch"]';
  285. scriptSelector = scriptHtmlSelector + ' ' + scriptBodySelector
  286. ytwp.event.buildStylesheet();
  287. ytwp.style.appendRule('body[data-spf-name="watch"]:not(.ytwp-window-player) #masthead-positioner', {
  288. 'position': 'absolute',
  289. 'top': playerHeight + ' !important'
  290. });
  291. }
  292. ytwp.style.injectIntoHeader();
  293. },
  294. buildStylesheet: function() {
  295. ytwp.log('buildStylesheet');
  296. //--- Browser Scrollbar
  297. // Chrome/Webkit
  298. ytwp.style.appendRule(scriptBodySelector + '::-webkit-scrollbar', {
  299. 'width': '0',
  300. 'height': '0',
  301. });
  302. // Firefox/Gecko
  303. // Requires about:config flag to be toggled as of FireFox v63
  304. // https://github.com/Zren/ResizeYoutubePlayerToWindowSize/issues/42
  305. ytwp.style.appendRule('html', {
  306. 'scrollbar-width': 'none',
  307. });
  308.  
  309. //--- Video Player
  310. var d;
  311. d = buildVenderPropertyDict(transitionProperties, 'left 0s linear, padding-left 0s linear');
  312. d['padding'] = '0 !important';
  313. d['margin'] = '0 !important';
  314. ytwp.style.appendRule([
  315. scriptBodySelector + ' #player',
  316. scriptBodySelector + '.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible #player',
  317. scriptBodySelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player',
  318. scriptBodySelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player-legacy',
  319. scriptBodySelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #watch7-main-container',
  320. ], d);
  321. //
  322. d = buildVenderPropertyDict(transitionProperties, 'width 0s linear, left 0s linear');
  323.  
  324. // Bugfix for Firefox
  325. // Parts of the header (search box) are hidden under the player.
  326. // Firefox doesn't seem to be using the fixed header+guide yet.
  327. d['float'] = 'initial';
  328.  
  329. // Skinny mode
  330. d['left'] = 0;
  331. d['margin-left'] = 0;
  332.  
  333. ytwp.style.appendRule(scriptBodySelector + ' #player-api', d);
  334.  
  335. // Theatre mode
  336. ytwp.style.appendRule(scriptBodySelector + ' .watch-stage-mode #player .player-api', {
  337. 'left': 'initial !important',
  338. 'margin-left': 'initial !important',
  339. });
  340.  
  341. // Hide the cinema/wide mode button since it's useless.
  342. //ytwp.style.appendRule(scriptBodySelector + ' #movie_player .ytp-size-button', 'display', 'none');
  343.  
  344. // !important is mainly for simplicity, but is needed to override the !important styling when the Guide is open due to:
  345. // .sidebar-collapsed #watch7-video, .sidebar-collapsed #watch7-main, .sidebar-collapsed .watch7-playlist { width: 945px!important; }
  346. // Also, Youtube Center resizes #player at element level.
  347. // Don't resize if Youtube+'s html.floater is detected.
  348. // Dont' resize if Youtube+ (Iridium/Material)'s html.iri-always-visible is detected.
  349. ytwp.style.appendRule(
  350. [
  351. scriptSelector + ' #player',
  352. scriptSelector + ' #player-api',
  353. scriptHtmlSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' #movie_player',
  354. scriptSelector + ' #player-mole-container',
  355. scriptHtmlSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' .html5-video-container',
  356. scriptHtmlSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' .html5-main-video',
  357. scriptSelector + ' ytd-watch-flexy[theater] #player-theater-container.ytd-watch-flexy',
  358. ],
  359. {
  360. 'width': '100% !important',
  361. 'min-width': '100% !important',
  362. 'max-width': '100% !important',
  363. 'height': playerHeight + ' !important',
  364. 'min-height': playerHeight + ' !important',
  365. 'max-height': playerHeight + ' !important',
  366. }
  367. );
  368.  
  369. ytwp.style.appendRule(
  370. [
  371. scriptSelector + ' #player',
  372. scriptSelector + ' .html5-main-video',
  373. ],
  374. {
  375. 'top': '0 !important',
  376. 'right': '0 !important',
  377. 'bottom': '0 !important',
  378. 'left': '0 !important',
  379. }
  380. );
  381. // Resize #player-unavailable, #player-api
  382. // Using min/max width/height will keep
  383. ytwp.style.appendRule(scriptSelector + ' #player .player-width', 'width', '100% !important');
  384. ytwp.style.appendRule(scriptSelector + ' #player .player-height', 'height', '100% !important');
  385.  
  386. // Fix video overlays
  387. ytwp.style.appendRule([
  388. scriptSelector + ' .html5-video-player .ad-container-single-media-element-annotations', // Ad
  389. scriptSelector + ' .html5-video-player .ytp-upnext', // Autoplay Next Video
  390. ], 'top', '0');
  391.  
  392.  
  393. //--- Move Video Player
  394. ytwp.style.appendRule(scriptSelector + ' #player', {
  395. 'position': 'absolute',
  396. // Already top:0; left: 0;
  397. });
  398. ytwp.style.appendRule(scriptSelector, { // body
  399. 'margin-top': playerHeight,
  400. });
  401.  
  402. // Fix the top right avatar button
  403. ytwp.style.appendRule(scriptSelector + ' button.ytp-button.ytp-cards-button', 'top', '0');
  404.  
  405.  
  406. //--- Sidebar
  407. // Remove the transition delay as you can see it moving on page load.
  408. d = buildVenderPropertyDict(transitionProperties, 'margin-top 0s linear, padding-top 0s linear');
  409. d['margin-top'] = '0 !important';
  410. d['top'] = '0 !important';
  411. ytwp.style.appendRule(scriptSelector + ' #watch7-sidebar', d);
  412.  
  413. ytwp.style.appendRule(scriptSelector + '.cardified-page #watch7-sidebar-contents', 'padding-top', '0');
  414.  
  415. //--- Absolutely position the fixed header.
  416. // Masthead
  417. d = buildVenderPropertyDict(transitionProperties, 'top 0s linear !important');
  418. ytwp.style.appendRule(scriptSelector + '.hide-header-transition #masthead-positioner', d);
  419. ytwp.style.appendRule(scriptSelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  420. 'position': 'absolute',
  421. 'top': playerHeight + ' !important'
  422. });
  423. // Lower masthead below Youtube+'s html.floater
  424. ytwp.style.appendRule('html.floater ' + scriptBodySelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  425. 'z-index': '5',
  426. });
  427. // Autocomplete popup
  428. ytwp.style.appendRule(scriptSelector + ' .sbdd_a', {
  429. 'top': '56px',
  430. });
  431. ytwp.style.appendRule(scriptSelector + '.' + viewingVideoClassId + ' .sbdd_a', {
  432. 'top': 'calc(' + playerHeight + ' + 56px) !important',
  433. 'position': 'absolute !important',
  434. });
  435.  
  436. // Guide
  437. // When watching the video, we need to line it up with the masthead.
  438. ytwp.style.appendRule(scriptSelector + '.' + viewingVideoClassId + ' #appbar-guide-menu', {
  439. 'display': 'initial',
  440. 'position': 'absolute',
  441. 'top': '100% !important' // Masthead height
  442. });
  443. ytwp.style.appendRule(scriptSelector + '.' + viewingVideoClassId + ' #page.watch #guide', {
  444. 'display': 'initial',
  445. 'margin': '0',
  446. 'position': 'initial'
  447. });
  448.  
  449.  
  450. //---
  451. // MiniPlayer-Bar
  452. ytwp.style.appendRule(scriptSelector + ' #miniplayer-bar #player', {
  453. 'position': 'static',
  454. });
  455. ytwp.style.appendRule(
  456. [
  457. scriptSelector + ' #miniplayer-bar #player',
  458. scriptSelector + ' #miniplayer-bar #player-api',
  459. scriptHtmlSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' #miniplayer-bar #movie_player',
  460. scriptSelector + ' #player-mole-container',
  461. scriptSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' #miniplayer-bar .html5-video-container',
  462. scriptSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' #miniplayer-bar .html5-main-video',
  463. ],
  464. {
  465. 'width': '252px !important',
  466. 'min-width': '252px !important',
  467. 'max-width': '252px !important',
  468. 'height': '142px !important',
  469. 'min-height': '142px !important',
  470. 'max-height': '142px !important',
  471. }
  472. );
  473. // Override inline style (caused by a JS animation) that breaks the miniplayer video
  474. // https://github.com/Zren/ResizeYoutubePlayerToWindowSize/issues/41#issuecomment-439710130
  475. ytwp.style.appendRule('.video-stream.html5-main-video', {
  476. 'top': '0 !important',
  477. });
  478.  
  479. //---
  480. // Hide Scrollbars
  481. ytwp.style.appendRule(scriptSelector + '.' + topOfPageClassId, 'overflow-x', 'hidden');
  482.  
  483.  
  484. //--- Fix Other Possible Style Issues
  485. ytwp.style.appendRule(scriptSelector + ' #placeholder-player', 'display', 'none');
  486. ytwp.style.appendRule(scriptSelector + ' #watch-sidebar-spacer', 'display', 'none');
  487. ytwp.style.appendRule(scriptSelector + ' .skip-nav', 'display', 'none');
  488.  
  489. //--- Whitespace Leftover From Moving The Video
  490. ytwp.style.appendRule(scriptSelector + ' #page.watch', 'padding-top', '0');
  491. ytwp.style.appendRule(scriptSelector + ' .player-branded-banner', 'height', '0');
  492.  
  493. //--- Youtube+ Compatiblity
  494. ytwp.style.appendRule(scriptSelector + ' #body-container', 'position', 'static');
  495. ytwp.style.appendRule(scriptHtmlSelector + '.part_static_size:not(.content-snap-width-skinny-mode) ' + scriptBodySelector + ' .watch-non-stage-mode #player-playlist', 'width', '1066px');
  496.  
  497. //--- Playlist Bar
  498. ytwp.style.appendRule([
  499. scriptSelector + ' #placeholder-playlist',
  500. scriptSelector + ' #player .player-height#watch-appbar-playlist',
  501. ], {
  502. 'height': '540px !important',
  503. 'max-height': '540px !important',
  504. });
  505.  
  506. d = buildVenderPropertyDict(transitionProperties, 'transform 0s linear');
  507. ytwp.style.appendRule(scriptSelector + ' #watch-appbar-playlist', d);
  508. d = buildVenderPropertyDict(transformProperties, 'translateY(0px)');
  509. d['margin-left'] = '0';
  510. d['top'] = 'calc(' + playerHeight + ' + 60px)';
  511. ytwp.style.appendRule(scriptSelector + ' #player .player-height#watch-appbar-playlist', d);
  512. ytwp.style.appendRule(scriptSelector + ' .playlist-videos-list', {
  513. 'max-height': '470px !important',
  514. 'height': 'initial !important',
  515. });
  516.  
  517. // Old layout `&disable_polymer=true`
  518. ytwp.style.appendRule(scriptSelector + ' #player .player-height#watch-appbar-playlist', {
  519. 'left': 'calc((100vw - 1066px)/2 + 640px + 10px)',
  520. 'width': '416px',
  521. });
  522. ytwp.style.stylesheet += '@media screen and (min-height: 630px) and (min-width: 1294px) {\n';
  523. ytwp.style.appendRule(scriptSelector + ' #player .player-height#watch-appbar-playlist', {
  524. 'left': 'calc((100vw - 1280px)/2 + 854px + 10px)',
  525. });
  526. ytwp.style.stylesheet += '}\n @media screen and (min-width: 1720px) and (min-height:980px) {\n';
  527. ytwp.style.appendRule(scriptSelector + ' #player .player-height#watch-appbar-playlist', {
  528. 'left': 'calc((100vw - 1706px)/2 + 1280px + 10px)',
  529. });
  530. ytwp.style.stylesheet += '}\n';
  531.  
  532. //---
  533. // Material UI
  534. ytwp.style.appendRule(scriptSelector + '.ytwp-scrolltop #extra-buttons', 'display', 'none !important');
  535. // ytwp.style.appendRule('body > #player:not(.ytd-watch)', 'display', 'none');
  536. // ytwp.style.appendRule('body.ytwp-viewing-video #content:not(app-header-layout) ytd-page-manager', 'margin-top', '0 !important');
  537. // ytwp.style.appendRule('.ytd-watch-0 #content-separator.ytd-watch', 'margin-top', '0');
  538. ytwp.style.appendRule('ytd-app', 'position', 'static !important');
  539. ytwp.style.appendRule('ytd-watch #top', 'margin-top', '71px !important'); // 56px (topnav height) + 15px (margin)
  540. ytwp.style.appendRule('ytd-watch #container', 'margin-top', '0 !important');
  541. ytwp.style.appendRule('ytd-watch #content-separator', 'margin-top', '0 !important');
  542. ytwp.style.appendRule(scriptSelector + '.ytwp-viewing-video ytd-app #masthead-container.ytd-app', {
  543. 'position': 'absolute',
  544. 'top': playerHeight,
  545. 'z-index': 0,
  546. });
  547. ytwp.style.appendRule(scriptSelector + '.ytwp-viewing-video ytd-watch #masthead-positioner', {
  548. 'top': playerHeight + ' !important',
  549. });
  550. ytwp.style.appendRule(scriptSelector + ' .ytp-cued-thumbnail-overlay', 'z-index', '10');
  551.  
  552. //---
  553. // Flexy UI
  554. ytwp.style.appendRule(scriptSelector + ' ytd-watch-flexy[theater] #player-theater-container.ytd-watch-flexy', {
  555. 'position': 'absolute',
  556. 'top': '0',
  557. });
  558. ytwp.style.appendRule(scriptSelector + ' ytd-watch-flexy', 'padding-top', '71px'); // 56px (topnav height) + 15px (margin)
  559. ytwp.style.appendRule(scriptSelector + ' #error-screen', 'z-index', '11');
  560. },
  561. onWatchInit: function() {
  562. ytwp.log('onWatchInit');
  563. if (!ytwp.initialized) return;
  564. if (ytwp.pageReady) return;
  565.  
  566. ytwp.event.addBodyClass();
  567. ytwp.pageReady = true;
  568. },
  569. onDispose: function() {
  570. ytwp.log('onDispose');
  571. ytwp.initialized = false;
  572. ytwp.pageReady = false;
  573. ytwp.isWatchPage = false;
  574. },
  575. addBodyClass: function() {
  576. // Insert CSS Into the body so people can style around the effects of this script.
  577. document.body.classList.add(scriptBodyClassId);
  578. ytwp.log('Applied ' + scriptBodySelector);
  579. },
  580. };
  581.  
  582. ytwp.html5PlayerFix = function() {
  583. ytwp.log('html5PlayerFix');
  584. return;
  585.  
  586. try {
  587. if (!uw.ytcenter // Youtube Center
  588. && !uw.html5Patched // Youtube+
  589. && (!ytwp.html5.app)
  590. && (uw.ytplayer && uw.ytplayer.config)
  591. && (uw.yt && uw.yt.player && uw.yt.player.Application && uw.yt.player.Application.create)
  592. ) {
  593. ytwp.html5.app = ytwp.html5.getPlayerInstance();
  594. }
  595.  
  596. ytwp.html5.update();
  597. ytwp.html5.autohideControls();
  598. } catch (e) {
  599. ytwp.error(e);
  600. }
  601. }
  602.  
  603. ytwp.fixMasthead = function() {
  604. ytwp.log('fixMasthead');
  605. var el = document.querySelector('#masthead-positioner-height-offset');
  606. if (el) {
  607. ytwp.fixMastheadElement(el);
  608. }
  609. }
  610. ytwp.fixMastheadElement = function(el) {
  611. ytwp.log('fixMastheadElement', el);
  612. if (el.style.height) { // != ""
  613. setTimeout(function(){
  614. el.style.height = ""
  615. document.querySelector('#appbar-guide-menu').style.marginTop = "";
  616. }, 0);
  617. }
  618. }
  619.  
  620. JSStyleSheet.injectIntoHeader(scriptStyleId + '-focusfix', 'input#search[autofocus] { display: none; }');
  621. ytwp.removeSearchAutofocus = function() {
  622. var e = document.querySelector('input#search');
  623. // ytwp.log('removeSearchAutofocus', e)
  624. if (e) {
  625. e.removeAttribute('autofocus')
  626. }
  627. }
  628.  
  629. ytwp.registerMastheadFix = function() {
  630. ytwp.log('registerMastheadFix');
  631. // Fix the offset when closing the Share widget (element.style.height = ~275px).
  632.  
  633. observe('#masthead-positioner-height-offset', {
  634. attributes: true,
  635. }, function(mutation) {
  636. console.log(mutation.type, mutation)
  637. if (mutation.attributeName === 'style') {
  638. var el = mutation.target;
  639. if (el.style.height) { // != ""
  640. setTimeout(function(){
  641. el.style.height = ""
  642. document.querySelector('#appbar-guide-menu').style.marginTop = "";
  643. }, 0);
  644. }
  645.  
  646. }
  647. });
  648. }
  649.  
  650. //--- Material UI
  651. ytwp.materialPageTransition = function() {
  652. ytwp.log('materialPageTransition')
  653. ytwp.init();
  654.  
  655. if (ytwp.isWatchUrl()) {
  656. ytwp.removeSearchAutofocus();
  657. ytwp.event.addBodyClass();
  658. // if (!ytwp.html5.app) {
  659. if (!ytwp.initialized) {
  660. ytwp.log('materialPageTransition !ytwp.html5.app', ytwp.html5.app)
  661. setTimeout(ytwp.materialPageTransition, 100);
  662. }
  663. var playerApi = document.querySelector('#player-api')
  664. if (playerApi) {
  665. playerApi.click()
  666. }
  667. } else {
  668. ytwp.event.onDispose();
  669. document.body.classList.remove(scriptBodyClassId);
  670. }
  671. ytwp.onScroll();
  672. ytwp.fixMasthead();
  673. ytwp.attemptToUpdatePlayer();
  674. };
  675.  
  676. //--- Listeners
  677. ytwp.registerListeners = function() {
  678. ytwp.registerMaterialListeners();
  679. ytwp.registerMastheadFix();
  680. };
  681.  
  682. ytwp.registerMaterialListeners = function() {
  683. // For Material UI
  684. HistoryEvent.listeners.push(ytwp.materialPageTransition);
  685. HistoryEvent.startTimer();
  686. // HistoryEvent.inject();
  687. // HistoryEvent.listeners.push(console.log.bind(console));
  688. };
  689.  
  690. ytwp.main = function() {
  691. ytwp.registerListeners();
  692. ytwp.init();
  693. ytwp.fixMasthead();
  694. };
  695.  
  696. ytwp.main();
  697.  
  698. // ytwp.updatePlayerTimerId = 0;
  699. ytwp.updatePlayerAttempts = -1;
  700. ytwp.updatePlayerMaxAttempts = 150; // 60fps = 2.5sec
  701. ytwp.attemptToUpdatePlayer = function() {
  702. console.log('ytwp.attemptToUpdatePlayer')
  703. if (0 <= ytwp.updatePlayerAttempts && ytwp.updatePlayerAttempts < ytwp.updatePlayerMaxAttempts) {
  704. ytwp.updatePlayerAttempts = 0;
  705. } else {
  706. ytwp.updatePlayerAttempts = 0;
  707. ytwp.attemptToUpdatePlayerTick();
  708. }
  709. // setTimeout(ytwp.updatePlayer, 10000); /// Just in case it's not caught
  710. }
  711. ytwp.attemptToUpdatePlayerTick = function() {
  712. console.log('ytwp.attemptToUpdatePlayerTick', ytwp.updatePlayerAttempts)
  713. if (ytwp.updatePlayerAttempts < ytwp.updatePlayerMaxAttempts) {
  714. ytwp.updatePlayerAttempts += 1;
  715. ytwp.updatePlayer();
  716. // ytwp.updatePlayerTimerId = setTimeout(ytwp.attemptToUpdatePlayerTick, 200);
  717. requestAnimationFrame(ytwp.attemptToUpdatePlayerTick);
  718. }
  719. }
  720.  
  721. ytwp.updatePlayer = function() {
  722. ytwp.removeSearchAutofocus();
  723. ytwp.enterTheaterMode();
  724. }
  725.  
  726. ytwp.materialPageTransition()
  727. setInterval(ytwp.updatePlayer, 2500);
  728.  
  729. })(window);