Resize YT To Window Size

Moves the YouTube video to the top of the website and resizes it to the window size.

当前为 2016-09-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Resize YT To Window Size
  3. // @description Moves the YouTube video to the top of the website and resizes it to the window size.
  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 86
  9. // @include http*://*.youtube.com/*
  10. // @include http*://youtube.com/*
  11. // @include http*://*.youtu.be/*
  12. // @include http*://youtu.be/*
  13. // ==/UserScript==
  14.  
  15. // Github: https://github.com/Zren/ResizeYoutubePlayerToWindowSize
  16. // GreasyFork: https://greasyfork.org/scripts/811-resize-yt-to-window-size
  17. // OpenUserJS.org: https://openuserjs.org/scripts/zren/Resize_YT_To_Window_Size
  18. // Userscripts.org: http://userscripts-mirror.org/scripts/show/153699
  19.  
  20. (function (window) {
  21. "use strict";
  22. //--- Imported Globals
  23. // yt
  24. // ytcenter
  25. // html5Patched (Youtube+)
  26. // ytplayer
  27. var uw = window;
  28.  
  29. //--- Already Loaded?
  30. // GreaseMonkey loads this script twice for some reason.
  31. if (uw.ytwp) return;
  32. //--- Is iframe?
  33. function inIframe () {
  34. try {
  35. return window.self !== window.top;
  36. } catch (e) {
  37. return true;
  38. }
  39. }
  40. if (inIframe()) return;
  41.  
  42. //--- Utils
  43. function isStringType(obj) { return typeof obj === 'string'; }
  44. function isArrayType(obj) { return obj instanceof Array; }
  45. function isObjectType(obj) { return typeof obj === 'object'; }
  46. function isUndefined(obj) { return typeof obj === 'undefined'; }
  47. function buildVenderPropertyDict(propertyNames, value) {
  48. var d = {};
  49. for (var i in propertyNames)
  50. d[propertyNames[i]] = value;
  51. return d;
  52. }
  53. function addClass(el, value) {
  54. var classes = value.split(' ');
  55. for (var i = 0; i < classes.length; i++) {
  56. el.classList.add(classes[i]);
  57. }
  58. }
  59. function removeClass(el, value) {
  60. var classes = value.split(' ');
  61. for (var i = 0; i < classes.length; i++) {
  62. el.classList.remove(classes[i]);
  63. }
  64. }
  65.  
  66. //--- Stylesheet
  67. var JSStyleSheet = function(id) {
  68. this.id = id;
  69. this.stylesheet = '';
  70. };
  71.  
  72. JSStyleSheet.prototype.buildRule = function(selector, styles) {
  73. var s = "";
  74. for (var key in styles) {
  75. s += "\t" + key + ": " + styles[key] + ";\n";
  76. }
  77. return selector + " {\n" + s + "}\n";
  78. };
  79.  
  80. JSStyleSheet.prototype.appendRule = function(selector, k, v) {
  81. if (isArrayType(selector))
  82. selector = selector.join(',\n');
  83. var newStyle;
  84. if (!isUndefined(k) && !isUndefined(v) && isStringType(k)) { // v can be any type (as we stringify it).
  85. var d = {};
  86. d[k] = v;
  87. newStyle = this.buildRule(selector, d);
  88. } else if (!isUndefined(k) && isUndefined(v) && isObjectType(k)) {
  89. newStyle = this.buildRule(selector, k);
  90. } else {
  91. // Invalid Arguments
  92. console.log('Illegal arguments', arguments);
  93. return;
  94. }
  95.  
  96. this.stylesheet += newStyle;
  97. };
  98.  
  99. JSStyleSheet.injectIntoHeader = function(injectedStyleId, stylesheet) {
  100. var styleElement = document.getElementById(injectedStyleId);
  101. if (!styleElement) {
  102. styleElement = document.createElement('style');
  103. styleElement.type = 'text/css';
  104. styleElement.id = injectedStyleId;
  105. document.getElementsByTagName('head')[0].appendChild(styleElement);
  106. }
  107. styleElement.appendChild(document.createTextNode(stylesheet));
  108. };
  109.  
  110. JSStyleSheet.prototype.injectIntoHeader = function(injectedStyleId, stylesheet) {
  111. JSStyleSheet.injectIntoHeader(this.id, this.stylesheet);
  112. };
  113.  
  114. //--- Constants
  115. var scriptShortName = 'ytwp'; // YT Window Player
  116. var scriptStyleId = scriptShortName + '-style'; // ytwp-style
  117. var scriptBodyClassId = scriptShortName + '-window-player'; // .ytwp-window-player
  118. var viewingVideoClassId = scriptShortName + '-viewing-video'; // .ytwp-viewing-video
  119. var topOfPageClassId = scriptShortName + '-scrolltop'; // .ytwp-scrolltop
  120. var scriptBodyClassSelector = 'body.' + scriptBodyClassId; // body.ytwp-window-player
  121.  
  122. var videoContainerId = 'player';
  123. var videoContainerPlacemarkerId = scriptShortName + '-placemarker'; // ytwp-placemarker
  124.  
  125. var transitionProperties = ["transition", "-ms-transition", "-moz-transition", "-webkit-transition", "-o-transition"];
  126. var transformProperties = ["transform", "-ms-transform", "-moz-transform", "-webkit-transform", "-o-transform"];
  127.  
  128. //--- YTWP
  129. var ytwp = uw.ytwp = {
  130. scriptShortName: scriptShortName, // YT Window Player
  131. log_: function(logger, args) { logger.apply(console, ['[' + this.scriptShortName + '] '].concat(Array.prototype.slice.call(args))); return 1; },
  132. log: function() { return this.log_(console.log, arguments); },
  133. error: function() { return this.log_(console.error, arguments); },
  134.  
  135. initialized: false,
  136. pageReady: false,
  137. watchPage: false,
  138. };
  139.  
  140. ytwp.util = {
  141. isWatchUrl: function (url) {
  142. if (!url)
  143. url = uw.location.href;
  144. return url.match(/https?:\/\/(www\.)?youtube.com\/watch\?/);
  145. }
  146. };
  147.  
  148. ytwp.html5 = {
  149. app: null,
  150. YTRect: null,
  151. YTApplication: null,
  152. playerInstances: null,
  153. moviePlayerElement: null,
  154. };
  155. ytwp.html5.getPlayerRect = function() {
  156. var moviePlayerElement = this.element || ytwp.html5.moviePlayerElement || document.querySelector('movie_player');
  157. return new ytwp.html5.YTRect(moviePlayerElement.clientWidth, moviePlayerElement.clientHeight);
  158. };
  159. ytwp.html5.getApplicationClass = function() {
  160. if (ytwp.html5.YTApplication === null) {
  161. var testEl = document.createElement('div');
  162. var testAppInstance = uw.yt.player.Application.create(testEl, {});
  163. // var testAppInstance = uw.yt.player.Application.create("player-api", uw.ytplayer.config);
  164. ytwp.html5.YTApplication = testAppInstance.constructor;
  165.  
  166. // Cleanup testAppInstance
  167. var playerInstances = ytwp.html5.getPlayerInstances();
  168.  
  169. var testAppInstanceKey = null;
  170. Object.keys(playerInstances).forEach(function(key) {
  171. if (playerInstances[key] === testAppInstance) {
  172. testAppInstanceKey = key;
  173. }
  174. });
  175. testAppInstance.dispose();
  176. delete playerInstances[testAppInstanceKey];
  177. }
  178. return ytwp.html5.YTApplication;
  179. };
  180. ytwp.html5.getPlayerInstances = function() {
  181. if (ytwp.html5.playerInstances === null) {
  182. var YTApplication = ytwp.html5.getApplicationClass();
  183. if (YTApplication === null)
  184. return null;
  185.  
  186. // Use yt.player.Application.create to find the playerInstancesKey.
  187. // function (a,b){try{var c=e9.D(a);if(e9.o[c]){try{e9.o[c].dispose()}catch(e){Fi(e)}e9.o[c]=null}var d=new e9(a,b);Kb(d,function(){e9.o[c]=null});return e9.o[c]=d}catch(e){throw Fi(e),e.stack;}}
  188. var appCreateRegex = /^function \(a,b\)\{try\{var c=([a-zA-Z_$][\w_$]*)\.([a-zA-Z_$][\w_$]*)\(a\);if\(([a-zA-Z_$][\w_$]*)\.([a-zA-Z_$][\w_$]*)\[c\]\)/;
  189. var fnString = yt.player.Application.create.toString();
  190. var m = appCreateRegex.exec(fnString);
  191. if (m) {
  192. var playerInstancesKey = m[4];
  193. ytwp.html5.playerInstances = YTApplication[playerInstancesKey];
  194. } else {
  195. ytwp.error('Error trying to find playerInstancesKey.', fnString);
  196. }
  197. ytwp.html5.playerInstances = YTApplication[playerInstancesKey];
  198. }
  199.  
  200. return ytwp.html5.playerInstances;
  201. };
  202. ytwp.html5.getPlayerInstance = function() {
  203. if (!ytwp.html5.app) {
  204. var playerInstances = ytwp.html5.getPlayerInstances();
  205. ytwp.log('playerInstances', playerInstances);
  206. var appInstance = null;
  207. var appInstanceKey = null;
  208. Object.keys(playerInstances).forEach(function(key) {
  209. appInstanceKey = key;
  210. appInstance = playerInstances[key];
  211. });
  212. ytwp.html5.app = appInstance;
  213. }
  214. return ytwp.html5.app;
  215. };
  216. ytwp.html5.autohideControls = function() {
  217. var moviePlayerElement = document.getElementById('movie_player');
  218. if (!moviePlayerElement) return;
  219. // ytwp.log(moviePlayerElement.classList);
  220. 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');
  221. addClass(moviePlayerElement, 'autominimize-progress-bar autohide-controls hide-controls-when-cued');
  222. // ytwp.log(moviePlayerElement.classList);
  223. };
  224. ytwp.html5.update = function() {
  225. if (!ytwp.html5.playerInstances)
  226. return;
  227. for (var key in ytwp.html5.playerInstances) {
  228. var playerInstance = ytwp.html5.playerInstances[key];
  229. ytwp.html5.updatePlayerInstance(playerInstance);
  230. }
  231. };
  232. ytwp.html5.replaceClientRect = function(app, moviePlayerKey, clientRectFnKey) {
  233. var moviePlayer = app[moviePlayerKey];
  234. ytwp.html5.moviePlayerElement = moviePlayer.element;
  235. ytwp.html5.YTRect = moviePlayer[clientRectFnKey].call(moviePlayer).constructor;
  236. moviePlayer[clientRectFnKey] = ytwp.html5.getPlayerRect;
  237. };
  238. ytwp.html5.setRectFn = function(app, moviePlayerKey, clientRectFnKey) {
  239. ytwp.html5.moviePlayerElement = document.getElementById('movie_player');
  240. var moviePlayer = app[moviePlayerKey];
  241. ytwp.html5.YTRect = moviePlayer[clientRectFnKey].call(moviePlayer).constructor;
  242. moviePlayer.constructor.prototype[clientRectFnKey] = ytwp.html5.getPlayerRect;
  243. };
  244. ytwp.html5.updatePlayerInstance = function(app) {
  245. if (!app) {
  246. return;
  247. }
  248.  
  249. var moviePlayerElement = document.getElementById('movie_player');
  250. var moviePlayer = null;
  251. var moviePlayerKey = null;
  252.  
  253. // function (a,b){return this.isDisposed()?!1:this.R.P.apply(this.R,arguments)}
  254. var applyFnRegex = /^function \(a,b\)\{return this\.isDisposed\(\)\?!1:this\.([a-zA-Z_$][\w_$]*)\.([a-zA-Z_$][\w_$]*)\.apply\(this\.([a-zA-Z_$][\w_$]*),arguments\)\}$/;
  255. var applyFnKey = null;
  256. var applyKey1 = null;
  257. var applyKey2 = null;
  258.  
  259. // 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}
  260. // 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}
  261. var clientRectFnRegex1 = /^(function \(a\)\{var b=this\.([a-zA-Z_$][\w_$]*)\.([a-zA-Z_$][\w_$]*)\(\)).*(\|\|\(c\.height\+=30\);return c})$/;
  262. // 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)}
  263. // Tail: return new H(this.element.clientWidth,this.element.clientHeight)}
  264. // 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)}
  265. // Tail: return new g.ef(this.element.clientWidth,this.element.clientHeight)}
  266. var clientRectFnRegex2 = /^(function \()(.|\n)*(return new ([a-zA-Z_$][\w_$]*\.)?([a-zA-Z_$][\w_$]*)\(this\.element\.clientWidth,this\.element\.clientHeight\)})$/;
  267. var clientRectFn = null;
  268. var clientRectFnKey = null;
  269.  
  270. var fnAlreadyReplacedCount = 0;
  271.  
  272. for (var key1 in app) {
  273. var val1 = app[key1];//console.log(key1, val1);
  274. if (typeof val1 === 'object' && val1 !== null && val1.element === moviePlayerElement) {
  275. moviePlayer = val1;
  276. moviePlayerKey = key1;
  277.  
  278. for (var key2 in moviePlayer) {
  279. var val2 = moviePlayer[key2];//console.log(key1, key2, val2);
  280. if (typeof val2 === 'function') {
  281. var fnString = val2.toString();
  282. // console.log(fnString);
  283. if (clientRectFn === null && (clientRectFnRegex1.test(fnString) || clientRectFnRegex2.test(fnString))) {
  284. clientRectFn = val2;
  285. clientRectFnKey = key2;
  286. } else if (val2 === ytwp.html5.getPlayerRect) {
  287. fnAlreadyReplacedCount += 1;
  288. clientRectFn = val2;
  289. clientRectFnKey = key2;
  290. } else {
  291. // console.log(key1, key2, val2, '[Not Used]');
  292. }
  293. }
  294. }
  295. } else if (typeof val1 === 'object' && val1 !== null && typeof val1.logEvent === 'function') {
  296. applyKey1 = key1;
  297. for (var key2 in val1) {
  298. var val2 = val1[key2];//console.log(key1, key2, val2);
  299. if (typeof val2 === 'function') {
  300. var fnString = val2.toString();
  301. // console.log(fnString);
  302. if (applyKey2 === null && applyFnRegex.test(fnString)) {
  303. applyKey2 = key2;
  304. }
  305. }
  306. }
  307. } else if (typeof val1 === 'function') {
  308. var fnString = val1.toString();
  309. if (applyFnRegex.test(fnString)) {
  310. applyFnKey = key1;
  311. }
  312. }
  313. }
  314.  
  315. if (fnAlreadyReplacedCount > 0) {
  316. // return;
  317. }
  318.  
  319. if (moviePlayer === null || clientRectFn === null) {
  320. console.log('[ytwp] ', '[Error]', 'HTML5 Player has changed or there\'s multiple playerInstances and this one has been destroyed.');
  321. console.log('moviePlayer', moviePlayerKey, moviePlayer);
  322. console.log('clientRectFn', clientRectFnKey, clientRectFn);
  323. console.log('fnAlreadyReplacedCount', fnAlreadyReplacedCount);
  324. if (moviePlayer === null) {
  325. console.log('Debugging: moviePlayer');
  326. var table = [];
  327. Object.keys(app).forEach(function(key1) {
  328. var val1 = app[key1];
  329. table.push({
  330. key: key1,
  331. element: typeof val1 === 'object' && val1 !== null && val1.element === moviePlayerElement,
  332. val: val1,
  333. });
  334. });
  335. console.table(table);
  336. }
  337. if (moviePlayer != null) {
  338. console.log('Debugging: clientRectFn');
  339. var table = [];
  340. for (var key2 in moviePlayer) {
  341. var val2 = moviePlayer[key2];
  342. table.push({
  343. key: key2,
  344. returns: moviePlayer[key2] && moviePlayer[key2].toString().indexOf('return'),
  345. src: moviePlayer[key2] && moviePlayer[key2].toString(),
  346. });
  347. }
  348. console.table(table);
  349. }
  350. return;
  351. }
  352. ytwp.html5.setRectFn(app, moviePlayerKey, clientRectFnKey);
  353.  
  354. if (applyFnKey) { // Probably not needed.
  355. ytwp.log('applyFnKey', applyFnKey);
  356. app[applyFnKey]('resize', ytwp.html5.getPlayerRect());
  357. } else if (applyKey1 && applyKey2) {
  358. ytwp.log('applyKey', applyKey1, applyKey2);
  359. app[applyKey1][applyKey2]('resize', ytwp.html5.getPlayerRect());
  360. } else {
  361. ytwp.log('applyFn not found');
  362. //app.oa.T('resize', ytwp.html5.getPlayerRect()); // tempfix
  363. }
  364. };
  365.  
  366.  
  367.  
  368. ytwp.event = {
  369. init: function() {
  370. ytwp.log('init');
  371. if (!ytwp.initialized) {
  372. ytwp.isWatchPage = ytwp.util.isWatchUrl();
  373. if (ytwp.isWatchPage) {
  374. if (!document.getElementById(scriptStyleId)) {
  375. ytwp.event.initStyle();
  376. }
  377. ytwp.event.initScroller();
  378. ytwp.initialized = true;
  379. ytwp.pageReady = false;
  380. }
  381. }
  382. ytwp.event.onWatchInit();
  383. if (ytwp.isWatchPage) {
  384. ytwp.event.html5PlayerFix();
  385. }
  386. },
  387. initScroller: function() {
  388. // Register listener & Call it now.
  389. uw.addEventListener('scroll', ytwp.event.onScroll, false);
  390. uw.addEventListener('resize', ytwp.event.onScroll, false);
  391. ytwp.event.onScroll();
  392. },
  393. onScroll: function() {
  394. var viewportHeight = document.documentElement.clientHeight;
  395.  
  396. // topOfPageClassId
  397. if (uw.scrollY == 0) {
  398. document.body.classList.add(topOfPageClassId);
  399. //var player = document.getElementById('movie_player');
  400. //if (player)
  401. // player.focus();
  402. } else {
  403. document.body.classList.remove(topOfPageClassId);
  404. }
  405.  
  406. // viewingVideoClassId
  407. if (uw.scrollY <= viewportHeight) {
  408. document.body.classList.add(viewingVideoClassId);
  409. } else {
  410. document.body.classList.remove(viewingVideoClassId);
  411. }
  412. },
  413. initStyle: function() {
  414. ytwp.log('initStyle');
  415. ytwp.style = new JSStyleSheet(scriptStyleId);
  416. ytwp.event.buildStylesheet();
  417. // Duplicate stylesheet targeting data-spf-name if enabled.
  418. if (uw.spf) {
  419. var temp = scriptBodyClassSelector;
  420. scriptBodyClassSelector = 'body[data-spf-name="watch"]';
  421. ytwp.event.buildStylesheet();
  422. ytwp.style.appendRule('body[data-spf-name="watch"]:not(.ytwp-window-player) #masthead-positioner', {
  423. 'position': 'absolute',
  424. 'top': '100% !important'
  425. });
  426. }
  427. ytwp.style.injectIntoHeader();
  428. },
  429. buildStylesheet: function() {
  430. ytwp.log('buildStylesheet');
  431. //--- Video Player
  432. var d;
  433. d = buildVenderPropertyDict(transitionProperties, 'left 0s linear, padding-left 0s linear');
  434. d['padding'] = '0 !important';
  435. d['margin'] = '0 !important';
  436. ytwp.style.appendRule([
  437. scriptBodyClassSelector + ' #player',
  438. scriptBodyClassSelector + '.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible #player',
  439. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player',
  440. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player-legacy',
  441. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #watch7-main-container',
  442. ], d);
  443. //
  444. d = buildVenderPropertyDict(transitionProperties, 'width 0s linear, left 0s linear');
  445.  
  446. // Bugfix for Firefox
  447. // Parts of the header (search box) are hidden under the player.
  448. // Firefox doesn't seem to be using the fixed header+guide yet.
  449. d['float'] = 'initial';
  450.  
  451. // Skinny mode
  452. d['left'] = 0;
  453. d['margin-left'] = 0;
  454.  
  455. ytwp.style.appendRule(scriptBodyClassSelector + ' #player-api', d);
  456.  
  457. // Theatre mode
  458. ytwp.style.appendRule(scriptBodyClassSelector + ' .watch-stage-mode #player .player-api', {
  459. 'left': 'initial',
  460. 'margin-left': 'initial',
  461. });
  462. // Hide the cinema/wide mode button since it's useless.
  463. //ytwp.style.appendRule(scriptBodyClassSelector + ' #movie_player .ytp-size-button', 'display', 'none');
  464.  
  465. // !important is mainly for simplicity, but is needed to override the !important styling when the Guide is open due to:
  466. // .sidebar-collapsed #watch7-video, .sidebar-collapsed #watch7-main, .sidebar-collapsed .watch7-playlist { width: 945px!important; }
  467. // Also, Youtube Center resizes #player at element level.
  468. // Don't resize if Youtube+'s html.floater is detected.
  469. ytwp.style.appendRule(
  470. [
  471. scriptBodyClassSelector + ' #player',
  472. scriptBodyClassSelector + ' #player-api',
  473. 'html:not(.floater) ' + scriptBodyClassSelector + ' #movie_player',
  474. scriptBodyClassSelector + ' #player-mole-container',
  475. 'html:not(.floater) ' + scriptBodyClassSelector + ' .html5-video-container',
  476. 'html:not(.floater) ' + scriptBodyClassSelector + ' .html5-main-video',
  477. ],
  478. {
  479. 'width': '100% !important',
  480. 'min-width': '100% !important',
  481. 'max-width': '100% !important',
  482. 'height': '100% !important',
  483. 'min-height': '100% !important',
  484. 'max-height': '100% !important',
  485. }
  486. );
  487.  
  488. ytwp.style.appendRule(
  489. [
  490. scriptBodyClassSelector + ' #player',
  491. scriptBodyClassSelector + ' .html5-main-video',
  492. ],
  493. {
  494. 'top': '0 !important',
  495. 'right': '0 !important',
  496. 'bottom': '0 !important',
  497. 'left': '0 !important',
  498. }
  499. );
  500. // Resize #player-unavailable, #player-api
  501. // Using min/max width/height will keep
  502. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-width', 'width', '100% !important');
  503. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height', 'height', '100% !important');
  504.  
  505. // Fix video overlays
  506. ytwp.style.appendRule([
  507. scriptBodyClassSelector + ' .html5-video-player .ad-container-single-media-element-annotations', // Ad
  508. scriptBodyClassSelector + ' .html5-video-player .ytp-upnext', // Autoplay Next Video
  509. ], 'top', '0');
  510.  
  511. //--- Move Video Player
  512. ytwp.style.appendRule(scriptBodyClassSelector + ' #player', {
  513. 'position': 'absolute',
  514. // Already top:0; left: 0;
  515. });
  516. ytwp.style.appendRule(scriptBodyClassSelector, { // body
  517. 'margin-top': '100vh',
  518. });
  519.  
  520.  
  521. //--- Sidebar
  522. // Remove the transition delay as you can see it moving on page load.
  523. d = buildVenderPropertyDict(transitionProperties, 'margin-top 0s linear, padding-top 0s linear');
  524. d['margin-top'] = '0 !important';
  525. d['top'] = '0 !important';
  526. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch7-sidebar', d);
  527.  
  528. ytwp.style.appendRule(scriptBodyClassSelector + '.cardified-page #watch7-sidebar-contents', 'padding-top', '0');
  529.  
  530. //--- Absolutely position the fixed header.
  531. // Masthead
  532. d = buildVenderPropertyDict(transitionProperties, 'top 0s linear !important');
  533. ytwp.style.appendRule(scriptBodyClassSelector + '.hide-header-transition #masthead-positioner', d);
  534. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  535. 'position': 'absolute',
  536. 'top': '100% !important'
  537. });
  538. // Fix the offset when closing the Share widget (element.style.height = ~275px).
  539. ytwp.style.appendRule(scriptBodyClassSelector + '.appbar-hidden #masthead-positioner-height-offset', 'height', '50px !important');
  540. ytwp.style.appendRule(scriptBodyClassSelector + ' #masthead-positioner-height-offset', 'height', '90px !important');
  541. // Lower masthead below Youtube+'s html.floater
  542. ytwp.style.appendRule('html.floater ' + scriptBodyClassSelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  543. 'z-index': '5',
  544. });
  545.  
  546. // Guide
  547. // When watching the video, we need to line it up with the masthead.
  548. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #appbar-guide-menu', {
  549. 'display': 'initial',
  550. 'position': 'absolute',
  551. 'top': '100% !important' // Masthead height
  552. });
  553. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #page.watch #guide', {
  554. 'display': 'initial',
  555. 'margin': '0',
  556. 'position': 'initial'
  557. });
  558.  
  559. //---
  560. // Hide Scrollbars
  561. ytwp.style.appendRule(scriptBodyClassSelector + '.' + topOfPageClassId, 'overflow-x', 'hidden');
  562.  
  563.  
  564. //--- Fix Other Possible Style Issues
  565. ytwp.style.appendRule(scriptBodyClassSelector + ' #placeholder-player', 'display', 'none');
  566. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch-sidebar-spacer', 'display', 'none');
  567. ytwp.style.appendRule(scriptBodyClassSelector + ' .skip-nav', 'display', 'none');
  568.  
  569. //--- Whitespace Leftover From Moving The Video
  570. ytwp.style.appendRule(scriptBodyClassSelector + ' #page.watch', 'padding-top', '0');
  571. ytwp.style.appendRule(scriptBodyClassSelector + ' .player-branded-banner', 'height', '0');
  572.  
  573. //--- Youtube+ Compatiblity
  574. ytwp.style.appendRule(scriptBodyClassSelector + ' #body-container', 'position', 'static');
  575. ytwp.style.appendRule('.part_static_size:not(.content-snap-width-skinny-mode) ' + scriptBodyClassSelector + ' .watch-non-stage-mode #player-playlist', 'width', '1066px');
  576.  
  577. //--- Playlist Bar
  578. ytwp.style.appendRule([
  579. scriptBodyClassSelector + ' #placeholder-playlist',
  580. scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist',
  581. ], {
  582. 'height': '540px !important',
  583. 'max-height': '540px !important',
  584. });
  585.  
  586. d = buildVenderPropertyDict(transitionProperties, 'transform 0s linear');
  587. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch-appbar-playlist', d);
  588. d = buildVenderPropertyDict(transformProperties, 'translateY(0px)');
  589. d['margin-left'] = '0';
  590. d['top'] = 'calc(100vh + 60px)';
  591. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height#watch-appbar-playlist', d);
  592. ytwp.style.appendRule(scriptBodyClassSelector + ' .playlist-videos-list', {
  593. 'max-height': '470px !important',
  594. 'height': 'initial !important',
  595. });
  596. },
  597. onWatchInit: function() {
  598. ytwp.log('onWatchInit');
  599. if (!ytwp.initialized) return;
  600. if (ytwp.pageReady) return;
  601.  
  602. ytwp.event.addBodyClass();
  603. ytwp.pageReady = true;
  604. },
  605. onDispose: function() {
  606. ytwp.log('onDispose');
  607. ytwp.initialized = false;
  608. ytwp.pageReady = false;
  609. ytwp.isWatchPage = false;
  610. ytwp.html5.app = null;
  611. // ytwp.html5.YTRect = null;
  612. ytwp.html5.YTApplication = null;
  613. ytwp.html5.playerInstances = null;
  614. //ytwp.html5.moviePlayerElement = null;
  615. },
  616. addBodyClass: function() {
  617. // Insert CSS Into the body so people can style around the effects of this script.
  618. document.body.classList.add(scriptBodyClassId);
  619. ytwp.log('Applied ' + scriptBodyClassSelector);
  620. },
  621. html5PlayerFix: function() {
  622. ytwp.log('html5PlayerFix');
  623.  
  624. try {
  625. if (!uw.ytcenter // Youtube Center
  626. && !uw.html5Patched // Youtube+
  627. && (!ytwp.html5.app)
  628. && (uw.ytplayer && uw.ytplayer.config)
  629. && (uw.yt && uw.yt.player && uw.yt.player.Application && uw.yt.player.Application.create)
  630. ) {
  631. ytwp.html5.app = ytwp.html5.getPlayerInstance();
  632. }
  633.  
  634. ytwp.html5.update();
  635. ytwp.html5.autohideControls();
  636. } catch (e) {
  637. ytwp.error(e);
  638. }
  639. },
  640.  
  641. };
  642.  
  643.  
  644. ytwp.pubsubListeners = {
  645. 'init': function() { // Not always called
  646. ytwp.event.init();
  647. },
  648. 'init-watch': function() { // Not always called
  649. ytwp.event.init();
  650. },
  651. 'player-added': function() { // Not always called
  652. // Usually called after init-watch, however this is called before init when going from channel -> watch page.
  653. // The init event is when the body element resets all it's classes.
  654. ytwp.event.init();
  655. },
  656. // 'player-resize': function() {},
  657. // 'player-playback-start': function() {},
  658. 'appbar-guide-delay-load': function() {
  659. // Listen to a later event that is always called in case the others are missed.
  660. ytwp.event.init();
  661.  
  662. // Channel -> /watch
  663. if (ytwp.util.isWatchUrl())
  664. ytwp.event.addBodyClass();
  665. },
  666. // 'dispose-watch': function() {},
  667. 'dispose': function() {
  668. ytwp.event.onDispose();
  669. }
  670. };
  671.  
  672. ytwp.registerYoutubeListeners = function() {
  673. ytwp.registerYoutubePubSubListeners();
  674. };
  675.  
  676. ytwp.registerYoutubePubSubListeners = function() {
  677. // Subscribe
  678. for (var eventName in ytwp.pubsubListeners) {
  679. var eventListener = ytwp.pubsubListeners[eventName];
  680. uw.yt.pubsub.instance_.subscribe(eventName, eventListener);
  681. }
  682. };
  683.  
  684. ytwp.main = function() {
  685. try {
  686. ytwp.registerYoutubeListeners();
  687. } catch(e) {
  688. ytwp.error("Could not hook yt.pubsub", e);
  689. setTimeout(ytwp.main, 1000);
  690. }
  691. ytwp.event.init();
  692. };
  693.  
  694. ytwp.main();
  695. })(typeof unsafeWindow !== 'undefined' ? unsafeWindow : window);