Resize YT To Window Size

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

当前为 2015-01-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Resize YT To Window Size
  3. // @description Moves the video to the top of the website and resizes it to the screen 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 1.50
  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. // appendRule('#blarg', 'display', 'none');
  127. var d = {};
  128. d[k] = v;
  129. newStyle = this.buildRule(selector, d);
  130. } else if (!isUndefined(k) && isUndefined(v) && isObjectType(k)) {
  131. // appendRule('#blarg', {'display': 'none'});
  132. newStyle = this.buildRule(selector, k);
  133. } else {
  134. // Invalid Arguments
  135. console.log('Illegal arguments', arguments);
  136. return;
  137. }
  138.  
  139. this.stylesheet += newStyle;
  140. };
  141.  
  142. JSStyleSheet.injectIntoHeader = function(injectedStyleId, stylesheet) {
  143. var styleElement = document.getElementById(injectedStyleId);
  144. if (!styleElement) {
  145. styleElement = document.createElement('style');
  146. styleElement.type = 'text/css';
  147. styleElement.id = injectedStyleId;
  148. document.getElementsByTagName('head')[0].appendChild(styleElement);
  149. }
  150. styleElement.appendChild(document.createTextNode(stylesheet));
  151. };
  152.  
  153. JSStyleSheet.prototype.injectIntoHeader = function(injectedStyleId, stylesheet) {
  154. JSStyleSheet.injectIntoHeader(this.id, this.stylesheet);
  155. };
  156.  
  157. //--- Constants
  158. var scriptShortName = 'ytwp'; // YT Window Player
  159. var scriptStyleId = scriptShortName + '-style'; // ytwp-style
  160. var scriptBodyClassId = scriptShortName + '-window-player'; // .ytwp-window-player
  161. var viewingVideoClassId = scriptShortName + '-viewing-video'; // .ytwp-viewing-video
  162. var topOfPageClassId = scriptShortName + '-scrolltop'; // .ytwp-scrolltop
  163. var scriptBodyClassSelector = 'body.' + scriptBodyClassId; // body.ytwp-window-player
  164.  
  165. var videoContainerId = 'player';
  166. var videoContainerPlacemarkerId = scriptShortName + '-placemarker'; // ytwp-placemarker
  167.  
  168. var transitionProperties = ["transition", "-ms-transition", "-moz-transition", "-webkit-transition", "-o-transition"];
  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. var Html5PlayerFix = {
  191. YTRect: null,
  192. moviePlayer: null,
  193. moviePlayerElement: null,
  194. app: null,
  195. };
  196. Html5PlayerFix.getPlayerRect = function() {
  197. return new Html5PlayerFix.YTRect(Html5PlayerFix.moviePlayerElement.clientWidth, Html5PlayerFix.moviePlayerElement.clientHeight);
  198. };
  199. Html5PlayerFix.isFixed = function(app) {
  200. return app.o.ub === Html5PlayerFix.getPlayerRect;
  201. };
  202. Html5PlayerFix.shouldFix = function() {
  203. return ytplayer.config.html5 && (Html5PlayerFix.app === null || !Html5PlayerFix.isFixed(Html5PlayerFix.app));
  204. }
  205. Html5PlayerFix.update = function(app) {
  206. try {
  207. if (Html5PlayerFix.app === null || Html5PlayerFix.app !== app) {
  208. Html5PlayerFix.app = app;
  209. Html5PlayerFix.moviePlayer = Html5PlayerFix.app.o;
  210. Html5PlayerFix.moviePlayerElement = Html5PlayerFix.moviePlayer.element;
  211. Html5PlayerFix.YTRect = Html5PlayerFix.moviePlayer.ub().constructor;
  212. }
  213. if (Html5PlayerFix.app && !Html5PlayerFix.isFixed(Html5PlayerFix.app)) {
  214. Html5PlayerFix.moviePlayer.ub = Html5PlayerFix.getPlayerRect;
  215. Html5PlayerFix.moviePlayer.hb = Html5PlayerFix.getPlayerRect;
  216. Html5PlayerFix.moviePlayer.pj();
  217. }
  218. } catch (e) {
  219. Html5PlayerFix.app = null;
  220. console.log('[ytwp] ', 'HTML5 Player has changed', e);
  221. }
  222. };
  223. ytwp.Html5PlayerFix = Html5PlayerFix;
  224.  
  225. ytwp.event = {
  226. init: function() {
  227. ytwp.log('init');
  228. if (ytwp.initialized) return;
  229.  
  230. ytwp.isWatchPage = ytwp.util.isWatchUrl();
  231. if (!ytwp.isWatchPage) return;
  232.  
  233. ytwp.event.initStyle();
  234. ytwp.event.initScroller();
  235. ytwp.initialized = true;
  236. ytwp.pageReady = false;
  237. },
  238. initScroller: function() {
  239. // Register listener & Call it now.
  240. unsafeWindow.addEventListener('scroll', ytwp.event.onScroll, false);
  241. unsafeWindow.addEventListener('resize', ytwp.event.onScroll, false);
  242. ytwp.event.onScroll();
  243. },
  244. onScroll: function() {
  245. var viewportHeight = document.documentElement.clientHeight;
  246.  
  247. // topOfPageClassId
  248. if (unsafeWindow.scrollY == 0) {
  249. jQuery.addClass(document.body, topOfPageClassId);
  250. } else {
  251. jQuery.removeClass(document.body, topOfPageClassId);
  252. }
  253.  
  254. // viewingVideoClassId
  255. if (unsafeWindow.scrollY <= viewportHeight) {
  256. jQuery.addClass(document.body, viewingVideoClassId);
  257. } else {
  258. jQuery.removeClass(document.body, viewingVideoClassId);
  259. }
  260. },
  261. initStyle: function() {
  262. ytwp.log('initStyle');
  263. ytwp.style = new JSStyleSheet(scriptStyleId);
  264. ytwp.event.buildStylesheet();
  265. ytwp.style.injectIntoHeader();
  266. },
  267. buildStylesheet: function() {
  268. ytwp.log('buildStylesheet');
  269. //--- Video Player
  270.  
  271. //
  272. var d;
  273. d = buildVenderPropertyDict(transitionProperties, 'left 0s linear, padding-left 0s linear');
  274. d['padding'] = '0 !important';
  275. d['margin'] = '0 !important';
  276. ytwp.style.appendRule([
  277. scriptBodyClassSelector + ' #player',
  278. scriptBodyClassSelector + '.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible #player',
  279. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player',
  280. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player-legacy',
  281. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #watch7-main-container',
  282. ], d);
  283. //
  284. d = buildVenderPropertyDict(transitionProperties, 'width 0s linear, left 0s linear');
  285.  
  286. // Bugfix for Firefox
  287. // Parts of the header (search box) are hidden under the player.
  288. // Firefox doesn't seem to be using the fixed header+guide yet.
  289. d['float'] = 'initial';
  290.  
  291. ytwp.style.appendRule(scriptBodyClassSelector + ' #player-api', d);
  292.  
  293. // !important is mainly for simplicity, but is needed to override the !important styling when the Guide is open due to:
  294. // .sidebar-collapsed #watch7-video, .sidebar-collapsed #watch7-main, .sidebar-collapsed .watch7-playlist { width: 945px!important; }
  295. // Also, Youtube Center resizes #player at element level.
  296. ytwp.style.appendRule(
  297. [
  298. scriptBodyClassSelector + ' #player',
  299. scriptBodyClassSelector + ' #movie_player',
  300. scriptBodyClassSelector + ' #player-mole-container',
  301. scriptBodyClassSelector + ' .html5-main-video',
  302. ],
  303. {
  304. 'width': '100% !important',
  305. 'min-width': '100% !important',
  306. 'max-width': '100% !important',
  307. 'height': '100% !important',
  308. 'min-height': '100% !important',
  309. 'max-height': '100% !important',
  310. }
  311. );
  312.  
  313. ytwp.style.appendRule(
  314. [
  315. scriptBodyClassSelector + ' #player',
  316. scriptBodyClassSelector + ' .html5-main-video',
  317. ],
  318. {
  319. 'top': '0 !important',
  320. 'right': '0 !important',
  321. 'bottom': '0 !important',
  322. 'left': '0 !important',
  323. }
  324. );
  325. // Resize #player-unavailable, #player-api
  326. // Using min/max width/height will keep
  327. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-width', 'width', '100% !important');
  328. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height', 'height', '100% !important');
  329.  
  330. //--- Move Video Player
  331. ytwp.style.appendRule(scriptBodyClassSelector + ' #player', {
  332. 'position': 'absolute',
  333. // Already top:0; left: 0;
  334. });
  335. ytwp.style.appendRule(scriptBodyClassSelector, { // body
  336. 'margin-top': '100vh',
  337. });
  338.  
  339.  
  340. //--- Sidebar
  341. // Remove the transition delay as you can see it moving on page load.
  342. d = buildVenderPropertyDict(transitionProperties, 'margin-top 0s linear, padding-top 0s linear');
  343. d['margin-top'] = '0 !important';
  344. d['top'] = '0 !important';
  345. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch7-sidebar', d);
  346.  
  347. ytwp.style.appendRule(scriptBodyClassSelector + '.cardified-page #watch7-sidebar-contents', 'padding-top', '0');
  348.  
  349. //--- Absolutely position the fixed header.
  350. // Masthead
  351. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  352. 'position': 'absolute',
  353. 'top': '100% !important'
  354. });
  355.  
  356. // Guide
  357. // When watching the video, we need to line it up with the masthead.
  358. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #appbar-guide-menu', {
  359. 'display': 'initial',
  360. 'position': 'absolute',
  361. 'top': '100% !important' // Masthead height
  362. });
  363. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #page.watch #guide', {
  364. 'display': 'initial',
  365. 'margin': '0',
  366. 'position': 'initial'
  367. });
  368.  
  369. //---
  370. // Hide Scrollbars
  371. ytwp.style.appendRule(scriptBodyClassSelector + '.' + topOfPageClassId, 'overflow-x', 'hidden');
  372.  
  373.  
  374. //--- Fix Other Possible Style Issues
  375.  
  376. //--- Whitespace Leftover From Moving The Video
  377. ytwp.style.appendRule(scriptBodyClassSelector + ' #page.watch', 'padding-top', '0');
  378. ytwp.style.appendRule(scriptBodyClassSelector + ' .player-branded-banner', 'height', '0');
  379.  
  380. //--- Playlist Bar
  381. //ytwp.style.appendRule(scriptBodyClassSelector + ' #watch7-playlist-tray-container', "margin", "-15px -10px 20px -10px");
  382. ytwp.style.appendRule(scriptBodyClassSelector + ' .watch7-playlist-bar-left', 'width', '640px !important'); // Same width as .watch-content
  383. ytwp.style.appendRule([
  384. scriptBodyClassSelector + ' .playlist',
  385. scriptBodyClassSelector + ' .playlist .watch7-playlist-bar',
  386. ], 'max-width', '1040px'); // Same width as .watch-content (640px) + .watch-sidebar (300-400px).
  387. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch7-playlist-tray-container', {
  388. "margin-top": "-15px",
  389. "height": "287px !important", // 65 (playlist tile) * 4 + 27 (trim on bottom)
  390. "margin-bottom": "15px"
  391. });
  392. ytwp.style.appendRule([
  393. scriptBodyClassSelector + '.cardified-page #watch7-playlist-tray-container + #watch7-sidebar-contents', // Pre Oct 26
  394. scriptBodyClassSelector + '.cardified-page #watch-appbar-playlist + #watch7-sidebar-contents', // Post Oct 26
  395. ], 'padding-top', '15px');
  396.  
  397. // YT Center
  398. ytwp.style.appendRule(scriptBodyClassSelector + ' #player', 'margin-bottom', '0 !important');
  399. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch7-playlist-tray-container', {
  400. 'left': 'initial !important',
  401. 'width': 'initial !important'
  402. });
  403. ytwp.style.appendRule(scriptBodyClassSelector + ' .watch7-playlist-bar-right', 'width', '363px !important');
  404. },
  405. onWatchInit: function() {
  406. ytwp.log('onWatchInit');
  407. if (!ytwp.initialized) return;
  408. if (ytwp.pageReady) return;
  409.  
  410. ytwp.event.addBodyClass();
  411. ytwp.pageReady = true;
  412. },
  413. onDispose: function() {
  414. window.removeEventListener('click', ytwp.event.onWindowClick);
  415. ytwp.initialized = false;
  416. ytwp.pageReady = false;
  417. ytwp.isWatchPage = false;
  418. },
  419. addBodyClass: function() {
  420. // Insert CSS Into the body so people can style around the effects of this script.
  421. jQuery.addClass(document.body, scriptBodyClassId);
  422. ytwp.log('Applied ' + scriptBodyClassSelector);
  423. },
  424. html5PlayerFix: function() {
  425. ytwp.log('html5PlayerFix');
  426.  
  427. // https://github.com/YePpHa/YouTubeCenter/issues/1083
  428. if (!uw.ytcenter
  429. && (!ytwp.ytapp || ytwp.Html5PlayerFix.shouldFix())
  430. && (uw.ytplayer && uw.ytplayer.config)
  431. && (uw.yt && uw.yt.player && uw.yt.player.Application && uw.yt.player.Application.create)
  432. ) {
  433. ytwp.log('rerunning ytplayer.load()');
  434. // Since we have to reload the player anyways, might as well set some useful settings.
  435. uw.ytplayer.config.args.autohide = 1; // Autohide the playback control bar.
  436. // Next 2 lines are equivalent to: ytplayer.load();
  437. ytwp.ytapp = uw.yt.player.Application.create("player-api", uw.ytplayer.config);
  438. uw.ytplayer.config.loaded = true;
  439.  
  440. ytwp.Html5PlayerFix.update(ytwp.ytapp);
  441. }
  442. },
  443. onWindowClick: function(event) {
  444. function isClickingLink(el) {
  445. while (el) {
  446. if (el.tagName === 'A' && el.href) { // is anchor tag
  447. return true;
  448. }
  449. el = el.parentNode;
  450. }
  451. return false;
  452. }
  453. var el = event.target;
  454. if (isClickingLink(event.target)) {
  455. if (ytwp.ytapp && ytwp.ytapp.j && ytwp.ytapp.j.ca === 'GIBBERISH') {
  456. ytwp.ytapp.j.ca = 'detailpage';
  457. ytwp.log('ytwp.ytapp.j.ca: "GIBBERISH" => "detailpage"');
  458. }
  459. }
  460. },
  461. };
  462.  
  463.  
  464. ytwp.pubsubListeners = {
  465. 'init': function() { // Not always called
  466. ytwp.event.init();
  467. ytwp.event.onWatchInit();
  468. ytwp.event.html5PlayerFix();
  469. },
  470. 'init-watch': function() { // Not always called
  471. ytwp.event.init();
  472. ytwp.event.onWatchInit();
  473. ytwp.event.html5PlayerFix();
  474. },
  475. 'player-added': function() { // Not always called
  476. // Usually called after init-watch, however this is called before init when going from channel -> watch page.
  477. // The init event is when the body element resets all it's classes.
  478. ytwp.event.init();
  479. ytwp.event.onWatchInit();
  480. ytwp.event.html5PlayerFix();
  481. },
  482. // 'player-resize': function() {},
  483. // 'player-playback-start': function() {},
  484. 'appbar-guide-delay-load': function() {
  485. // Listen to a later event that is always called in case the others are missed.
  486. ytwp.event.init();
  487. ytwp.event.onWatchInit();
  488.  
  489. // Channel -> /watch
  490. if (ytwp.util.isWatchUrl())
  491. ytwp.event.addBodyClass();
  492. },
  493. // 'dispose-watch': function() {},
  494. 'dispose': function() {
  495. ytwp.event.onDispose();
  496. }
  497. };
  498.  
  499. ytwp.registerYoutubeListeners = function() {
  500. ytwp.registerYoutubePubSubListeners();
  501. };
  502.  
  503. ytwp.registerYoutubePubSubListeners = function() {
  504. // Subscribe
  505. for (var eventName in ytwp.pubsubListeners) {
  506. var eventListener = ytwp.pubsubListeners[eventName];
  507. uw.yt.pubsub.instance_.subscribe(eventName, eventListener);
  508. }
  509. };
  510.  
  511. ytwp.main = function() {
  512. try {
  513. ytwp.registerYoutubeListeners();
  514. } catch(e) {
  515. ytwp.error("Could not hook yt.pubsub", e);
  516. }
  517. ytwp.event.html5PlayerFix();
  518. ytwp.event.init();
  519. ytwp.event.onWatchInit();
  520. };
  521.  
  522. ytwp.main();
  523. })(typeof unsafeWindow !== 'undefined' ? unsafeWindow : window);