Resize YT To Window Size

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

当前为 2021-02-09 提交的版本,查看 最新版本

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