Resize YT To Window Size

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

当前为 2014-10-31 提交的版本,查看 最新版本

  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.43
  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://gf.qytechs.cn/scripts/811-resize-yt-to-window-size
  17. // OpenUserJS.org: https://openuserjs.org/scripts/zren/httpxshade.ca/Resize_YT_To_Window_Size
  18. // Userscripts.org: http://userscripts.org:8080/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. //---
  158. // Credits to https://github.com/YePpHa/YouTubeCenter/
  159. // Needed to fix the HTML5 Player progress bar.
  160. // https://github.com/YePpHa/YouTubeCenter/compare/782812243a99e33e07fe443ef71d127d0917ed81...a81856b974603a1645e567ee6ac91e6856c529c8
  161. var ytcenter_player_experiments = (function(){
  162. function add(exp, config) {
  163. var cfg = getConfig(config);
  164. if (!has(exp, config)) {
  165. cfg.args.fexp += "," + exp;
  166. }
  167. }
  168. function remove(exp, config) {
  169. var cfg = getConfig(config);
  170. if (cfg && cfg.args && cfg.args.fexp) {
  171. var e = cfg.args.fexp.split(","), i, a = [];
  172. for (i = 0; i < e.length; i++) {
  173. if (exp !== e[i]) {
  174. a.push(e[i]);
  175. }
  176. }
  177. cfg.args.fexp = a.join(",");
  178. }
  179. }
  180. function has(exp, config) {
  181. var cfg = getConfig(config);
  182. if (cfg && cfg.args && typeof cfg.fexp === "string") {
  183. var e = cfg.args.fexp.split(","), i, a = [];
  184. for (i = 0; i < e.length; i++) {
  185. if (exp === e[i]) {
  186. return true;
  187. }
  188. }
  189. }
  190. return false;
  191. }
  192. function clear(config) {
  193. var cfg = getConfig(config);
  194. if (cfg && cfg.args) {
  195. cfg.args.fexp = "";
  196. }
  197. }
  198. function getConfig(config) {
  199. return config || ytcenter.player.config.args;
  200. }
  201. return { add: add, remove: remove, has: has, clear: clear };
  202. })();
  203.  
  204. //--- Constants
  205. var scriptShortName = 'ytwp'; // YT Window Player
  206. var scriptStyleId = scriptShortName + '-style'; // ytwp-style
  207. var scriptBodyClassId = scriptShortName + '-window-player'; // .ytwp-window-player
  208. var viewingVideoClassId = scriptShortName + '-viewing-video'; // .ytwp-viewing-video
  209. var topOfPageClassId = scriptShortName + '-scrolltop'; // .ytwp-scrolltop
  210. var scriptBodyClassSelector = 'body.' + scriptBodyClassId; // body.ytwp-window-player
  211.  
  212. var videoContainerId = 'player';
  213. var videoContainerPlacemarkerId = scriptShortName + '-placemarker'; // ytwp-placemarker
  214.  
  215. var transitionProperties = ["transition", "-ms-transition", "-moz-transition", "-webkit-transition", "-o-transition"];
  216.  
  217. //--- YTWP
  218. var ytwp = uw.ytwp = {
  219. scriptShortName: scriptShortName, // YT Window Player
  220. log_: function(logger, args) { logger.apply(console, ['[' + this.scriptShortName + '] '].concat(Array.prototype.slice.call(args))); return 1; },
  221. log: function() { return this.log_(console.log, arguments); },
  222. error: function() { return this.log_(console.error, arguments); },
  223.  
  224. initialized: false,
  225. pageReady: false,
  226. watchPage: false,
  227. };
  228.  
  229. ytwp.util = {
  230. isWatchUrl: function (url) {
  231. if (!url)
  232. url = uw.location.href;
  233. return url.match(/https?:\/\/(www\.)?youtube.com\/watch\?/);
  234. }
  235. };
  236.  
  237. ytwp.event = {
  238. init: function() {
  239. ytwp.log('init');
  240. if (ytwp.initialized) return;
  241.  
  242. ytwp.isWatchPage = ytwp.util.isWatchUrl();
  243. if (!ytwp.isWatchPage) return;
  244.  
  245. ytwp.event.initStyle();
  246. ytwp.event.initScroller();
  247. ytwp.initialized = true;
  248. ytwp.pageReady = false;
  249. },
  250. initScroller: function() {
  251. // Register listener & Call it now.
  252. unsafeWindow.addEventListener('scroll', ytwp.event.onScroll, false);
  253. unsafeWindow.addEventListener('resize', ytwp.event.onScroll, false);
  254. ytwp.event.onScroll();
  255. },
  256. onScroll: function() {
  257. var viewportHeight = document.documentElement.clientHeight;
  258.  
  259. // topOfPageClassId
  260. if (unsafeWindow.scrollY == 0) {
  261. jQuery.addClass(document.body, topOfPageClassId);
  262. } else {
  263. jQuery.removeClass(document.body, topOfPageClassId);
  264. }
  265.  
  266. // viewingVideoClassId
  267. if (unsafeWindow.scrollY <= viewportHeight) {
  268. jQuery.addClass(document.body, viewingVideoClassId);
  269. } else {
  270. jQuery.removeClass(document.body, viewingVideoClassId);
  271. }
  272. },
  273. initStyle: function() {
  274. ytwp.log('initStyle');
  275. ytwp.style = new JSStyleSheet(scriptStyleId);
  276. ytwp.event.buildStylesheet();
  277. ytwp.style.injectIntoHeader();
  278. },
  279. buildStylesheet: function() {
  280. ytwp.log('buildStylesheet');
  281. //--- Video Player
  282.  
  283. //
  284. var d;
  285. d = buildVenderPropertyDict(transitionProperties, 'left 0s linear, padding-left 0s linear');
  286. d['padding'] = '0 !important';
  287. d['margin'] = '0 !important';
  288. ytwp.style.appendRule([
  289. scriptBodyClassSelector + ' #player',
  290. scriptBodyClassSelector + '.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible #player',
  291. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player',
  292. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player-legacy',
  293. scriptBodyClassSelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #watch7-main-container',
  294. ], d);
  295. //
  296. d = buildVenderPropertyDict(transitionProperties, 'width 0s linear, left 0s linear');
  297.  
  298. // Bugfix for Firefox
  299. // Parts of the header (search box) are hidden under the player.
  300. // Firefox doesn't seem to be using the fixed header+guide yet.
  301. d['float'] = 'initial';
  302.  
  303. ytwp.style.appendRule(scriptBodyClassSelector + ' #player-api', d);
  304.  
  305. // !important is mainly for simplicity, but is needed to override the !important styling when the Guide is open due to:
  306. // .sidebar-collapsed #watch7-video, .sidebar-collapsed #watch7-main, .sidebar-collapsed .watch7-playlist { width: 945px!important; }
  307. // Also, Youtube Center resizes #player at element level.
  308. ytwp.style.appendRule(
  309. [
  310. scriptBodyClassSelector + ' #player',
  311. scriptBodyClassSelector + ' #movie_player',
  312. scriptBodyClassSelector + ' #player-mole-container',
  313. scriptBodyClassSelector + ' .html5-main-video',
  314. ],
  315. {
  316. 'width': '100% !important',
  317. 'min-width': '100% !important',
  318. 'max-width': '100% !important',
  319. 'height': '100% !important',
  320. 'min-height': '100% !important',
  321. 'max-height': '100% !important',
  322. }
  323. );
  324.  
  325. ytwp.style.appendRule(
  326. [
  327. scriptBodyClassSelector + ' #player',
  328. scriptBodyClassSelector + ' .html5-main-video',
  329. ],
  330. {
  331. 'top': '0 !important',
  332. 'right': '0 !important',
  333. 'bottom': '0 !important',
  334. 'left': '0 !important',
  335. }
  336. );
  337. // Resize #player-unavailable, #player-api
  338. // Using min/max width/height will keep
  339. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-width', 'width', '100% !important');
  340. ytwp.style.appendRule(scriptBodyClassSelector + ' #player .player-height', 'height', '100% !important');
  341.  
  342. //--- Move Video Player
  343. ytwp.style.appendRule(scriptBodyClassSelector + ' #player', {
  344. 'position': 'absolute',
  345. // Already top:0; left: 0;
  346. });
  347. ytwp.style.appendRule(scriptBodyClassSelector, { // body
  348. 'margin-top': '100vh',
  349. });
  350.  
  351.  
  352. //--- Sidebar
  353. // Remove the transition delay as you can see it moving on page load.
  354. d = buildVenderPropertyDict(transitionProperties, 'margin-top 0s linear, padding-top 0s linear');
  355. d['margin-top'] = '0 !important';
  356. d['top'] = '0 !important';
  357. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch7-sidebar', d);
  358.  
  359. ytwp.style.appendRule(scriptBodyClassSelector + '.cardified-page #watch7-sidebar-contents', 'padding-top', '0');
  360.  
  361. //--- Absolutely position the fixed header.
  362. // Masthead
  363. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  364. 'position': 'absolute',
  365. 'top': '100% !important'
  366. });
  367.  
  368. // Guide
  369. // When watching the video, we need to line it up with the masthead.
  370. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #appbar-guide-menu', {
  371. 'display': 'initial',
  372. 'position': 'absolute',
  373. 'top': '100% !important' // Masthead height
  374. });
  375. ytwp.style.appendRule(scriptBodyClassSelector + '.' + viewingVideoClassId + ' #page.watch #guide', {
  376. 'display': 'initial',
  377. 'margin': '0',
  378. 'position': 'initial'
  379. });
  380.  
  381. //---
  382. // Hide Scrollbars
  383. ytwp.style.appendRule(scriptBodyClassSelector + '.' + topOfPageClassId, 'overflow-x', 'hidden');
  384.  
  385.  
  386. //--- Fix Other Possible Style Issues
  387.  
  388. //--- Whitespace Leftover From Moving The Video
  389. ytwp.style.appendRule(scriptBodyClassSelector + ' #page.watch', 'padding-top', '0');
  390. ytwp.style.appendRule(scriptBodyClassSelector + ' .player-branded-banner', 'height', '0');
  391.  
  392. //--- Playlist Bar
  393. //ytwp.style.appendRule(scriptBodyClassSelector + ' #watch7-playlist-tray-container', "margin", "-15px -10px 20px -10px");
  394. ytwp.style.appendRule(scriptBodyClassSelector + ' .watch7-playlist-bar-left', 'width', '640px !important'); // Same width as .watch-content
  395. ytwp.style.appendRule([
  396. scriptBodyClassSelector + ' .playlist',
  397. scriptBodyClassSelector + ' .playlist .watch7-playlist-bar',
  398. ], 'max-width', '1040px'); // Same width as .watch-content (640px) + .watch-sidebar (300-400px).
  399. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch7-playlist-tray-container', {
  400. "margin-top": "-15px",
  401. "height": "287px !important", // 65 (playlist tile) * 4 + 27 (trim on bottom)
  402. "margin-bottom": "15px"
  403. });
  404. ytwp.style.appendRule([
  405. scriptBodyClassSelector + '.cardified-page #watch7-playlist-tray-container + #watch7-sidebar-contents', // Pre Oct 26
  406. scriptBodyClassSelector + '.cardified-page #watch-appbar-playlist + #watch7-sidebar-contents', // Post Oct 26
  407. ], 'padding-top', '15px');
  408.  
  409. // YT Center
  410. ytwp.style.appendRule(scriptBodyClassSelector + ' #player', 'margin-bottom', '0 !important');
  411. ytwp.style.appendRule(scriptBodyClassSelector + ' #watch7-playlist-tray-container', {
  412. 'left': 'initial !important',
  413. 'width': 'initial !important'
  414. });
  415. ytwp.style.appendRule(scriptBodyClassSelector + ' .watch7-playlist-bar-right', 'width', '363px !important');
  416. },
  417. onWatchInit: function() {
  418. ytwp.log('onWatchInit');
  419. if (!ytwp.initialized) return;
  420. if (ytwp.pageReady) return;
  421.  
  422. ytwp.event.addBodyClass();
  423. ytwp.pageReady = true;
  424. },
  425. onDispose: function() {
  426. ytwp.initialized = false;
  427. ytwp.pageReady = false;
  428. ytwp.isWatchPage = false;
  429. },
  430. addBodyClass: function() {
  431. // Insert CSS Into the body so people can style around the effects of this script.
  432. jQuery.addClass(document.body, scriptBodyClassId);
  433. ytwp.log('Applied ' + scriptBodyClassSelector);
  434. },
  435. html5PlayerFix: function() {
  436. ytwp.log('html5PlayerFix');
  437. // Since we have to reload the player anyways, might as well set some useful settings.
  438. uw.ytplayer.config.args.autohide = 1; // Autohide the playback control bar.
  439. // https://github.com/YePpHa/YouTubeCenter/issues/1083
  440. if (!ytwp.ytapp || ytwp.ytapp.g.ba === "detailpage") {
  441. ytwp.log('rerunning ytplayer.load()');
  442. // Next 2 lines are equivalent to: ytplayer.load();
  443. ytwp.ytapp = yt.player.Application.create("player-api", ytplayer.config);
  444. ytplayer.config.loaded = true;
  445.  
  446. ytwp.ytapp.g.ba = "asdf";
  447. }
  448. }
  449. };
  450.  
  451.  
  452. ytwp.pubsubListeners = {
  453. 'init': function() { // Not always called
  454. ytwp.event.init();
  455. ytwp.event.onWatchInit();
  456. ytwp.event.html5PlayerFix();
  457. },
  458. 'init-watch': function() { // Not always called
  459. ytwp.event.init();
  460. ytwp.event.onWatchInit();
  461. ytwp.event.html5PlayerFix();
  462. },
  463. 'player-added': function() { // Not always called
  464. // Usually called after init-watch, however this is called before init when going from channel -> watch page.
  465. // The init event is when the body element resets all it's classes.
  466. ytwp.event.init();
  467. ytwp.event.onWatchInit();
  468. ytwp.event.html5PlayerFix();
  469. },
  470. // 'player-resize': function() {},
  471. // 'player-playback-start': function() {},
  472. 'appbar-guide-delay-load': function() {
  473. // Listen to a later event that is always called in case the others are missed.
  474. ytwp.event.init();
  475. ytwp.event.onWatchInit();
  476.  
  477. // Channel -> /watch
  478. if (ytwp.util.isWatchUrl())
  479. ytwp.event.addBodyClass();
  480. },
  481. // 'dispose-watch': function() {},
  482. 'dispose': function() {
  483. ytwp.event.onDispose();
  484. }
  485. };
  486.  
  487. ytwp.registerYoutubeListeners = function() {
  488. ytwp.registerYoutubePubSubListeners();
  489. };
  490.  
  491. ytwp.registerYoutubePubSubListeners = function() {
  492. // Subscribe
  493. for (var eventName in ytwp.pubsubListeners) {
  494. var eventListener = ytwp.pubsubListeners[eventName];
  495. uw.yt.pubsub.instance_.subscribe(eventName, eventListener);
  496. }
  497. };
  498.  
  499. ytwp.main = function() {
  500. try {
  501. ytwp.registerYoutubeListeners();
  502. } catch(e) {
  503. ytwp.error("Could not hook yt.pubsub", e);
  504. }
  505. ytwp.event.html5PlayerFix();
  506. ytwp.event.init();
  507. ytwp.event.onWatchInit();
  508. };
  509.  
  510. ytwp.main();
  511. })(typeof unsafeWindow !== 'undefined' ? unsafeWindow : window);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址