Resize YT To Window Size

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

当前为 2022-04-19 提交的版本,查看 最新版本

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