Resize YT To Window Size

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

29.06.2023 itibariyledir. En son verisyonu görün.

  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 132
  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. var enableOnLoad = true;
  28. var scriptToggleKey = 'w';
  29.  
  30. //--- Imported Globals
  31. // yt
  32. // ytcenter
  33. // html5Patched (Youtube+)
  34. // ytplayer
  35. var uw = window;
  36.  
  37. //--- Already Loaded?
  38. // GreaseMonkey loads this script twice for some reason.
  39. if (uw.ytwp) return;
  40.  
  41. //--- Is iframe?
  42. function inIframe () {
  43. try {
  44. return window.self !== window.top;
  45. } catch (e) {
  46. return true;
  47. }
  48. }
  49. if (inIframe()) return;
  50.  
  51. //--- Utils
  52. function isStringType(obj) { return typeof obj === 'string'; }
  53. function isArrayType(obj) { return obj instanceof Array; }
  54. function isObjectType(obj) { return typeof obj === 'object'; }
  55. function isUndefined(obj) { return typeof obj === 'undefined'; }
  56. function buildVenderPropertyDict(propertyNames, value) {
  57. var d = {};
  58. for (var i in propertyNames)
  59. d[propertyNames[i]] = value;
  60. return d;
  61. }
  62. function observe(selector, config, callback) {
  63. var observer = new MutationObserver(function(mutations) {
  64. mutations.forEach(function(mutation){
  65. callback(mutation);
  66. });
  67. });
  68. var target = document.querySelector(selector);
  69. if (!target) {
  70. return null;
  71. }
  72. observer.observe(target, config);
  73. return observer;
  74. }
  75.  
  76. //--- Stylesheet
  77. var JSStyleSheet = function(id) {
  78. this.id = id;
  79. this.stylesheet = '';
  80. };
  81.  
  82. JSStyleSheet.prototype.buildRule = function(selector, styles) {
  83. var s = "";
  84. for (var key in styles) {
  85. s += "\t" + key + ": " + styles[key] + ";\n";
  86. }
  87. return selector + " {\n" + s + "}\n";
  88. };
  89.  
  90. JSStyleSheet.prototype.appendRule = function(selector, k, v) {
  91. if (isArrayType(selector))
  92. selector = selector.join(',\n');
  93. var newStyle;
  94. if (!isUndefined(k) && !isUndefined(v) && isStringType(k)) { // v can be any type (as we stringify it).
  95. var d = {};
  96. d[k] = v;
  97. newStyle = this.buildRule(selector, d);
  98. } else if (!isUndefined(k) && isUndefined(v) && isObjectType(k)) {
  99. newStyle = this.buildRule(selector, k);
  100. } else {
  101. // Invalid Arguments
  102. console.log('Illegal arguments', arguments);
  103. return;
  104. }
  105.  
  106. this.stylesheet += newStyle;
  107. };
  108.  
  109. JSStyleSheet.injectIntoHeader = function(injectedStyleId, stylesheet) {
  110. var styleElement = document.getElementById(injectedStyleId);
  111. if (!styleElement) {
  112. styleElement = document.createElement('style');
  113. styleElement.type = 'text/css';
  114. styleElement.id = injectedStyleId;
  115. document.getElementsByTagName('head')[0].appendChild(styleElement);
  116. }
  117. styleElement.appendChild(document.createTextNode(stylesheet));
  118. };
  119.  
  120. JSStyleSheet.prototype.injectIntoHeader = function(injectedStyleId, stylesheet) {
  121. JSStyleSheet.injectIntoHeader(this.id, this.stylesheet);
  122. };
  123.  
  124. //--- History
  125. var HistoryEvent = function() {}
  126. HistoryEvent.listeners = []
  127.  
  128. HistoryEvent.dispatch = function(state, title, url) {
  129. var stack = this.listeners
  130. for (var i = 0, l = stack.length; i < l; i++) {
  131. stack[i].call(this, state, title, url)
  132. }
  133. }
  134. HistoryEvent.onPushState = function(state, title, url) {
  135. HistoryEvent.dispatch(state, title, url)
  136. return HistoryEvent.origPushState.apply(window.history, arguments)
  137. }
  138. HistoryEvent.onReplaceState = function(state, title, url) {
  139. HistoryEvent.dispatch(state, title, url)
  140. return HistoryEvent.origReplaceState.apply(window.history, arguments)
  141. }
  142. HistoryEvent.inject = function() {
  143. if (!HistoryEvent.injected) {
  144. HistoryEvent.origPushState = window.history.pushState
  145. HistoryEvent.origReplaceState = window.history.replaceState
  146.  
  147. window.history.pushState = HistoryEvent.onPushState
  148. window.history.replaceState = HistoryEvent.onReplaceState
  149. HistoryEvent.injected = true
  150. }
  151. }
  152.  
  153. HistoryEvent.timerId = 0
  154. HistoryEvent.onTick = function() {
  155. var currentPage = window.location.pathname + window.location.search
  156. if (HistoryEvent.lastPage != currentPage) {
  157. HistoryEvent.dispatch({}, document.title, window.location.href)
  158. HistoryEvent.lastPage = currentPage
  159. }
  160. }
  161. HistoryEvent.startTimer = function() {
  162. HistoryEvent.lastPage = window.location.pathname + window.location.search
  163. HistoryEvent.timerId = setInterval(HistoryEvent.onTick, 500)
  164. }
  165. HistoryEvent.stopTimer = function() {
  166. clearInterval(HistoryEvent.timerId)
  167. }
  168. window.ytwpHistoryEvent = HistoryEvent
  169.  
  170.  
  171. //--- Constants
  172. var scriptShortName = 'ytwp'; // YT Window Player
  173. var scriptStyleId = scriptShortName + '-style'; // ytwp-style
  174. var scriptBodyClassId = scriptShortName + '-window-player'; // .ytwp-window-player
  175. var viewingVideoClassId = scriptShortName + '-viewing-video'; // .ytwp-viewing-video
  176. var topOfPageClassId = scriptShortName + '-scrolltop'; // .ytwp-scrolltop
  177.  
  178. var scriptHtmlSelector = 'html:not([fullscreen="true"])';
  179. var scriptBodySelector = 'body.' + scriptBodyClassId; // body.ytwp-window-player
  180. scriptBodySelector += ':not(.enhancer-for-youtube-pinned-player)'; // Support "Enhancer for Youtube" (Pull Request #51)
  181. var scriptSelector = scriptHtmlSelector + ' ' + scriptBodySelector;
  182.  
  183. var videoContainerId = 'player';
  184. var videoContainerPlacemarkerId = scriptShortName + '-placemarker'; // ytwp-placemarker
  185.  
  186. var transitionProperties = ["transition", "-ms-transition", "-moz-transition", "-webkit-transition", "-o-transition"];
  187. var transformProperties = ["transform", "-ms-transform", "-moz-transform", "-webkit-transform", "-o-transform"];
  188.  
  189. //--- YTWP
  190. var ytwp = uw.ytwp = {
  191. scriptShortName: scriptShortName, // YT Window Player
  192. log_: function(logger, args) { logger.apply(console, ['[' + this.scriptShortName + '] '].concat(Array.prototype.slice.call(args))); return 1; },
  193. log: function() { return this.log_(console.log, arguments); },
  194. error: function() { return this.log_(console.error, arguments); },
  195.  
  196. initialized: false,
  197. pageReady: false,
  198. isWatchPage: false,
  199. };
  200.  
  201. ytwp.isWatchUrl = function (url) {
  202. if (!url)
  203. url = uw.location.href;
  204. if (url.match(/https?:\/\/(www\.)?youtube.com\/(c|channel|user)\/[^\/]+\/live/)) {
  205. if (document.querySelector('ytd-browse')) {
  206. return false
  207. } else {
  208. return true
  209. }
  210. }
  211. return url.match(/https?:\/\/(www\.)?youtube.com\/watch\?/);
  212. };
  213.  
  214. ytwp.setTheaterMode = function(enable) {
  215. // ytwp.log('setTheaterMode', enable)
  216.  
  217. var watchElement = document.querySelector('ytd-watch:not([hidden])') || document.querySelector('ytd-watch-flexy:not([hidden])')
  218. if (watchElement) {
  219. var isTheater = watchElement.hasAttribute('theater')
  220. if (enable != isTheater) {
  221. // Note: (Issue #75) ytd-watch-flexy watchElement.querySelector() will find
  222. // Nothing for some reason. We need to query from the document scope.
  223. var sizeButton = document.querySelector(watchElement.tagName + ':not([hidden]) button.ytp-size-button')
  224. if (!sizeButton) {
  225. var screenModeButtons = document.querySelectorAll(watchElement.tagName + ':not([hidden]) button.ytp-screen-mode-settings-button')
  226. sizeButton = screenModeButtons[1] // 2nd button is "Theater mode (t)"
  227. }
  228. if (sizeButton) {
  229. sizeButton.click()
  230. }
  231. }
  232. watchElement.canFitTheater_ = true // When it's too small, it disables the theater mode.
  233. } else if (watchElement = document.querySelector('#page.watch')) {
  234. var isTheater = watchElement.classList.contains('watch-stage-mode')
  235. if (enable != isTheater) {
  236. var sizeButton = watchElement.querySelector('button.ytp-size-button')
  237. if (sizeButton) {
  238. sizeButton.click()
  239. }
  240. }
  241. }
  242. }
  243. ytwp.enterTheaterMode = function() {
  244. // ytwp.log('enterTheaterMode')
  245. if (!document.body.classList.contains(scriptBodyClassId)) {
  246. return
  247. }
  248.  
  249. ytwp.setTheaterMode(true)
  250. }
  251. ytwp.enterTheaterMode();
  252. uw.addEventListener('resize', ytwp.enterTheaterMode);
  253.  
  254. ytwp.detectPlayerUnavailable = function() {
  255. if (document.querySelector('[player-unavailable]')) {
  256. ytwp.event.removeBodyClass()
  257. }
  258. }
  259.  
  260.  
  261. ytwp.init = function() {
  262. ytwp.log('init');
  263. if (!ytwp.initialized) {
  264. ytwp.isWatchPage = ytwp.isWatchUrl();
  265. if (ytwp.isWatchPage) {
  266. ytwp.removeSearchAutofocus();
  267. if (!document.getElementById(scriptStyleId)) {
  268. ytwp.event.initStyle();
  269. }
  270. ytwp.initScroller();
  271. ytwp.initialized = true;
  272. ytwp.pageReady = false;
  273. }
  274. }
  275. ytwp.event.onWatchInit();
  276. if (ytwp.isWatchPage) {
  277. ytwp.html5PlayerFix();
  278. }
  279. }
  280.  
  281. ytwp.initScroller = function() {
  282. // Register listener & Call it now.
  283. uw.addEventListener('scroll', ytwp.onScroll, false);
  284. uw.addEventListener('resize', ytwp.onScroll, false);
  285. ytwp.onScroll();
  286. }
  287.  
  288. ytwp.onScroll = function() {
  289. var viewportHeight = document.documentElement.clientHeight;
  290.  
  291. // topOfPageClassId
  292. if (ytwp.isWatchPage && uw.scrollY == 0) {
  293. document.body.classList.add(topOfPageClassId);
  294. //var player = document.getElementById('movie_player');
  295. //if (player)
  296. // player.focus();
  297. } else {
  298. document.body.classList.remove(topOfPageClassId);
  299. }
  300.  
  301. // viewingVideoClassId
  302. if (ytwp.isWatchPage && uw.scrollY <= viewportHeight) {
  303. document.body.classList.add(viewingVideoClassId);
  304. } else {
  305. document.body.classList.remove(viewingVideoClassId);
  306. }
  307. }
  308.  
  309. ytwp.event = {
  310. initStyle: function() {
  311. ytwp.log('initStyle');
  312. ytwp.style = new JSStyleSheet(scriptStyleId);
  313. ytwp.event.buildStylesheet();
  314. // Duplicate stylesheet targeting data-spf-name if enabled.
  315. if (uw.spf) {
  316. var temp = scriptBodySelector;
  317. scriptBodySelector = 'body[data-spf-name="watch"]';
  318. scriptSelector = scriptHtmlSelector + ' ' + scriptBodySelector
  319. ytwp.event.buildStylesheet();
  320. ytwp.style.appendRule('body[data-spf-name="watch"]:not(.ytwp-window-player) #masthead-positioner', {
  321. 'position': 'absolute',
  322. 'top': playerHeight + ' !important'
  323. });
  324. }
  325. ytwp.style.injectIntoHeader();
  326. },
  327. buildStylesheet: function() {
  328. ytwp.log('buildStylesheet');
  329. //--- Browser Scrollbar
  330. // Chrome/Webkit
  331. ytwp.style.appendRule(scriptBodySelector + '::-webkit-scrollbar', {
  332. 'width': '0 !important',
  333. 'height': '0 !important',
  334. });
  335. // Firefox/Gecko
  336. // Requires about:config flag to be toggled as of FireFox v63
  337. // https://github.com/Zren/ResizeYoutubePlayerToWindowSize/issues/42
  338. ytwp.style.appendRule('html', {
  339. 'scrollbar-width': 'none',
  340. });
  341.  
  342. //--- Video Player
  343. var d;
  344. d = buildVenderPropertyDict(transitionProperties, 'left 0s linear, padding-left 0s linear');
  345. d['padding'] = '0 !important';
  346. d['margin'] = '0 !important';
  347. ytwp.style.appendRule([
  348. scriptBodySelector + ' #player',
  349. scriptBodySelector + '.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible #player',
  350. scriptBodySelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player',
  351. scriptBodySelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player-legacy',
  352. scriptBodySelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #watch7-main-container',
  353. ], d);
  354. //
  355. d = buildVenderPropertyDict(transitionProperties, 'width 0s linear, left 0s linear');
  356.  
  357. // Bugfix for Firefox
  358. // Parts of the header (search box) are hidden under the player.
  359. // Firefox doesn't seem to be using the fixed header+guide yet.
  360. d['float'] = 'initial';
  361.  
  362. // Skinny mode
  363. d['left'] = 0;
  364. d['margin-left'] = 0;
  365.  
  366. ytwp.style.appendRule(scriptBodySelector + ' #player-api', d);
  367.  
  368. // Theater mode
  369. ytwp.style.appendRule(scriptBodySelector + ' .watch-stage-mode #player .player-api', {
  370. 'left': 'initial !important',
  371. 'margin-left': 'initial !important',
  372. });
  373.  
  374. // Hide the cinema/wide mode button since it's useless.
  375. //ytwp.style.appendRule(scriptBodySelector + ' #movie_player .ytp-size-button', 'display', 'none');
  376.  
  377. // !important is mainly for simplicity, but is needed to override the !important styling when the Guide is open due to:
  378. // .sidebar-collapsed #watch7-video, .sidebar-collapsed #watch7-main, .sidebar-collapsed .watch7-playlist { width: 945px!important; }
  379. // Also, Youtube Center resizes #player at element level.
  380. // Don't resize if Youtube+'s html.floater is detected.
  381. // Dont' resize if Youtube+ (Iridium/Material)'s html.iri-always-visible is detected.
  382. ytwp.style.appendRule(
  383. [
  384. scriptSelector + ' #player',
  385. scriptSelector + ' #player-wrap',
  386. scriptSelector + ' #player-api',
  387. scriptHtmlSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' #movie_player',
  388. scriptSelector + ' #player-mole-container',
  389. scriptHtmlSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' .html5-video-container',
  390. scriptHtmlSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' .html5-main-video',
  391. scriptSelector + ' ytd-watch-flexy[theater] #player-theater-container.ytd-watch-flexy',
  392. scriptSelector + ' ytd-watch-flexy[flexy] #player-container-outer.ytd-watch-flexy',
  393. scriptSelector + ' ytd-watch-flexy[flexy] #player-container-inner.ytd-watch-flexy',
  394. scriptSelector + ' ytd-watch-flexy[flexy] #player-container.ytd-watch-flexy',
  395. ],
  396. {
  397. 'width': '100% !important',
  398. 'min-width': '100% !important',
  399. 'max-width': '100% !important',
  400. 'height': playerHeight + ' !important',
  401. 'min-height': playerHeight + ' !important',
  402. 'max-height': playerHeight + ' !important',
  403. }
  404. );
  405.  
  406. ytwp.style.appendRule(
  407. [
  408. scriptSelector + ' #player',
  409. scriptSelector + ' .html5-main-video',
  410. ],
  411. {
  412. 'top': '0 !important',
  413. 'right': '0 !important',
  414. 'bottom': '0 !important',
  415. 'left': '0 !important',
  416. }
  417. );
  418. // Resize #player-unavailable, #player-api
  419. // Using min/max width/height will keep
  420. ytwp.style.appendRule(scriptSelector + ' #player .player-width', 'width', '100% !important');
  421. ytwp.style.appendRule(scriptSelector + ' #player .player-height', 'height', '100% !important');
  422.  
  423. // Fix video overlays
  424. ytwp.style.appendRule([
  425. scriptSelector + ' .html5-video-player .ad-container-single-media-element-annotations', // Ad
  426. scriptSelector + ' .html5-video-player .ytp-upnext', // Autoplay Next Video
  427. ], 'top', '0');
  428.  
  429. // Fix video cropping (object-fit: cover) (Issue #70)
  430. ytwp.style.appendRule(scriptSelector + ' .ytp-fit-cover-video .html5-main-video', 'object-fit', 'contain !important');
  431. // Thumbnail cropping
  432. ytwp.style.appendRule(scriptSelector + ' .ytp-cued-thumbnail-overlay-image', {
  433. 'background-size': 'contain !important',
  434. '-moz-background-size': 'contain !important',
  435. '-webkit-background-size': 'contain !important',
  436. });
  437.  
  438. //--- Video Container Background
  439. ytwp.style.appendRule(scriptSelector + ' #movie_player', 'background-color', '#000000');
  440.  
  441. //--- Move Video Player
  442. ytwp.style.appendRule(scriptSelector + ' #player', {
  443. 'position': 'absolute',
  444. // Already top:0; left: 0;
  445. });
  446. ytwp.style.appendRule(scriptSelector, { // body
  447. 'margin-top': playerHeight,
  448. });
  449.  
  450. // Fix the top right avatar button
  451. ytwp.style.appendRule(scriptSelector + ' button.ytp-button.ytp-cards-button', 'top', '0');
  452.  
  453.  
  454. //--- Sidebar
  455. // Remove the transition delay as you can see it moving on page load.
  456. d = buildVenderPropertyDict(transitionProperties, 'margin-top 0s linear, padding-top 0s linear');
  457. d['margin-top'] = '0 !important';
  458. d['top'] = '0 !important';
  459. ytwp.style.appendRule(scriptSelector + ' #watch7-sidebar', d);
  460.  
  461. ytwp.style.appendRule(scriptSelector + '.cardified-page #watch7-sidebar-contents', 'padding-top', '0');
  462.  
  463. //--- Absolutely position the fixed header.
  464. // Masthead
  465. ytwp.style.appendRule('#skip-navigation.ytd-masthead', 'top', '-150vh'); // Normally -1000px can be shorter than screen (Issue #77)
  466. d = buildVenderPropertyDict(transitionProperties, 'top 0s linear !important');
  467. ytwp.style.appendRule(scriptSelector + '.hide-header-transition #masthead-positioner', d);
  468. ytwp.style.appendRule(scriptSelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  469. 'position': 'absolute',
  470. 'top': playerHeight + ' !important'
  471. });
  472. // Lower masthead below Youtube+'s html.floater
  473. ytwp.style.appendRule('html.floater ' + scriptBodySelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  474. 'z-index': '5',
  475. });
  476. // Autocomplete popup
  477. ytwp.style.appendRule(scriptSelector + ' .sbdd_a', {
  478. 'top': '56px',
  479. });
  480. ytwp.style.appendRule(scriptSelector + '.' + viewingVideoClassId + ' .sbdd_a', {
  481. 'top': 'calc(' + playerHeight + ' + 56px) !important',
  482. 'position': 'absolute !important',
  483. });
  484.  
  485. // Guide
  486. // When watching the video, we need to line it up with the masthead.
  487. ytwp.style.appendRule(scriptSelector + '.' + viewingVideoClassId + ' #appbar-guide-menu', {
  488. 'display': 'initial',
  489. 'position': 'absolute',
  490. 'top': '100% !important' // Masthead height
  491. });
  492. ytwp.style.appendRule(scriptSelector + '.' + viewingVideoClassId + ' #page.watch #guide', {
  493. 'display': 'initial',
  494. 'margin': '0',
  495. 'position': 'initial'
  496. });
  497. // When the guide is open, it adds body{top:-1200px} which messes with the top position.
  498. ytwp.style.appendRule(scriptSelector + '.lock-scrollbar', {
  499. 'top': '0 !important',
  500. 'position': 'static !important',
  501. });
  502.  
  503.  
  504. //---
  505. // MiniPlayer-Bar
  506. ytwp.style.appendRule(scriptSelector + ' #miniplayer-bar #player', {
  507. 'position': 'static',
  508. });
  509. ytwp.style.appendRule(
  510. [
  511. scriptSelector + ' #miniplayer-bar #player',
  512. scriptSelector + ' #miniplayer-bar #player-api',
  513. scriptHtmlSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' #miniplayer-bar #movie_player',
  514. scriptSelector + ' #player-mole-container',
  515. scriptSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' #miniplayer-bar .html5-video-container',
  516. scriptSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' #miniplayer-bar .html5-main-video',
  517. ],
  518. {
  519. 'width': '252px !important',
  520. 'min-width': '252px !important',
  521. 'max-width': '252px !important',
  522. 'height': '142px !important',
  523. 'min-height': '142px !important',
  524. 'max-height': '142px !important',
  525. }
  526. );
  527. // Override inline style (caused by a JS animation) that breaks the miniplayer video
  528. // https://github.com/Zren/ResizeYoutubePlayerToWindowSize/issues/41#issuecomment-439710130
  529. ytwp.style.appendRule('.video-stream.html5-main-video', {
  530. 'top': '0 !important',
  531. });
  532.  
  533. //---
  534. // Hide Scrollbars
  535. ytwp.style.appendRule(scriptSelector + '.' + topOfPageClassId, 'overflow-x', 'hidden');
  536.  
  537.  
  538. //--- Fix Other Possible Style Issues
  539. ytwp.style.appendRule(scriptSelector + ' #placeholder-player', 'display', 'none');
  540. ytwp.style.appendRule(scriptSelector + ' #watch-sidebar-spacer', 'display', 'none');
  541. ytwp.style.appendRule(scriptSelector + ' .skip-nav', 'display', 'none');
  542.  
  543. //--- Whitespace Leftover From Moving The Video
  544. ytwp.style.appendRule(scriptSelector + ' #page.watch', 'padding-top', '0');
  545. ytwp.style.appendRule(scriptSelector + ' .player-branded-banner', 'height', '0');
  546.  
  547. //--- Youtube+ Compatiblity
  548. ytwp.style.appendRule(scriptSelector + ' #body-container', 'position', 'static');
  549. ytwp.style.appendRule(scriptHtmlSelector + '.part_static_size:not(.content-snap-width-skinny-mode) ' + scriptBodySelector + ' .watch-non-stage-mode #player-playlist', 'width', '1066px');
  550.  
  551. //--- Playlist Bar
  552. ytwp.style.appendRule([
  553. scriptSelector + ' #placeholder-playlist',
  554. scriptSelector + ' #player .player-height#watch-appbar-playlist',
  555. ], {
  556. 'height': '540px !important',
  557. 'max-height': '540px !important',
  558. });
  559.  
  560. d = buildVenderPropertyDict(transitionProperties, 'transform 0s linear');
  561. ytwp.style.appendRule(scriptSelector + ' #watch-appbar-playlist', d);
  562. d = buildVenderPropertyDict(transformProperties, 'translateY(0px)');
  563. d['margin-left'] = '0';
  564. d['top'] = 'calc(' + playerHeight + ' + 60px)';
  565. ytwp.style.appendRule(scriptSelector + ' #player .player-height#watch-appbar-playlist', d);
  566. ytwp.style.appendRule(scriptSelector + ' .playlist-videos-list', {
  567. 'max-height': '470px !important',
  568. 'height': 'initial !important',
  569. });
  570.  
  571. // Old layout `&disable_polymer=true`
  572. ytwp.style.appendRule(scriptSelector + ' #player .player-height#watch-appbar-playlist', {
  573. 'left': 'calc((100vw - 1066px)/2 + 640px + 10px)',
  574. 'width': '416px',
  575. });
  576. ytwp.style.stylesheet += '@media screen and (min-height: 630px) and (min-width: 1294px) {\n';
  577. ytwp.style.appendRule(scriptSelector + ' #player .player-height#watch-appbar-playlist', {
  578. 'left': 'calc((100vw - 1280px)/2 + 854px + 10px)',
  579. });
  580. ytwp.style.stylesheet += '}\n @media screen and (min-width: 1720px) and (min-height:980px) {\n';
  581. ytwp.style.appendRule(scriptSelector + ' #player .player-height#watch-appbar-playlist', {
  582. 'left': 'calc((100vw - 1706px)/2 + 1280px + 10px)',
  583. });
  584. ytwp.style.stylesheet += '}\n';
  585.  
  586. //---
  587. // Material UI
  588. ytwp.style.appendRule(scriptSelector + '.ytwp-scrolltop #extra-buttons', 'display', 'none !important');
  589. // ytwp.style.appendRule('body > #player:not(.ytd-watch)', 'display', 'none');
  590. // ytwp.style.appendRule('body.ytwp-viewing-video #content:not(app-header-layout) ytd-page-manager', 'margin-top', '0 !important');
  591. // ytwp.style.appendRule('.ytd-watch-0 #content-separator.ytd-watch', 'margin-top', '0');
  592. ytwp.style.appendRule('ytd-app', 'position', 'static !important');
  593. ytwp.style.appendRule('ytd-watch #top', 'margin-top', '71px !important'); // 56px (topnav height) + 15px (margin)
  594. ytwp.style.appendRule('ytd-watch #container', 'margin-top', '0 !important');
  595. ytwp.style.appendRule('ytd-watch #content-separator', 'margin-top', '0 !important');
  596. // Note: Container is now relative since 2023 June (Issue #77)
  597. ytwp.style.appendRule([
  598. scriptSelector + ' ytd-watch-flexy[theater] #player-wide-container.ytd-watch-flexy',
  599. scriptSelector + ' ytd-watch-flexy[fullscreen] #player-wide-container.ytd-watch-flexy',
  600. ], {
  601. 'position': 'static',
  602. 'height': 0,
  603. 'min-height': 0,
  604. });
  605.  
  606. ytwp.style.appendRule(scriptSelector + '.ytwp-viewing-video ytd-app #masthead-container.ytd-app', {
  607. 'position': 'absolute',
  608. 'top': playerHeight,
  609. 'z-index': 0,
  610. });
  611. ytwp.style.appendRule(scriptSelector + '.ytwp-viewing-video ytd-watch #masthead-positioner', {
  612. 'top': playerHeight + ' !important',
  613. });
  614. ytwp.style.appendRule(scriptSelector + ' .ytp-cued-thumbnail-overlay', 'z-index', '10');
  615.  
  616. //---
  617. // Flexy UI
  618. ytwp.style.appendRule(scriptSelector + ' ytd-watch-flexy[theater] #player-theater-container.ytd-watch-flexy', {
  619. 'position': 'absolute',
  620. 'top': '0',
  621. });
  622. ytwp.style.appendRule(scriptSelector + ' ytd-watch-flexy', 'padding-top', '71px'); // 56px (topnav height) + 15px (margin)
  623. ytwp.style.appendRule(scriptSelector + ' #error-screen', 'z-index', '11');
  624. },
  625. onWatchInit: function() {
  626. ytwp.log('onWatchInit');
  627. if (!ytwp.initialized) return;
  628. if (ytwp.pageReady) return;
  629.  
  630. if (enableOnLoad) {
  631. ytwp.event.addBodyClass();
  632. }
  633. ytwp.pageReady = true;
  634. },
  635. onDispose: function() {
  636. ytwp.log('onDispose');
  637. ytwp.initialized = false;
  638. ytwp.pageReady = false;
  639. ytwp.isWatchPage = false;
  640. },
  641. addBodyClass: function() {
  642. // Insert CSS Into the body so people can style around the effects of this script.
  643. document.body.classList.add(scriptBodyClassId);
  644. ytwp.log('Applied ' + scriptBodySelector);
  645. },
  646. removeBodyClass: function() {
  647. document.body.classList.remove(scriptBodyClassId);
  648. ytwp.log('Removed ' + scriptBodySelector);
  649. },
  650. };
  651.  
  652. ytwp.html5PlayerFix = function() {
  653. ytwp.log('html5PlayerFix');
  654. return;
  655.  
  656. try {
  657. if (!uw.ytcenter // Youtube Center
  658. && !uw.html5Patched // Youtube+
  659. && (!ytwp.html5.app)
  660. && (uw.ytplayer && uw.ytplayer.config)
  661. && (uw.yt && uw.yt.player && uw.yt.player.Application && uw.yt.player.Application.create)
  662. ) {
  663. ytwp.html5.app = ytwp.html5.getPlayerInstance();
  664. }
  665.  
  666. ytwp.html5.update();
  667. ytwp.html5.autohideControls();
  668. } catch (e) {
  669. ytwp.error(e);
  670. }
  671. }
  672.  
  673. ytwp.fixMasthead = function() {
  674. ytwp.log('fixMasthead');
  675. var el = document.querySelector('#masthead-positioner-height-offset');
  676. if (el) {
  677. ytwp.fixMastheadElement(el);
  678. }
  679. }
  680. ytwp.fixMastheadElement = function(el) {
  681. ytwp.log('fixMastheadElement', el);
  682. if (el.style.height) { // != ""
  683. setTimeout(function(){
  684. el.style.height = ""
  685. document.querySelector('#appbar-guide-menu').style.marginTop = "";
  686. }, 0);
  687. }
  688. }
  689.  
  690. JSStyleSheet.injectIntoHeader(scriptStyleId + '-focusfix', 'input#search[autofocus] { display: none; }');
  691. ytwp.removeSearchAutofocus = function() {
  692. var e = document.querySelector('input#search');
  693. // ytwp.log('removeSearchAutofocus', e)
  694. if (e) {
  695. e.removeAttribute('autofocus')
  696. }
  697. }
  698.  
  699. ytwp.registerMastheadFix = function() {
  700. ytwp.log('registerMastheadFix');
  701. // Fix the offset when closing the Share widget (element.style.height = ~275px).
  702.  
  703. observe('#masthead-positioner-height-offset', {
  704. attributes: true,
  705. }, function(mutation) {
  706. console.log(mutation.type, mutation)
  707. if (mutation.attributeName === 'style') {
  708. var el = mutation.target;
  709. if (el.style.height) { // != ""
  710. setTimeout(function(){
  711. el.style.height = ""
  712. document.querySelector('#appbar-guide-menu').style.marginTop = "";
  713. }, 0);
  714. }
  715.  
  716. }
  717. });
  718. }
  719.  
  720. //--- Material UI
  721. ytwp.materialPageTransition = function() {
  722. ytwp.log('materialPageTransition')
  723. ytwp.init();
  724.  
  725. if (ytwp.isWatchUrl()) {
  726. ytwp.removeSearchAutofocus();
  727. if (enableOnLoad) {
  728. ytwp.event.addBodyClass();
  729. }
  730. // if (!ytwp.html5.app) {
  731. if (!ytwp.initialized) {
  732. ytwp.log('materialPageTransition !ytwp.html5.app', ytwp.html5.app)
  733. setTimeout(ytwp.materialPageTransition, 100);
  734. }
  735. var playerApi = document.querySelector('#player-api')
  736. if (playerApi) {
  737. playerApi.click()
  738. }
  739. } else {
  740. ytwp.event.onDispose();
  741. document.body.classList.remove(scriptBodyClassId);
  742. }
  743. ytwp.onScroll();
  744. ytwp.fixMasthead();
  745. ytwp.attemptToUpdatePlayer();
  746. };
  747.  
  748. //--- Listeners
  749. ytwp.registerListeners = function() {
  750. ytwp.registerMaterialListeners();
  751. ytwp.registerMastheadFix();
  752. };
  753.  
  754. ytwp.registerMaterialListeners = function() {
  755. // For Material UI
  756. // HistoryEvent.listeners.push(ytwp.materialPageTransition);
  757. // HistoryEvent.startTimer();
  758. // HistoryEvent.inject();
  759. // HistoryEvent.listeners.push(console.log.bind(console));
  760. window.addEventListener('yt-navigate-start', function(e) {
  761. console.log('yt-navigate-start', e)
  762. console.log('window.location.href', window.location.href)
  763. ytwp.materialPageTransition()
  764. })
  765. window.addEventListener('yt-navigate-finish', function(e) {
  766. console.log('yt-navigate-finish', e)
  767. console.log('window.location.href', window.location.href)
  768. })
  769. };
  770.  
  771. ytwp.main = function() {
  772. ytwp.registerListeners();
  773. ytwp.init();
  774. ytwp.fixMasthead();
  775. };
  776.  
  777. ytwp.main();
  778.  
  779. // ytwp.updatePlayerTimerId = 0;
  780. ytwp.updatePlayerAttempts = -1;
  781. ytwp.updatePlayerMaxAttempts = 150; // 60fps = 2.5sec
  782. ytwp.attemptToUpdatePlayer = function() {
  783. // console.log('ytwp.attemptToUpdatePlayer')
  784. if (0 <= ytwp.updatePlayerAttempts && ytwp.updatePlayerAttempts < ytwp.updatePlayerMaxAttempts) {
  785. ytwp.updatePlayerAttempts = 0;
  786. } else {
  787. ytwp.updatePlayerAttempts = 0;
  788. ytwp.attemptToUpdatePlayerTick();
  789. }
  790. // setTimeout(ytwp.updatePlayer, 10000); /// Just in case it's not caught
  791. }
  792. ytwp.attemptToUpdatePlayerTick = function() {
  793. // console.log('ytwp.attemptToUpdatePlayerTick', ytwp.updatePlayerAttempts)
  794. if (ytwp.updatePlayerAttempts < ytwp.updatePlayerMaxAttempts) {
  795. ytwp.updatePlayerAttempts += 1;
  796. ytwp.updatePlayer();
  797. // ytwp.updatePlayerTimerId = setTimeout(ytwp.attemptToUpdatePlayerTick, 200);
  798. requestAnimationFrame(ytwp.attemptToUpdatePlayerTick);
  799. }
  800. }
  801.  
  802. ytwp.updatePlayer = function() {
  803. ytwp.removeSearchAutofocus();
  804. ytwp.enterTheaterMode();
  805. ytwp.detectPlayerUnavailable();
  806. }
  807.  
  808. ytwp.toggleExtension = function() {
  809. document.body.classList.toggle('ytwp-window-player')
  810. ytwp.setTheaterMode(document.body.classList.contains('ytwp-window-player'))
  811. }
  812.  
  813.  
  814. //--- Main
  815. ytwp.materialPageTransition()
  816. setInterval(ytwp.updatePlayer, 2500);
  817.  
  818.  
  819. //--- Keyboard Shortcut
  820. function childOf(child, ancestor) {
  821. var parent = child.parentNode
  822. while (parent) {
  823. if (parent == ancestor) {
  824. return true
  825. }
  826. parent = parent.parentNode
  827. }
  828. return false
  829. }
  830. window.addEventListener('keypress', function(e){
  831. var isKey = e.key === scriptToggleKey
  832. var validTarget = (
  833. e.target === document.body
  834. || e.target.id === 'player-api'
  835. || e.target.id === 'movie_player'
  836. || childOf(e.target, document.querySelector('#movie_player'))
  837. )
  838.  
  839. if (validTarget && isKey) {
  840. e.preventDefault()
  841. ytwp.toggleExtension()
  842. }
  843. })
  844.  
  845.  
  846. //--- Browser Extension
  847. if (typeof browser !== "undefined") {
  848. browser.runtime.onMessage.addListener(request => {
  849. if (request.id == "toggle") {
  850. ytwp.toggleExtension()
  851.  
  852. return Promise.resolve({
  853. enabled: document.body.classList.contains('ytwp-window-player'),
  854. })
  855. } else {
  856. return Promise.reject(new Error('Unreconized message.id'))
  857. }
  858. });
  859. }
  860.  
  861. })(window);