Resize YT To Window Size

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

当前为 2017-05-11 提交的版本,查看 最新版本

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