YouTube aspect ratio switcher

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

目前為 2019-06-21 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name YouTube aspect ratio switcher
  3. // @namespace https://greasyfork.org/en/users/27283-mutationobserver
  4. // @version 2019.01.07v3
  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 buttonhtml = `<button id="aspectratioSwitcher" class="ytp-button" title="Aspect ratio">↔</button>`;
  17. var csshtml = `
  18. <style>
  19. #aspectratioSwitcher {
  20. top: -13px;
  21. position: relative;
  22. text-align: center;
  23. font-size: 19px;
  24. }
  25. .ytp-big-mode #aspectratioSwitcher {
  26. font-size: 182%;
  27. top: -19px;
  28. }
  29. .aspectRatio16_9 #aspectratioSwitcher,
  30. .aspectRatio4_3 #aspectratioSwitcher {
  31. font-size: unset;
  32. }
  33. .html5-main-video { transition: .2s; }
  34. .aspectRatio16_9 .html5-main-video { transform: scale(1.335,1)!important; }
  35. .aspectRatio4_3 .html5-main-video { transform: scale(.75,1)!important; }
  36. </style>
  37. `;
  38. if (!document.querySelector(".ytp-subtitles-button.ytp-button")) var anchorElem = document.querySelector(".ytp-button.ytp-settings-button");
  39. else var anchorElem = document.querySelector(".ytp-subtitles-button.ytp-button");
  40.  
  41. anchorElem.insertAdjacentHTML("beforebegin", buttonhtml + csshtml);
  42.  
  43. var buttonElem = document.querySelector("#aspectratioSwitcher");
  44.  
  45. buttonElem.addEventListener("click", aspectRatioSwitch);
  46.  
  47. function aspectRatioSwitch() {
  48. var videoElem = document.querySelector("#movie_player");
  49. if (!currentRatio) {
  50. videoElem.classList.add("aspectRatio16_9");
  51. currentRatio = "16:9";
  52. buttonElem.innerHTML = currentRatio;
  53. return;
  54. }
  55. if (currentRatio == "16:9") {
  56. videoElem.classList.remove("aspectRatio16_9");
  57. videoElem.classList.add("aspectRatio4_3");
  58. currentRatio = "4:3";
  59. buttonElem.innerHTML = currentRatio;
  60. return;
  61. }
  62. if (currentRatio == "4:3") {
  63. videoElem.classList.remove("aspectRatio4_3");
  64. currentRatio = null;
  65. buttonElem.innerHTML = "↔";
  66. return;
  67. }
  68. }
  69.  
  70. })();