Resize YT To Window Size

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

当前为 2015-05-06 提交的版本,查看 最新版本

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