YouTube aspect ratio switcher

Adds an aspect ratio switcher to the player, for videos that have the wrong aspect ratio.

  1. // ==UserScript==
  2. // @name YouTube aspect ratio switcher
  3. // @namespace https://gf.qytechs.cn/en/users/27283-mutationobserver
  4. // @version 2023.02.01v5
  5. // @description Adds an aspect ratio switcher to the player, for videos that have the wrong aspect ratio.
  6. // @author MutationObserver
  7. // @match https://*.youtube.com/watch*v=*
  8. // @match https://*.youtube.com/embed/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. var currentRatio = null;
  16. var aspectRatiosIndex = 0;
  17. var videoElemAttr = "data-aspectRatio-userscript";
  18. var buttonhtml = `<button id="aspectratioSwitcher" class="ytp-button" title="Aspect ratio">↔</button>`;
  19. var csshtml = `
  20. <style>
  21. #aspectratioSwitcher {
  22. top: -13px;
  23. position: relative;
  24. text-align: center;
  25. font-size: 25px;
  26. }
  27. .ytp-big-mode #aspectratioSwitcher {
  28. font-size: 182%;
  29. top: -19px;
  30. }
  31. #movie_player[data-aspectRatio-userscript="16:9"] #aspectratioSwitcher,
  32. #movie_player[data-aspectRatio-userscript="9:16"] #aspectratioSwitcher,
  33. #movie_player[data-aspectRatio-userscript="4:3"] #aspectratioSwitcher {
  34. font-size: unset;
  35. }
  36. .html5-main-video { transition: .2s; }
  37. #movie_player[data-aspectRatio-userscript="16:9"] .html5-main-video { transform: scale(1.335,1)!important; }
  38. #movie_player[data-aspectRatio-userscript="4:3"] .html5-main-video { transform: scale(.75,1)!important; }
  39. #movie_player[data-aspectRatio-userscript="9:16"] .html5-main-video { transform: scale(1.77,1)!important; }
  40. </style>
  41. `;
  42. if (!document.querySelector(".ytp-subtitles-button.ytp-button")) var anchorElem = document.querySelector(".ytp-button.ytp-settings-button");
  43. else var anchorElem = document.querySelector(".ytp-subtitles-button.ytp-button");
  44.  
  45. anchorElem.insertAdjacentHTML("beforebegin", buttonhtml + csshtml);
  46.  
  47. var buttonElem = document.querySelector("#aspectratioSwitcher");
  48.  
  49. buttonElem.addEventListener("click", aspectRatioSwitch);
  50.  
  51. function aspectRatioSwitch() {
  52. var videoElem = document.querySelector("#movie_player");
  53. aspectRatiosIndex++;
  54. if (aspectRatiosIndex > 3) aspectRatiosIndex = 0;
  55. switch(aspectRatiosIndex) {
  56. case 1:
  57. currentRatio = "4:3";
  58. videoElem.setAttribute(videoElemAttr, currentRatio);
  59. buttonElem.innerHTML = currentRatio;
  60. return;
  61. break;
  62. case 2:
  63. currentRatio = "16:9";
  64. videoElem.setAttribute(videoElemAttr, currentRatio);
  65. buttonElem.innerHTML = currentRatio;
  66. return;
  67. break;
  68. case 3:
  69. currentRatio = "9:16";
  70. videoElem.setAttribute(videoElemAttr, currentRatio);
  71. buttonElem.innerHTML = currentRatio;
  72. return;
  73. break;
  74. default:
  75. videoElem.setAttribute(videoElemAttr, "");
  76. currentRatio = null;
  77. buttonElem.innerHTML = "↔";
  78. return;
  79. }
  80. }
  81.  
  82. })();

QingJ © 2025

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