Resize YT To Window Size

Moves the YouTube video to the top of the website and fill the window with the video player.

当前为 2021-04-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Resize YT To Window Size
  3. // @description Moves the YouTube video to the top of the website and fill the window with the video player.
  4. // @author Chris H (Zren / Shade)
  5. // @license MIT
  6. // @icon https://s.ytimg.com/yts/img/favicon_32-vflOogEID.png
  7. // @homepageURL https://github.com/Zren/ResizeYoutubePlayerToWindowSize/
  8. // @namespace http://xshade.ca
  9. // @version 127
  10. // @include http*://*.youtube.com/*
  11. // @include http*://youtube.com/*
  12. // @include http*://*.youtu.be/*
  13. // @include http*://youtu.be/*
  14. // @grant none
  15. // ==/UserScript==
  16.  
  17. // Github: https://github.com/Zren/ResizeYoutubePlayerToWindowSize
  18. // GreasyFork: https://gf.qytechs.cn/scripts/811-resize-yt-to-window-size
  19. // OpenUserJS.org: https://openuserjs.org/scripts/zren/Resize_YT_To_Window_Size
  20. // Userscripts.org: http://userscripts-mirror.org/scripts/show/153699
  21.  
  22. (function (window) {
  23. "use strict";
  24.  
  25. //--- Settings
  26. var playerHeight = '100vh';
  27.  
  28. //--- Imported Globals
  29. // yt
  30. // ytcenter
  31. // html5Patched (Youtube+)
  32. // ytplayer
  33. var uw = window;
  34.  
  35. //--- Already Loaded?
  36. // GreaseMonkey loads this script twice for some reason.
  37. if (uw.ytwp) return;
  38.  
  39. //--- Is iframe?
  40. function inIframe () {
  41. try {
  42. return window.self !== window.top;
  43. } catch (e) {
  44. return true;
  45. }
  46. }
  47. if (inIframe()) return;
  48.  
  49. //--- Utils
  50. function isStringType(obj) { return typeof obj === 'string'; }
  51. function isArrayType(obj) { return obj instanceof Array; }
  52. function isObjectType(obj) { return typeof obj === 'object'; }
  53. function isUndefined(obj) { return typeof obj === 'undefined'; }
  54. function buildVenderPropertyDict(propertyNames, value) {
  55. var d = {};
  56. for (var i in propertyNames)
  57. d[propertyNames[i]] = value;
  58. return d;
  59. }
  60. function observe(selector, config, callback) {
  61. var observer = new MutationObserver(function(mutations) {
  62. mutations.forEach(function(mutation){
  63. callback(mutation);
  64. });
  65. });
  66. var target = document.querySelector(selector);
  67. if (!target) {
  68. return null;
  69. }
  70. observer.observe(target, config);
  71. return observer;
  72. }
  73.  
  74. //--- Stylesheet
  75. var JSStyleSheet = function(id) {
  76. this.id = id;
  77. this.stylesheet = '';
  78. };
  79.  
  80. JSStyleSheet.prototype.buildRule = function(selector, styles) {
  81. var s = "";
  82. for (var key in styles) {
  83. s += "\t" + key + ": " + styles[key] + ";\n";
  84. }
  85. return selector + " {\n" + s + "}\n";
  86. };
  87.  
  88. JSStyleSheet.prototype.appendRule = function(selector, k, v) {
  89. if (isArrayType(selector))
  90. selector = selector.join(',\n');
  91. var newStyle;
  92. if (!isUndefined(k) && !isUndefined(v) && isStringType(k)) { // v can be any type (as we stringify it).
  93. var d = {};
  94. d[k] = v;
  95. newStyle = this.buildRule(selector, d);
  96. } else if (!isUndefined(k) && isUndefined(v) && isObjectType(k)) {
  97. newStyle = this.buildRule(selector, k);
  98. } else {
  99. // Invalid Arguments
  100. console.log('Illegal arguments', arguments);
  101. return;
  102. }
  103.  
  104. this.stylesheet += newStyle;
  105. };
  106.  
  107. JSStyleSheet.injectIntoHeader = function(injectedStyleId, stylesheet) {
  108. var styleElement = document.getElementById(injectedStyleId);
  109. if (!styleElement) {
  110. styleElement = document.createElement('style');
  111. styleElement.type = 'text/css';
  112. styleElement.id = injectedStyleId;
  113. document.getElementsByTagName('head')[0].appendChild(styleElement);
  114. }
  115. styleElement.appendChild(document.createTextNode(stylesheet));
  116. };
  117.  
  118. JSStyleSheet.prototype.injectIntoHeader = function(injectedStyleId, stylesheet) {
  119. JSStyleSheet.injectIntoHeader(this.id, this.stylesheet);
  120. };
  121.  
  122. //--- History
  123. var HistoryEvent = function() {}
  124. HistoryEvent.listeners = []
  125.  
  126. HistoryEvent.dispatch = function(state, title, url) {
  127. var stack = this.listeners
  128. for (var i = 0, l = stack.length; i < l; i++) {
  129. stack[i].call(this, state, title, url)
  130. }
  131. }
  132. HistoryEvent.onPushState = function(state, title, url) {
  133. HistoryEvent.dispatch(state, title, url)
  134. return HistoryEvent.origPushState.apply(window.history, arguments)
  135. }
  136. HistoryEvent.onReplaceState = function(state, title, url) {
  137. HistoryEvent.dispatch(state, title, url)
  138. return HistoryEvent.origReplaceState.apply(window.history, arguments)
  139. }
  140. HistoryEvent.inject = function() {
  141. if (!HistoryEvent.injected) {
  142. HistoryEvent.origPushState = window.history.pushState
  143. HistoryEvent.origReplaceState = window.history.replaceState
  144.  
  145. window.history.pushState = HistoryEvent.onPushState
  146. window.history.replaceState = HistoryEvent.onReplaceState
  147. HistoryEvent.injected = true
  148. }
  149. }
  150.  
  151. HistoryEvent.timerId = 0
  152. HistoryEvent.onTick = function() {
  153. var currentPage = window.location.pathname + window.location.search
  154. if (HistoryEvent.lastPage != currentPage) {
  155. HistoryEvent.dispatch({}, document.title, window.location.href)
  156. HistoryEvent.lastPage = currentPage
  157. }
  158. }
  159. HistoryEvent.startTimer = function() {
  160. HistoryEvent.lastPage = window.location.pathname + window.location.search
  161. HistoryEvent.timerId = setInterval(HistoryEvent.onTick, 500)
  162. }
  163. HistoryEvent.stopTimer = function() {
  164. clearInterval(HistoryEvent.timerId)
  165. }
  166. window.ytwpHistoryEvent = HistoryEvent
  167.  
  168.  
  169. //--- Constants
  170. var scriptShortName = 'ytwp'; // YT Window Player
  171. var scriptStyleId = scriptShortName + '-style'; // ytwp-style
  172. var scriptBodyClassId = scriptShortName + '-window-player'; // .ytwp-window-player
  173. var viewingVideoClassId = scriptShortName + '-viewing-video'; // .ytwp-viewing-video
  174. var topOfPageClassId = scriptShortName + '-scrolltop'; // .ytwp-scrolltop
  175.  
  176. var scriptHtmlSelector = 'html:not([fullscreen="true"])';
  177. var scriptBodySelector = 'body.' + scriptBodyClassId; // body.ytwp-window-player
  178. scriptBodySelector += ':not(.enhancer-for-youtube-pinned-player)'; // Support "Enhancer for Youtube" (Pull Request #51)
  179. var scriptSelector = scriptHtmlSelector + ' ' + scriptBodySelector;
  180.  
  181. var videoContainerId = 'player';
  182. var videoContainerPlacemarkerId = scriptShortName + '-placemarker'; // ytwp-placemarker
  183.  
  184. var transitionProperties = ["transition", "-ms-transition", "-moz-transition", "-webkit-transition", "-o-transition"];
  185. var transformProperties = ["transform", "-ms-transform", "-moz-transform", "-webkit-transform", "-o-transform"];
  186.  
  187. //--- YTWP
  188. var ytwp = uw.ytwp = {
  189. scriptShortName: scriptShortName, // YT Window Player
  190. log_: function(logger, args) { logger.apply(console, ['[' + this.scriptShortName + '] '].concat(Array.prototype.slice.call(args))); return 1; },
  191. log: function() { return this.log_(console.log, arguments); },
  192. error: function() { return this.log_(console.error, arguments); },
  193.  
  194. initialized: false,
  195. pageReady: false,
  196. isWatchPage: false,
  197. };
  198.  
  199. ytwp.isWatchUrl = function (url) {
  200. if (!url)
  201. url = uw.location.href;
  202. if (url.match(/https?:\/\/(www\.)?youtube.com\/(c|channel|user)\/[^\/]+\/live/)) {
  203. if (document.querySelector('ytd-browse')) {
  204. return false
  205. } else {
  206. return true
  207. }
  208. }
  209. return url.match(/https?:\/\/(www\.)?youtube.com\/watch\?/);
  210. };
  211.  
  212. ytwp.setTheaterMode = function(enable) {
  213. // ytwp.log('setTheaterMode', enable)
  214.  
  215. var watchElement = document.querySelector('ytd-watch:not([hidden])') || document.querySelector('ytd-watch-flexy:not([hidden])')
  216. if (watchElement) {
  217. var isTheater = watchElement.hasAttribute('theater')
  218. if (enable != isTheater) {
  219. var sizeButton = watchElement.querySelector('button.ytp-size-button')
  220. if (sizeButton) {
  221. sizeButton.click()
  222. }
  223. }
  224. watchElement.canFitTheater_ = true // When it's too small, it disables the theater mode.
  225. } else if (watchElement = document.querySelector('#page.watch')) {
  226. var isTheater = watchElement.classList.contains('watch-stage-mode')
  227. if (enable != isTheater) {
  228. var sizeButton = watchElement.querySelector('button.ytp-size-button')
  229. if (sizeButton) {
  230. sizeButton.click()
  231. }
  232. }
  233. }
  234. }
  235. ytwp.enterTheaterMode = function() {
  236. // ytwp.log('enterTheaterMode')
  237. if (!document.body.classList.contains(scriptBodyClassId)) {
  238. return
  239. }
  240.  
  241. ytwp.setTheaterMode(true)
  242. }
  243. ytwp.enterTheaterMode();
  244. uw.addEventListener('resize', ytwp.enterTheaterMode);
  245.  
  246. ytwp.init = function() {
  247. ytwp.log('init');
  248. if (!ytwp.initialized) {
  249. ytwp.isWatchPage = ytwp.isWatchUrl();
  250. if (ytwp.isWatchPage) {
  251. ytwp.removeSearchAutofocus();
  252. if (!document.getElementById(scriptStyleId)) {
  253. ytwp.event.initStyle();
  254. }
  255. ytwp.initScroller();
  256. ytwp.initialized = true;
  257. ytwp.pageReady = false;
  258. }
  259. }
  260. ytwp.event.onWatchInit();
  261. if (ytwp.isWatchPage) {
  262. ytwp.html5PlayerFix();
  263. }
  264. }
  265.  
  266. ytwp.initScroller = function() {
  267. // Register listener & Call it now.
  268. uw.addEventListener('scroll', ytwp.onScroll, false);
  269. uw.addEventListener('resize', ytwp.onScroll, false);
  270. ytwp.onScroll();
  271. }
  272.  
  273. ytwp.onScroll = function() {
  274. var viewportHeight = document.documentElement.clientHeight;
  275.  
  276. // topOfPageClassId
  277. if (ytwp.isWatchPage && uw.scrollY == 0) {
  278. document.body.classList.add(topOfPageClassId);
  279. //var player = document.getElementById('movie_player');
  280. //if (player)
  281. // player.focus();
  282. } else {
  283. document.body.classList.remove(topOfPageClassId);
  284. }
  285.  
  286. // viewingVideoClassId
  287. if (ytwp.isWatchPage && uw.scrollY <= viewportHeight) {
  288. document.body.classList.add(viewingVideoClassId);
  289. } else {
  290. document.body.classList.remove(viewingVideoClassId);
  291. }
  292. }
  293.  
  294. ytwp.event = {
  295. initStyle: function() {
  296. ytwp.log('initStyle');
  297. ytwp.style = new JSStyleSheet(scriptStyleId);
  298. ytwp.event.buildStylesheet();
  299. // Duplicate stylesheet targeting data-spf-name if enabled.
  300. if (uw.spf) {
  301. var temp = scriptBodySelector;
  302. scriptBodySelector = 'body[data-spf-name="watch"]';
  303. scriptSelector = scriptHtmlSelector + ' ' + scriptBodySelector
  304. ytwp.event.buildStylesheet();
  305. ytwp.style.appendRule('body[data-spf-name="watch"]:not(.ytwp-window-player) #masthead-positioner', {
  306. 'position': 'absolute',
  307. 'top': playerHeight + ' !important'
  308. });
  309. }
  310. ytwp.style.injectIntoHeader();
  311. },
  312. buildStylesheet: function() {
  313. ytwp.log('buildStylesheet');
  314. //--- Browser Scrollbar
  315. // Chrome/Webkit
  316. ytwp.style.appendRule(scriptBodySelector + '::-webkit-scrollbar', {
  317. 'width': '0 !important',
  318. 'height': '0 !important',
  319. });
  320. // Firefox/Gecko
  321. // Requires about:config flag to be toggled as of FireFox v63
  322. // https://github.com/Zren/ResizeYoutubePlayerToWindowSize/issues/42
  323. ytwp.style.appendRule('html', {
  324. 'scrollbar-width': 'none',
  325. });
  326.  
  327. //--- Video Player
  328. var d;
  329. d = buildVenderPropertyDict(transitionProperties, 'left 0s linear, padding-left 0s linear');
  330. d['padding'] = '0 !important';
  331. d['margin'] = '0 !important';
  332. ytwp.style.appendRule([
  333. scriptBodySelector + ' #player',
  334. scriptBodySelector + '.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible #player',
  335. scriptBodySelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player',
  336. scriptBodySelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #player-legacy',
  337. scriptBodySelector + '.ltr.ytcenter-site-center.ytcenter-non-resize.ytcenter-guide-visible.guide-collapsed #watch7-main-container',
  338. ], d);
  339. //
  340. d = buildVenderPropertyDict(transitionProperties, 'width 0s linear, left 0s linear');
  341.  
  342. // Bugfix for Firefox
  343. // Parts of the header (search box) are hidden under the player.
  344. // Firefox doesn't seem to be using the fixed header+guide yet.
  345. d['float'] = 'initial';
  346.  
  347. // Skinny mode
  348. d['left'] = 0;
  349. d['margin-left'] = 0;
  350.  
  351. ytwp.style.appendRule(scriptBodySelector + ' #player-api', d);
  352.  
  353. // Theatre mode
  354. ytwp.style.appendRule(scriptBodySelector + ' .watch-stage-mode #player .player-api', {
  355. 'left': 'initial !important',
  356. 'margin-left': 'initial !important',
  357. });
  358.  
  359. // Hide the cinema/wide mode button since it's useless.
  360. //ytwp.style.appendRule(scriptBodySelector + ' #movie_player .ytp-size-button', 'display', 'none');
  361.  
  362. // !important is mainly for simplicity, but is needed to override the !important styling when the Guide is open due to:
  363. // .sidebar-collapsed #watch7-video, .sidebar-collapsed #watch7-main, .sidebar-collapsed .watch7-playlist { width: 945px!important; }
  364. // Also, Youtube Center resizes #player at element level.
  365. // Don't resize if Youtube+'s html.floater is detected.
  366. // Dont' resize if Youtube+ (Iridium/Material)'s html.iri-always-visible is detected.
  367. ytwp.style.appendRule(
  368. [
  369. scriptSelector + ' #player',
  370. scriptSelector + ' #player-wrap',
  371. scriptSelector + ' #player-api',
  372. scriptHtmlSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' #movie_player',
  373. scriptSelector + ' #player-mole-container',
  374. scriptHtmlSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' .html5-video-container',
  375. scriptHtmlSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' .html5-main-video',
  376. scriptSelector + ' ytd-watch-flexy[theater] #player-theater-container.ytd-watch-flexy',
  377. ],
  378. {
  379. 'width': '100% !important',
  380. 'min-width': '100% !important',
  381. 'max-width': '100% !important',
  382. 'height': playerHeight + ' !important',
  383. 'min-height': playerHeight + ' !important',
  384. 'max-height': playerHeight + ' !important',
  385. }
  386. );
  387.  
  388. ytwp.style.appendRule(
  389. [
  390. scriptSelector + ' #player',
  391. scriptSelector + ' .html5-main-video',
  392. ],
  393. {
  394. 'top': '0 !important',
  395. 'right': '0 !important',
  396. 'bottom': '0 !important',
  397. 'left': '0 !important',
  398. }
  399. );
  400. // Resize #player-unavailable, #player-api
  401. // Using min/max width/height will keep
  402. ytwp.style.appendRule(scriptSelector + ' #player .player-width', 'width', '100% !important');
  403. ytwp.style.appendRule(scriptSelector + ' #player .player-height', 'height', '100% !important');
  404.  
  405. // Fix video overlays
  406. ytwp.style.appendRule([
  407. scriptSelector + ' .html5-video-player .ad-container-single-media-element-annotations', // Ad
  408. scriptSelector + ' .html5-video-player .ytp-upnext', // Autoplay Next Video
  409. ], 'top', '0');
  410.  
  411.  
  412. //--- Move Video Player
  413. ytwp.style.appendRule(scriptSelector + ' #player', {
  414. 'position': 'absolute',
  415. // Already top:0; left: 0;
  416. });
  417. ytwp.style.appendRule(scriptSelector, { // body
  418. 'margin-top': playerHeight,
  419. });
  420.  
  421. // Fix the top right avatar button
  422. ytwp.style.appendRule(scriptSelector + ' button.ytp-button.ytp-cards-button', 'top', '0');
  423.  
  424.  
  425. //--- Sidebar
  426. // Remove the transition delay as you can see it moving on page load.
  427. d = buildVenderPropertyDict(transitionProperties, 'margin-top 0s linear, padding-top 0s linear');
  428. d['margin-top'] = '0 !important';
  429. d['top'] = '0 !important';
  430. ytwp.style.appendRule(scriptSelector + ' #watch7-sidebar', d);
  431.  
  432. ytwp.style.appendRule(scriptSelector + '.cardified-page #watch7-sidebar-contents', 'padding-top', '0');
  433.  
  434. //--- Absolutely position the fixed header.
  435. // Masthead
  436. d = buildVenderPropertyDict(transitionProperties, 'top 0s linear !important');
  437. ytwp.style.appendRule(scriptSelector + '.hide-header-transition #masthead-positioner', d);
  438. ytwp.style.appendRule(scriptSelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  439. 'position': 'absolute',
  440. 'top': playerHeight + ' !important'
  441. });
  442. // Lower masthead below Youtube+'s html.floater
  443. ytwp.style.appendRule('html.floater ' + scriptBodySelector + '.' + viewingVideoClassId + ' #masthead-positioner', {
  444. 'z-index': '5',
  445. });
  446. // Autocomplete popup
  447. ytwp.style.appendRule(scriptSelector + ' .sbdd_a', {
  448. 'top': '56px',
  449. });
  450. ytwp.style.appendRule(scriptSelector + '.' + viewingVideoClassId + ' .sbdd_a', {
  451. 'top': 'calc(' + playerHeight + ' + 56px) !important',
  452. 'position': 'absolute !important',
  453. });
  454.  
  455. // Guide
  456. // When watching the video, we need to line it up with the masthead.
  457. ytwp.style.appendRule(scriptSelector + '.' + viewingVideoClassId + ' #appbar-guide-menu', {
  458. 'display': 'initial',
  459. 'position': 'absolute',
  460. 'top': '100% !important' // Masthead height
  461. });
  462. ytwp.style.appendRule(scriptSelector + '.' + viewingVideoClassId + ' #page.watch #guide', {
  463. 'display': 'initial',
  464. 'margin': '0',
  465. 'position': 'initial'
  466. });
  467.  
  468.  
  469. //---
  470. // MiniPlayer-Bar
  471. ytwp.style.appendRule(scriptSelector + ' #miniplayer-bar #player', {
  472. 'position': 'static',
  473. });
  474. ytwp.style.appendRule(
  475. [
  476. scriptSelector + ' #miniplayer-bar #player',
  477. scriptSelector + ' #miniplayer-bar #player-api',
  478. scriptHtmlSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' #miniplayer-bar #movie_player',
  479. scriptSelector + ' #player-mole-container',
  480. scriptSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' #miniplayer-bar .html5-video-container',
  481. scriptSelector + ':not(.floater):not(.iri-always-visible) ' + scriptBodySelector + ' #miniplayer-bar .html5-main-video',
  482. ],
  483. {
  484. 'width': '252px !important',
  485. 'min-width': '252px !important',
  486. 'max-width': '252px !important',
  487. 'height': '142px !important',
  488. 'min-height': '142px !important',
  489. 'max-height': '142px !important',
  490. }
  491. );
  492. // Override inline style (caused by a JS animation) that breaks the miniplayer video
  493. // https://github.com/Zren/ResizeYoutubePlayerToWindowSize/issues/41#issuecomment-439710130
  494. ytwp.style.appendRule('.video-stream.html5-main-video', {
  495. 'top': '0 !important',
  496. });
  497.  
  498. //---
  499. // Hide Scrollbars
  500. ytwp.style.appendRule(scriptSelector + '.' + topOfPageClassId, 'overflow-x', 'hidden');
  501.  
  502.  
  503. //--- Fix Other Possible Style Issues
  504. ytwp.style.appendRule(scriptSelector + ' #placeholder-player', 'display', 'none');
  505. ytwp.style.appendRule(scriptSelector + ' #watch-sidebar-spacer', 'display', 'none');
  506. ytwp.style.appendRule(scriptSelector + ' .skip-nav', 'display', 'none');
  507.  
  508. //--- Whitespace Leftover From Moving The Video
  509. ytwp.style.appendRule(scriptSelector + ' #page.watch', 'padding-top', '0');
  510. ytwp.style.appendRule(scriptSelector + ' .player-branded-banner', 'height', '0');
  511.  
  512. //--- Youtube+ Compatiblity
  513. ytwp.style.appendRule(scriptSelector + ' #body-container', 'position', 'static');
  514. ytwp.style.appendRule(scriptHtmlSelector + '.part_static_size:not(.content-snap-width-skinny-mode) ' + scriptBodySelector + ' .watch-non-stage-mode #player-playlist', 'width', '1066px');
  515.  
  516. //--- Playlist Bar
  517. ytwp.style.appendRule([
  518. scriptSelector + ' #placeholder-playlist',
  519. scriptSelector + ' #player .player-height#watch-appbar-playlist',
  520. ], {
  521. 'height': '540px !important',
  522. 'max-height': '540px !important',
  523. });
  524.  
  525. d = buildVenderPropertyDict(transitionProperties, 'transform 0s linear');
  526. ytwp.style.appendRule(scriptSelector + ' #watch-appbar-playlist', d);
  527. d = buildVenderPropertyDict(transformProperties, 'translateY(0px)');
  528. d['margin-left'] = '0';
  529. d['top'] = 'calc(' + playerHeight + ' + 60px)';
  530. ytwp.style.appendRule(scriptSelector + ' #player .player-height#watch-appbar-playlist', d);
  531. ytwp.style.appendRule(scriptSelector + ' .playlist-videos-list', {
  532. 'max-height': '470px !important',
  533. 'height': 'initial !important',
  534. });
  535.  
  536. // Old layout `&disable_polymer=true`
  537. ytwp.style.appendRule(scriptSelector + ' #player .player-height#watch-appbar-playlist', {
  538. 'left': 'calc((100vw - 1066px)/2 + 640px + 10px)',
  539. 'width': '416px',
  540. });
  541. ytwp.style.stylesheet += '@media screen and (min-height: 630px) and (min-width: 1294px) {\n';
  542. ytwp.style.appendRule(scriptSelector + ' #player .player-height#watch-appbar-playlist', {
  543. 'left': 'calc((100vw - 1280px)/2 + 854px + 10px)',
  544. });
  545. ytwp.style.stylesheet += '}\n @media screen and (min-width: 1720px) and (min-height:980px) {\n';
  546. ytwp.style.appendRule(scriptSelector + ' #player .player-height#watch-appbar-playlist', {
  547. 'left': 'calc((100vw - 1706px)/2 + 1280px + 10px)',
  548. });
  549. ytwp.style.stylesheet += '}\n';
  550.  
  551. //---
  552. // Material UI
  553. ytwp.style.appendRule(scriptSelector + '.ytwp-scrolltop #extra-buttons', 'display', 'none !important');
  554. // ytwp.style.appendRule('body > #player:not(.ytd-watch)', 'display', 'none');
  555. // ytwp.style.appendRule('body.ytwp-viewing-video #content:not(app-header-layout) ytd-page-manager', 'margin-top', '0 !important');
  556. // ytwp.style.appendRule('.ytd-watch-0 #content-separator.ytd-watch', 'margin-top', '0');
  557. ytwp.style.appendRule('ytd-app', 'position', 'static !important');
  558. ytwp.style.appendRule('ytd-watch #top', 'margin-top', '71px !important'); // 56px (topnav height) + 15px (margin)
  559. ytwp.style.appendRule('ytd-watch #container', 'margin-top', '0 !important');
  560. ytwp.style.appendRule('ytd-watch #content-separator', 'margin-top', '0 !important');
  561. ytwp.style.appendRule(scriptSelector + '.ytwp-viewing-video ytd-app #masthead-container.ytd-app', {
  562. 'position': 'absolute',
  563. 'top': playerHeight,
  564. 'z-index': 0,
  565. });
  566. ytwp.style.appendRule(scriptSelector + '.ytwp-viewing-video ytd-watch #masthead-positioner', {
  567. 'top': playerHeight + ' !important',
  568. });
  569. ytwp.style.appendRule(scriptSelector + ' .ytp-cued-thumbnail-overlay', 'z-index', '10');
  570.  
  571. //---
  572. // Flexy UI
  573. ytwp.style.appendRule(scriptSelector + ' ytd-watch-flexy[theater] #player-theater-container.ytd-watch-flexy', {
  574. 'position': 'absolute',
  575. 'top': '0',
  576. });
  577. ytwp.style.appendRule(scriptSelector + ' ytd-watch-flexy', 'padding-top', '71px'); // 56px (topnav height) + 15px (margin)
  578. ytwp.style.appendRule(scriptSelector + ' #error-screen', 'z-index', '11');
  579. },
  580. onWatchInit: function() {
  581. ytwp.log('onWatchInit');
  582. if (!ytwp.initialized) return;
  583. if (ytwp.pageReady) return;
  584.  
  585. ytwp.event.addBodyClass();
  586. ytwp.pageReady = true;
  587. },
  588. onDispose: function() {
  589. ytwp.log('onDispose');
  590. ytwp.initialized = false;
  591. ytwp.pageReady = false;
  592. ytwp.isWatchPage = false;
  593. },
  594. addBodyClass: function() {
  595. // Insert CSS Into the body so people can style around the effects of this script.
  596. document.body.classList.add(scriptBodyClassId);
  597. ytwp.log('Applied ' + scriptBodySelector);
  598. },
  599. };
  600.  
  601. ytwp.html5PlayerFix = function() {
  602. ytwp.log('html5PlayerFix');
  603. return;
  604.  
  605. try {
  606. if (!uw.ytcenter // Youtube Center
  607. && !uw.html5Patched // Youtube+
  608. && (!ytwp.html5.app)
  609. && (uw.ytplayer && uw.ytplayer.config)
  610. && (uw.yt && uw.yt.player && uw.yt.player.Application && uw.yt.player.Application.create)
  611. ) {
  612. ytwp.html5.app = ytwp.html5.getPlayerInstance();
  613. }
  614.  
  615. ytwp.html5.update();
  616. ytwp.html5.autohideControls();
  617. } catch (e) {
  618. ytwp.error(e);
  619. }
  620. }
  621.  
  622. ytwp.fixMasthead = function() {
  623. ytwp.log('fixMasthead');
  624. var el = document.querySelector('#masthead-positioner-height-offset');
  625. if (el) {
  626. ytwp.fixMastheadElement(el);
  627. }
  628. }
  629. ytwp.fixMastheadElement = function(el) {
  630. ytwp.log('fixMastheadElement', el);
  631. if (el.style.height) { // != ""
  632. setTimeout(function(){
  633. el.style.height = ""
  634. document.querySelector('#appbar-guide-menu').style.marginTop = "";
  635. }, 0);
  636. }
  637. }
  638.  
  639. JSStyleSheet.injectIntoHeader(scriptStyleId + '-focusfix', 'input#search[autofocus] { display: none; }');
  640. ytwp.removeSearchAutofocus = function() {
  641. var e = document.querySelector('input#search');
  642. // ytwp.log('removeSearchAutofocus', e)
  643. if (e) {
  644. e.removeAttribute('autofocus')
  645. }
  646. }
  647.  
  648. ytwp.registerMastheadFix = function() {
  649. ytwp.log('registerMastheadFix');
  650. // Fix the offset when closing the Share widget (element.style.height = ~275px).
  651.  
  652. observe('#masthead-positioner-height-offset', {
  653. attributes: true,
  654. }, function(mutation) {
  655. console.log(mutation.type, mutation)
  656. if (mutation.attributeName === 'style') {
  657. var el = mutation.target;
  658. if (el.style.height) { // != ""
  659. setTimeout(function(){
  660. el.style.height = ""
  661. document.querySelector('#appbar-guide-menu').style.marginTop = "";
  662. }, 0);
  663. }
  664.  
  665. }
  666. });
  667. }
  668.  
  669. //--- Material UI
  670. ytwp.materialPageTransition = function() {
  671. ytwp.log('materialPageTransition')
  672. ytwp.init();
  673.  
  674. if (ytwp.isWatchUrl()) {
  675. ytwp.removeSearchAutofocus();
  676. ytwp.event.addBodyClass();
  677. // if (!ytwp.html5.app) {
  678. if (!ytwp.initialized) {
  679. ytwp.log('materialPageTransition !ytwp.html5.app', ytwp.html5.app)
  680. setTimeout(ytwp.materialPageTransition, 100);
  681. }
  682. var playerApi = document.querySelector('#player-api')
  683. if (playerApi) {
  684. playerApi.click()
  685. }
  686. } else {
  687. ytwp.event.onDispose();
  688. document.body.classList.remove(scriptBodyClassId);
  689. }
  690. ytwp.onScroll();
  691. ytwp.fixMasthead();
  692. ytwp.attemptToUpdatePlayer();
  693. };
  694.  
  695. //--- Listeners
  696. ytwp.registerListeners = function() {
  697. ytwp.registerMaterialListeners();
  698. ytwp.registerMastheadFix();
  699. };
  700.  
  701. ytwp.registerMaterialListeners = function() {
  702. // For Material UI
  703. HistoryEvent.listeners.push(ytwp.materialPageTransition);
  704. HistoryEvent.startTimer();
  705. // HistoryEvent.inject();
  706. // HistoryEvent.listeners.push(console.log.bind(console));
  707. };
  708.  
  709. ytwp.main = function() {
  710. ytwp.registerListeners();
  711. ytwp.init();
  712. ytwp.fixMasthead();
  713. };
  714.  
  715. ytwp.main();
  716.  
  717. // ytwp.updatePlayerTimerId = 0;
  718. ytwp.updatePlayerAttempts = -1;
  719. ytwp.updatePlayerMaxAttempts = 150; // 60fps = 2.5sec
  720. ytwp.attemptToUpdatePlayer = function() {
  721. console.log('ytwp.attemptToUpdatePlayer')
  722. if (0 <= ytwp.updatePlayerAttempts && ytwp.updatePlayerAttempts < ytwp.updatePlayerMaxAttempts) {
  723. ytwp.updatePlayerAttempts = 0;
  724. } else {
  725. ytwp.updatePlayerAttempts = 0;
  726. ytwp.attemptToUpdatePlayerTick();
  727. }
  728. // setTimeout(ytwp.updatePlayer, 10000); /// Just in case it's not caught
  729. }
  730. ytwp.attemptToUpdatePlayerTick = function() {
  731. console.log('ytwp.attemptToUpdatePlayerTick', ytwp.updatePlayerAttempts)
  732. if (ytwp.updatePlayerAttempts < ytwp.updatePlayerMaxAttempts) {
  733. ytwp.updatePlayerAttempts += 1;
  734. ytwp.updatePlayer();
  735. // ytwp.updatePlayerTimerId = setTimeout(ytwp.attemptToUpdatePlayerTick, 200);
  736. requestAnimationFrame(ytwp.attemptToUpdatePlayerTick);
  737. }
  738. }
  739.  
  740. ytwp.updatePlayer = function() {
  741. ytwp.removeSearchAutofocus();
  742. ytwp.enterTheaterMode();
  743. }
  744.  
  745. ytwp.toggleExtension = function() {
  746. document.body.classList.toggle('ytwp-window-player')
  747. ytwp.setTheaterMode(document.body.classList.contains('ytwp-window-player'))
  748. }
  749.  
  750.  
  751. //--- Main
  752. ytwp.materialPageTransition()
  753. setInterval(ytwp.updatePlayer, 2500);
  754.  
  755.  
  756. //--- Keyboard Shortcut
  757. function childOf(child, ancestor) {
  758. var parent = child.parentNode
  759. while (parent) {
  760. if (parent == ancestor) {
  761. return true
  762. }
  763. parent = parent.parentNode
  764. }
  765. return false
  766. }
  767. window.addEventListener('keypress', function(e){
  768. var isKey = e.key === 'w'
  769. var validTarget = (
  770. e.target === document.body
  771. || e.target.id === 'player-api'
  772. || e.target.id === 'movie_player'
  773. || childOf(e.target, document.querySelector('#movie_player'))
  774. )
  775.  
  776. if (validTarget && isKey) {
  777. e.preventDefault()
  778. ytwp.toggleExtension()
  779. }
  780. })
  781.  
  782.  
  783. //--- Browser Extension
  784. if (typeof browser !== "undefined") {
  785. browser.runtime.onMessage.addListener(request => {
  786. if (request.id == "toggle") {
  787. ytwp.toggleExtension()
  788.  
  789. return Promise.resolve({
  790. enabled: document.body.classList.contains('ytwp-window-player'),
  791. })
  792. } else {
  793. return Promise.reject(new Error('Unreconized message.id'))
  794. }
  795. });
  796. }
  797.  
  798. })(window);

QingJ © 2025

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