Resize YT To Window Size

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

目前為 2018-11-13 提交的版本,檢視 最新版本

  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 120
  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. 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. // Chrome/Webkit
  294. ytwp.style.appendRule(scriptBodyClassSelector + '::-webkit-scrollbar', {
  295. 'width': '0',
  296. 'height': '0',
  297. });
  298. // Firefox/Gecko
  299. // Requires about:config flag to be toggled as of FireFox v63
  300. // https://github.com/Zren/ResizeYoutubePlayerToWindowSize/issues/42
  301. ytwp.style.appendRule('html', {
  302. 'scrollbar-width': 'none',
  303. });
  304.  
  305. //--- Video Player
  306. var d;
  307. d = buildVenderPropertyDict(transitionProperties, 'left 0s linear, padding-left 0s linear');
  308. d['padding'] = '0 !important';
  309. d['margin'] = '0 !important';
  310. ytwp.style.appendRule([
  311. scriptBodyClassSelector + ' #player',
  312. scriptBodyClassSelector + '.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible #player',
  313. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player',
  314. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player-legacy',
  315. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #watch7-main-container',
  316. ], d);
  317. //
  318. d = buildVenderPropertyDict(transitionProperties, 'width 0s linear, left 0s linear');
  319.  
  320. // Bugfix for Firefox
  321. // Parts of the header (search box) are hidden under the player.
  322. // Firefox doesn't seem to be using the fixed header+guide yet.
  323. d['float'] = 'initial';
  324.  
  325. // Skinny mode
  326. d['left'] = 0;
  327. d['margin-left'] = 0;
  328.  
  329. ytwp.style.appendRule(scriptBodyClassSelector + ' #player-api', d);
  330.  
  331. // Theatre mode
  332. ytwp.style.appendRule(scriptBodyClassSelector + ' .watch-stage-mode #player .player-api', {
  333. 'left': 'initial !important',
  334. 'margin-left': 'initial !important',
  335. });
  336.  
  337. // Hide the cinema/wide mode button since it's useless.
  338. //ytwp.style.appendRule(scriptBodyClassSelector + ' #movie_player .ytp-size-button', 'display', 'none');
  339.  
  340. // !important is mainly for simplicity, but is needed to override the !important styling when the Guide is open due to:
  341. // .sidebar-collapsed #watch7-video, .sidebar-collapsed #watch7-main, .sidebar-collapsed .watch7-playlist { width: 945px!important; }
  342. // Also, Youtube Center resizes #player at element level.
  343. // Don't resize if Youtube+'s html.floater is detected.
  344. // Dont' resize if Youtube+ (Iridium/Material)'s html.iri-always-visible is detected.
  345. ytwp.style.appendRule(
  346. [
  347. scriptBodyClassSelector + ' #player',
  348. scriptBodyClassSelector + ' #player-api',
  349. 'html:not(.floater):not(.iri-always-visible) ' + scriptBodyClassSelector + ' #movie_player',
  350. scriptBodyClassSelector + ' #player-mole-container',
  351. 'html:not(.floater):not(.iri-always-visible) ' + scriptBodyClassSelector + ' .html5-video-container',
  352. 'html:not(.floater):not(.iri-always-visible) ' + scriptBodyClassSelector + ' .html5-main-video',
  353. scriptBodyClassSelector + ' ytd-watch-flexy[theater] #player-theater-container.ytd-watch-flexy',
  354. ],
  355. {
  356. 'width': '100% !important',
  357. 'min-width': '100% !important',
  358. 'max-width': '100% !important',
  359. 'height': playerHeight + ' !important',
  360. 'min-height': playerHeight + ' !important',
  361. 'max-height': playerHeight + ' !important',
  362. }
  363. );
  364.  
  365. ytwp.style.appendRule(
  366. [
  367. scriptBodyClassSelector + ' #player',
  368. scriptBodyClassSelector + ' .html5-main-video',
  369. ],
  370. {
  371. 'top': '0 !important',
  372. 'right': '0 !important',
  373. 'bottom': '0 !important',
  374. 'left': '0 !important',
  375. }
  376. );
  377. // Resize #player-unavailable, #player-api
  378. // Using min/max width/height will keep
  379. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-width', 'width', '100% !important');
  380. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height', 'height', '100% !important');
  381.  
  382. // Fix video overlays
  383. ytwp.style.appendRule([
  384. scriptBodyClassSelector + ' .html5-video-player .ad-container-single-media-element-annotations', // Ad
  385. scriptBodyClassSelector + ' .html5-video-player .ytp-upnext', // Autoplay Next Video
  386. ], 'top', '0');
  387.  
  388.  
  389. //--- Move Video Player
  390. ytwp.style.appendRule(scriptBodyClassSelector + ' #player', {
  391. 'position': 'absolute',
  392. // Already top:0; left: 0;
  393. });
  394. ytwp.style.appendRule(scriptBodyClassSelector, { // body
  395. 'margin-top': playerHeight,
  396. });
  397.  
  398. // Fix the top right avatar button
  399. ytwp.style.appendRule(scriptBodyClassSelector + ' button.ytp-button.ytp-cards-button', 'top', '0');
  400.  
  401.  
  402. //--- Sidebar
  403. // Remove the transition delay as you can see it moving on page load.
  404. d = buildVenderPropertyDict(transitionProperties, 'margin-top 0s linear, padding-top 0s linear');
  405. d['margin-top'] = '0 !important';
  406. d['top'] = '0 !important';
  407. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch7-sidebar', d);
  408.  
  409. ytwp.style.appendRule(scriptBodyClassSelector + '.cardified-page #watch7-sidebar-contents', 'padding-top', '0');
  410.  
  411. //--- Absolutely position the fixed header.
  412. // Masthead
  413. d = buildVenderPropertyDict(transitionProperties, 'top 0s linear !important');
  414. ytwp.style.appendRule(scriptBodyClassSelector + '.hide-header-transition #masthead-positioner', d);
  415. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  416. 'position': 'absolute',
  417. 'top': playerHeight + ' !important'
  418. });
  419. // Lower masthead below Youtube+'s html.floater
  420. ytwp.style.appendRule('html.floater ' + scriptBodyClassSelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  421. 'z-index': '5',
  422. });
  423. // Autocomplete popup
  424. ytwp.style.appendRule(scriptBodyClassSelector + ' .sbdd_a', {
  425. 'top': '56px',
  426. });
  427. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' .sbdd_a', {
  428. 'top': 'calc(' + playerHeight + ' + 56px) !important',
  429. 'position': 'absolute !important',
  430. });
  431.  
  432. // Guide
  433. // When watching the video, we need to line it up with the masthead.
  434. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #appbar-guide-menu', {
  435. 'display': 'initial',
  436. 'position': 'absolute',
  437. 'top': '100% !important' // Masthead height
  438. });
  439. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #page.watch #guide', {
  440. 'display': 'initial',
  441. 'margin': '0',
  442. 'position': 'initial'
  443. });
  444.  
  445.  
  446. //---
  447. // MiniPlayer-Bar
  448. ytwp.style.appendRule(scriptBodyClassSelector + ' #miniplayer-bar #player', {
  449. 'position': 'static',
  450. });
  451. ytwp.style.appendRule(
  452. [
  453. scriptBodyClassSelector + ' #miniplayer-bar #player',
  454. scriptBodyClassSelector + ' #miniplayer-bar #player-api',
  455. 'html:not(.floater):not(.iri-always-visible) ' + scriptBodyClassSelector + ' #miniplayer-bar #movie_player',
  456. scriptBodyClassSelector + ' #player-mole-container',
  457. 'html:not(.floater):not(.iri-always-visible) ' + scriptBodyClassSelector + ' #miniplayer-bar .html5-video-container',
  458. 'html:not(.floater):not(.iri-always-visible) ' + scriptBodyClassSelector + ' #miniplayer-bar .html5-main-video',
  459. ],
  460. {
  461. 'width': '252px !important',
  462. 'min-width': '252px !important',
  463. 'max-width': '252px !important',
  464. 'height': '142px !important',
  465. 'min-height': '142px !important',
  466. 'max-height': '142px !important',
  467. }
  468. );
  469.  
  470. //---
  471. // Hide Scrollbars
  472. ytwp.style.appendRule(scriptBodyClassSelector + '.' + topOfPageClassId, 'overflow-x', 'hidden');
  473.  
  474.  
  475. //--- Fix Other Possible Style Issues
  476. ytwp.style.appendRule(scriptBodyClassSelector + ' #placeholder-player', 'display', 'none');
  477. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch-sidebar-spacer', 'display', 'none');
  478. ytwp.style.appendRule(scriptBodyClassSelector + ' .skip-nav', 'display', 'none');
  479.  
  480. //--- Whitespace Leftover From Moving The Video
  481. ytwp.style.appendRule(scriptBodyClassSelector + ' #page.watch', 'padding-top', '0');
  482. ytwp.style.appendRule(scriptBodyClassSelector + ' .player-branded-banner', 'height', '0');
  483.  
  484. //--- Youtube+ Compatiblity
  485. ytwp.style.appendRule(scriptBodyClassSelector + ' #body-container', 'position', 'static');
  486. ytwp.style.appendRule('.part_static_size:not(.content-snap-width-skinny-mode) ' + scriptBodyClassSelector + ' .watch-non-stage-mode #player-playlist', 'width', '1066px');
  487.  
  488. //--- Playlist Bar
  489. ytwp.style.appendRule([
  490. scriptBodyClassSelector + ' #placeholder-playlist',
  491. scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist',
  492. ], {
  493. 'height': '540px !important',
  494. 'max-height': '540px !important',
  495. });
  496.  
  497. d = buildVenderPropertyDict(transitionProperties, 'transform 0s linear');
  498. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch-appbar-playlist', d);
  499. d = buildVenderPropertyDict(transformProperties, 'translateY(0px)');
  500. d['margin-left'] = '0';
  501. d['top'] = 'calc(' + playerHeight + ' + 60px)';
  502. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist', d);
  503. ytwp.style.appendRule(scriptBodyClassSelector + ' .playlist-videos-list', {
  504. 'max-height': '470px !important',
  505. 'height': 'initial !important',
  506. });
  507.  
  508. // Old layout `&disable_polymer=true`
  509. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist', {
  510. 'left': 'calc((100vw - 1066px)/2 + 640px + 10px)',
  511. 'width': '416px',
  512. });
  513. ytwp.style.stylesheet += '@media screen and (min-height: 630px) and (min-width: 1294px) {\n';
  514. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist', {
  515. 'left': 'calc((100vw - 1280px)/2 + 854px + 10px)',
  516. });
  517. ytwp.style.stylesheet += '}\n @media screen and (min-width: 1720px) and (min-height:980px) {\n';
  518. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist', {
  519. 'left': 'calc((100vw - 1706px)/2 + 1280px + 10px)',
  520. });
  521. ytwp.style.stylesheet += '}\n';
  522.  
  523. //---
  524. // Material UI
  525. ytwp.style.appendRule(scriptBodyClassSelector + '.ytwp-scrolltop #extra-buttons', 'display', 'none !important');
  526. // ytwp.style.appendRule('body > #player:not(.ytd-watch)', 'display', 'none');
  527. // ytwp.style.appendRule('body.ytwp-viewing-video #content:not(app-header-layout) ytd-page-manager', 'margin-top', '0 !important');
  528. // ytwp.style.appendRule('.ytd-watch-0 #content-separator.ytd-watch', 'margin-top', '0');
  529. ytwp.style.appendRule('ytd-app', 'position', 'static !important');
  530. ytwp.style.appendRule('ytd-watch #top', 'margin-top', '71px !important'); // 56px (topnav height) + 15px (margin)
  531. ytwp.style.appendRule('ytd-watch #container', 'margin-top', '0 !important');
  532. ytwp.style.appendRule('ytd-watch #content-separator', 'margin-top', '0 !important');
  533. ytwp.style.appendRule(scriptBodyClassSelector + '.ytwp-viewing-video ytd-app #masthead-container.ytd-app', {
  534. 'position': 'absolute',
  535. 'top': playerHeight,
  536. 'z-index': 0,
  537. });
  538. ytwp.style.appendRule(scriptBodyClassSelector + '.ytwp-viewing-video ytd-watch #masthead-positioner', {
  539. 'top': playerHeight + ' !important',
  540. });
  541. ytwp.style.appendRule(scriptBodyClassSelector + ' .ytp-cued-thumbnail-overlay', 'z-index', '10');
  542.  
  543. //---
  544. // Flexy UI
  545. ytwp.style.appendRule(scriptBodyClassSelector + ' ytd-watch-flexy[theater] #player-theater-container.ytd-watch-flexy', {
  546. 'position': 'absolute',
  547. 'top': '0',
  548. });
  549. ytwp.style.appendRule(scriptBodyClassSelector + ' ytd-watch-flexy', 'padding-top', '71px'); // 56px (topnav height) + 15px (margin)
  550. ytwp.style.appendRule(scriptBodyClassSelector + ' #error-screen', 'z-index', '11');
  551. },
  552. onWatchInit: function() {
  553. ytwp.log('onWatchInit');
  554. if (!ytwp.initialized) return;
  555. if (ytwp.pageReady) return;
  556.  
  557. ytwp.event.addBodyClass();
  558. ytwp.pageReady = true;
  559. },
  560. onDispose: function() {
  561. ytwp.log('onDispose');
  562. ytwp.initialized = false;
  563. ytwp.pageReady = false;
  564. ytwp.isWatchPage = false;
  565. },
  566. addBodyClass: function() {
  567. // Insert CSS Into the body so people can style around the effects of this script.
  568. document.body.classList.add(scriptBodyClassId);
  569. ytwp.log('Applied ' + scriptBodyClassSelector);
  570. },
  571. };
  572.  
  573. ytwp.html5PlayerFix = function() {
  574. ytwp.log('html5PlayerFix');
  575. return;
  576.  
  577. try {
  578. if (!uw.ytcenter // Youtube Center
  579. && !uw.html5Patched // Youtube+
  580. && (!ytwp.html5.app)
  581. && (uw.ytplayer && uw.ytplayer.config)
  582. && (uw.yt && uw.yt.player && uw.yt.player.Application && uw.yt.player.Application.create)
  583. ) {
  584. ytwp.html5.app = ytwp.html5.getPlayerInstance();
  585. }
  586.  
  587. ytwp.html5.update();
  588. ytwp.html5.autohideControls();
  589. } catch (e) {
  590. ytwp.error(e);
  591. }
  592. }
  593.  
  594. ytwp.fixMasthead = function() {
  595. ytwp.log('fixMasthead');
  596. var el = document.querySelector('#masthead-positioner-height-offset');
  597. if (el) {
  598. ytwp.fixMastheadElement(el);
  599. }
  600. }
  601. ytwp.fixMastheadElement = function(el) {
  602. ytwp.log('fixMastheadElement', el);
  603. if (el.style.height) { // != ""
  604. setTimeout(function(){
  605. el.style.height = ""
  606. document.querySelector('#appbar-guide-menu').style.marginTop = "";
  607. }, 0);
  608. }
  609. }
  610.  
  611. JSStyleSheet.injectIntoHeader(scriptStyleId + '-focusfix', 'input#search[autofocus] { display: none; }');
  612. ytwp.removeSearchAutofocus = function() {
  613. var e = document.querySelector('input#search');
  614. // ytwp.log('removeSearchAutofocus', e)
  615. if (e) {
  616. e.removeAttribute('autofocus')
  617. }
  618. }
  619.  
  620. ytwp.registerMastheadFix = function() {
  621. ytwp.log('registerMastheadFix');
  622. // Fix the offset when closing the Share widget (element.style.height = ~275px).
  623.  
  624. observe('#masthead-positioner-height-offset', {
  625. attributes: true,
  626. }, function(mutation) {
  627. console.log(mutation.type, mutation)
  628. if (mutation.attributeName === 'style') {
  629. var el = mutation.target;
  630. if (el.style.height) { // != ""
  631. setTimeout(function(){
  632. el.style.height = ""
  633. document.querySelector('#appbar-guide-menu').style.marginTop = "";
  634. }, 0);
  635. }
  636.  
  637. }
  638. });
  639. }
  640.  
  641. //--- Material UI
  642. ytwp.materialPageTransition = function() {
  643. ytwp.log('materialPageTransition')
  644. ytwp.init();
  645.  
  646. if (ytwp.isWatchUrl()) {
  647. ytwp.removeSearchAutofocus();
  648. ytwp.event.addBodyClass();
  649. // if (!ytwp.html5.app) {
  650. if (!ytwp.initialized) {
  651. ytwp.log('materialPageTransition !ytwp.html5.app', ytwp.html5.app)
  652. setTimeout(ytwp.materialPageTransition, 100);
  653. }
  654. var playerApi = document.querySelector('#player-api')
  655. if (playerApi) {
  656. playerApi.click()
  657. }
  658. } else {
  659. ytwp.event.onDispose();
  660. document.body.classList.remove(scriptBodyClassId);
  661. }
  662. ytwp.onScroll();
  663. ytwp.fixMasthead();
  664. ytwp.attemptToUpdatePlayer();
  665. };
  666.  
  667. //--- Listeners
  668. ytwp.registerListeners = function() {
  669. ytwp.registerMaterialListeners();
  670. ytwp.registerMastheadFix();
  671. };
  672.  
  673. ytwp.registerMaterialListeners = function() {
  674. // For Material UI
  675. HistoryEvent.listeners.push(ytwp.materialPageTransition);
  676. HistoryEvent.startTimer();
  677. // HistoryEvent.inject();
  678. // HistoryEvent.listeners.push(console.log.bind(console));
  679. };
  680.  
  681. ytwp.main = function() {
  682. ytwp.registerListeners();
  683. ytwp.init();
  684. ytwp.fixMasthead();
  685. };
  686.  
  687. ytwp.main();
  688.  
  689. // ytwp.updatePlayerTimerId = 0;
  690. ytwp.updatePlayerAttempts = -1;
  691. ytwp.updatePlayerMaxAttempts = 150; // 60fps = 2.5sec
  692. ytwp.attemptToUpdatePlayer = function() {
  693. console.log('ytwp.attemptToUpdatePlayer')
  694. if (0 <= ytwp.updatePlayerAttempts && ytwp.updatePlayerAttempts < ytwp.updatePlayerMaxAttempts) {
  695. ytwp.updatePlayerAttempts = 0;
  696. } else {
  697. ytwp.updatePlayerAttempts = 0;
  698. ytwp.attemptToUpdatePlayerTick();
  699. }
  700. // setTimeout(ytwp.updatePlayer, 10000); /// Just in case it's not caught
  701. }
  702. ytwp.attemptToUpdatePlayerTick = function() {
  703. console.log('ytwp.attemptToUpdatePlayerTick', ytwp.updatePlayerAttempts)
  704. if (ytwp.updatePlayerAttempts < ytwp.updatePlayerMaxAttempts) {
  705. ytwp.updatePlayerAttempts += 1;
  706. ytwp.updatePlayer();
  707. // ytwp.updatePlayerTimerId = setTimeout(ytwp.attemptToUpdatePlayerTick, 200);
  708. requestAnimationFrame(ytwp.attemptToUpdatePlayerTick);
  709. }
  710. }
  711.  
  712. ytwp.updatePlayer = function() {
  713. ytwp.removeSearchAutofocus();
  714. ytwp.enterTheaterMode();
  715. }
  716.  
  717. ytwp.materialPageTransition()
  718. setInterval(ytwp.updatePlayer, 2500);
  719.  
  720. })(window);