Resize YT To Window Size

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

目前为 2016-03-27 提交的版本。查看 最新版本

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