Resize YT To Window Size

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

当前为 2022-03-10 提交的版本,查看 最新版本

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