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-18 提交的版本,查看 最新版本

  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 121
  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. // Override inline style (caused by a JS animation) that breaks the miniplayer video
  470. // https://github.com/Zren/ResizeYoutubePlayerToWindowSize/issues/41#issuecomment-439710130
  471. ytwp.style.appendRule('.video-stream.html5-main-video', {
  472. 'top': '0 !important',
  473. });
  474.  
  475. //---
  476. // Hide Scrollbars
  477. ytwp.style.appendRule(scriptBodyClassSelector + '.' + topOfPageClassId, 'overflow-x', 'hidden');
  478.  
  479.  
  480. //--- Fix Other Possible Style Issues
  481. ytwp.style.appendRule(scriptBodyClassSelector + ' #placeholder-player', 'display', 'none');
  482. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch-sidebar-spacer', 'display', 'none');
  483. ytwp.style.appendRule(scriptBodyClassSelector + ' .skip-nav', 'display', 'none');
  484.  
  485. //--- Whitespace Leftover From Moving The Video
  486. ytwp.style.appendRule(scriptBodyClassSelector + ' #page.watch', 'padding-top', '0');
  487. ytwp.style.appendRule(scriptBodyClassSelector + ' .player-branded-banner', 'height', '0');
  488.  
  489. //--- Youtube+ Compatiblity
  490. ytwp.style.appendRule(scriptBodyClassSelector + ' #body-container', 'position', 'static');
  491. ytwp.style.appendRule('.part_static_size:not(.content-snap-width-skinny-mode) ' + scriptBodyClassSelector + ' .watch-non-stage-mode #player-playlist', 'width', '1066px');
  492.  
  493. //--- Playlist Bar
  494. ytwp.style.appendRule([
  495. scriptBodyClassSelector + ' #placeholder-playlist',
  496. scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist',
  497. ], {
  498. 'height': '540px !important',
  499. 'max-height': '540px !important',
  500. });
  501.  
  502. d = buildVenderPropertyDict(transitionProperties, 'transform 0s linear');
  503. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch-appbar-playlist', d);
  504. d = buildVenderPropertyDict(transformProperties, 'translateY(0px)');
  505. d['margin-left'] = '0';
  506. d['top'] = 'calc(' + playerHeight + ' + 60px)';
  507. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist', d);
  508. ytwp.style.appendRule(scriptBodyClassSelector + ' .playlist-videos-list', {
  509. 'max-height': '470px !important',
  510. 'height': 'initial !important',
  511. });
  512.  
  513. // Old layout `&disable_polymer=true`
  514. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist', {
  515. 'left': 'calc((100vw - 1066px)/2 + 640px + 10px)',
  516. 'width': '416px',
  517. });
  518. ytwp.style.stylesheet += '@media screen and (min-height: 630px) and (min-width: 1294px) {\n';
  519. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist', {
  520. 'left': 'calc((100vw - 1280px)/2 + 854px + 10px)',
  521. });
  522. ytwp.style.stylesheet += '}\n @media screen and (min-width: 1720px) and (min-height:980px) {\n';
  523. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist', {
  524. 'left': 'calc((100vw - 1706px)/2 + 1280px + 10px)',
  525. });
  526. ytwp.style.stylesheet += '}\n';
  527.  
  528. //---
  529. // Material UI
  530. ytwp.style.appendRule(scriptBodyClassSelector + '.ytwp-scrolltop #extra-buttons', 'display', 'none !important');
  531. // ytwp.style.appendRule('body > #player:not(.ytd-watch)', 'display', 'none');
  532. // ytwp.style.appendRule('body.ytwp-viewing-video #content:not(app-header-layout) ytd-page-manager', 'margin-top', '0 !important');
  533. // ytwp.style.appendRule('.ytd-watch-0 #content-separator.ytd-watch', 'margin-top', '0');
  534. ytwp.style.appendRule('ytd-app', 'position', 'static !important');
  535. ytwp.style.appendRule('ytd-watch #top', 'margin-top', '71px !important'); // 56px (topnav height) + 15px (margin)
  536. ytwp.style.appendRule('ytd-watch #container', 'margin-top', '0 !important');
  537. ytwp.style.appendRule('ytd-watch #content-separator', 'margin-top', '0 !important');
  538. ytwp.style.appendRule(scriptBodyClassSelector + '.ytwp-viewing-video ytd-app #masthead-container.ytd-app', {
  539. 'position': 'absolute',
  540. 'top': playerHeight,
  541. 'z-index': 0,
  542. });
  543. ytwp.style.appendRule(scriptBodyClassSelector + '.ytwp-viewing-video ytd-watch #masthead-positioner', {
  544. 'top': playerHeight + ' !important',
  545. });
  546. ytwp.style.appendRule(scriptBodyClassSelector + ' .ytp-cued-thumbnail-overlay', 'z-index', '10');
  547.  
  548. //---
  549. // Flexy UI
  550. ytwp.style.appendRule(scriptBodyClassSelector + ' ytd-watch-flexy[theater] #player-theater-container.ytd-watch-flexy', {
  551. 'position': 'absolute',
  552. 'top': '0',
  553. });
  554. ytwp.style.appendRule(scriptBodyClassSelector + ' ytd-watch-flexy', 'padding-top', '71px'); // 56px (topnav height) + 15px (margin)
  555. ytwp.style.appendRule(scriptBodyClassSelector + ' #error-screen', 'z-index', '11');
  556. },
  557. onWatchInit: function() {
  558. ytwp.log('onWatchInit');
  559. if (!ytwp.initialized) return;
  560. if (ytwp.pageReady) return;
  561.  
  562. ytwp.event.addBodyClass();
  563. ytwp.pageReady = true;
  564. },
  565. onDispose: function() {
  566. ytwp.log('onDispose');
  567. ytwp.initialized = false;
  568. ytwp.pageReady = false;
  569. ytwp.isWatchPage = false;
  570. },
  571. addBodyClass: function() {
  572. // Insert CSS Into the body so people can style around the effects of this script.
  573. document.body.classList.add(scriptBodyClassId);
  574. ytwp.log('Applied ' + scriptBodyClassSelector);
  575. },
  576. };
  577.  
  578. ytwp.html5PlayerFix = function() {
  579. ytwp.log('html5PlayerFix');
  580. return;
  581.  
  582. try {
  583. if (!uw.ytcenter // Youtube Center
  584. && !uw.html5Patched // Youtube+
  585. && (!ytwp.html5.app)
  586. && (uw.ytplayer && uw.ytplayer.config)
  587. && (uw.yt && uw.yt.player && uw.yt.player.Application && uw.yt.player.Application.create)
  588. ) {
  589. ytwp.html5.app = ytwp.html5.getPlayerInstance();
  590. }
  591.  
  592. ytwp.html5.update();
  593. ytwp.html5.autohideControls();
  594. } catch (e) {
  595. ytwp.error(e);
  596. }
  597. }
  598.  
  599. ytwp.fixMasthead = function() {
  600. ytwp.log('fixMasthead');
  601. var el = document.querySelector('#masthead-positioner-height-offset');
  602. if (el) {
  603. ytwp.fixMastheadElement(el);
  604. }
  605. }
  606. ytwp.fixMastheadElement = function(el) {
  607. ytwp.log('fixMastheadElement', el);
  608. if (el.style.height) { // != ""
  609. setTimeout(function(){
  610. el.style.height = ""
  611. document.querySelector('#appbar-guide-menu').style.marginTop = "";
  612. }, 0);
  613. }
  614. }
  615.  
  616. JSStyleSheet.injectIntoHeader(scriptStyleId + '-focusfix', 'input#search[autofocus] { display: none; }');
  617. ytwp.removeSearchAutofocus = function() {
  618. var e = document.querySelector('input#search');
  619. // ytwp.log('removeSearchAutofocus', e)
  620. if (e) {
  621. e.removeAttribute('autofocus')
  622. }
  623. }
  624.  
  625. ytwp.registerMastheadFix = function() {
  626. ytwp.log('registerMastheadFix');
  627. // Fix the offset when closing the Share widget (element.style.height = ~275px).
  628.  
  629. observe('#masthead-positioner-height-offset', {
  630. attributes: true,
  631. }, function(mutation) {
  632. console.log(mutation.type, mutation)
  633. if (mutation.attributeName === 'style') {
  634. var el = mutation.target;
  635. if (el.style.height) { // != ""
  636. setTimeout(function(){
  637. el.style.height = ""
  638. document.querySelector('#appbar-guide-menu').style.marginTop = "";
  639. }, 0);
  640. }
  641.  
  642. }
  643. });
  644. }
  645.  
  646. //--- Material UI
  647. ytwp.materialPageTransition = function() {
  648. ytwp.log('materialPageTransition')
  649. ytwp.init();
  650.  
  651. if (ytwp.isWatchUrl()) {
  652. ytwp.removeSearchAutofocus();
  653. ytwp.event.addBodyClass();
  654. // if (!ytwp.html5.app) {
  655. if (!ytwp.initialized) {
  656. ytwp.log('materialPageTransition !ytwp.html5.app', ytwp.html5.app)
  657. setTimeout(ytwp.materialPageTransition, 100);
  658. }
  659. var playerApi = document.querySelector('#player-api')
  660. if (playerApi) {
  661. playerApi.click()
  662. }
  663. } else {
  664. ytwp.event.onDispose();
  665. document.body.classList.remove(scriptBodyClassId);
  666. }
  667. ytwp.onScroll();
  668. ytwp.fixMasthead();
  669. ytwp.attemptToUpdatePlayer();
  670. };
  671.  
  672. //--- Listeners
  673. ytwp.registerListeners = function() {
  674. ytwp.registerMaterialListeners();
  675. ytwp.registerMastheadFix();
  676. };
  677.  
  678. ytwp.registerMaterialListeners = function() {
  679. // For Material UI
  680. HistoryEvent.listeners.push(ytwp.materialPageTransition);
  681. HistoryEvent.startTimer();
  682. // HistoryEvent.inject();
  683. // HistoryEvent.listeners.push(console.log.bind(console));
  684. };
  685.  
  686. ytwp.main = function() {
  687. ytwp.registerListeners();
  688. ytwp.init();
  689. ytwp.fixMasthead();
  690. };
  691.  
  692. ytwp.main();
  693.  
  694. // ytwp.updatePlayerTimerId = 0;
  695. ytwp.updatePlayerAttempts = -1;
  696. ytwp.updatePlayerMaxAttempts = 150; // 60fps = 2.5sec
  697. ytwp.attemptToUpdatePlayer = function() {
  698. console.log('ytwp.attemptToUpdatePlayer')
  699. if (0 <= ytwp.updatePlayerAttempts && ytwp.updatePlayerAttempts < ytwp.updatePlayerMaxAttempts) {
  700. ytwp.updatePlayerAttempts = 0;
  701. } else {
  702. ytwp.updatePlayerAttempts = 0;
  703. ytwp.attemptToUpdatePlayerTick();
  704. }
  705. // setTimeout(ytwp.updatePlayer, 10000); /// Just in case it's not caught
  706. }
  707. ytwp.attemptToUpdatePlayerTick = function() {
  708. console.log('ytwp.attemptToUpdatePlayerTick', ytwp.updatePlayerAttempts)
  709. if (ytwp.updatePlayerAttempts < ytwp.updatePlayerMaxAttempts) {
  710. ytwp.updatePlayerAttempts += 1;
  711. ytwp.updatePlayer();
  712. // ytwp.updatePlayerTimerId = setTimeout(ytwp.attemptToUpdatePlayerTick, 200);
  713. requestAnimationFrame(ytwp.attemptToUpdatePlayerTick);
  714. }
  715. }
  716.  
  717. ytwp.updatePlayer = function() {
  718. ytwp.removeSearchAutofocus();
  719. ytwp.enterTheaterMode();
  720. }
  721.  
  722. ytwp.materialPageTransition()
  723. setInterval(ytwp.updatePlayer, 2500);
  724.  
  725. })(window);