YouTube aspect ratio switcher

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

目前为 2018-07-25 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube aspect ratio switcher
  3. // @namespace https://greasyfork.org/en/users/27283-mutationobserver
  4. // @version 2018.07.25v1
  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. .aspectRatio16_9 #aspectratioSwitcher,
  26. .aspectRatio4_3 #aspectratioSwitcher {
  27. font-size: unset;
  28. }
  29. .html5-main-video { transition: .2s; }
  30. .aspectRatio16_9 .html5-main-video { transform: scale(1.335,1)!important; }
  31. .aspectRatio4_3 .html5-main-video { transform: scale(.75,1)!important; }
  32. </style>
  33. `;
  34. if (!document.querySelector(".ytp-subtitles-button.ytp-button")) var anchorElem = document.querySelector(".ytp-button.ytp-settings-button");
  35. else var anchorElem = document.querySelector(".ytp-subtitles-button.ytp-button");
  36.  
  37. anchorElem.insertAdjacentHTML("beforebegin", buttonhtml + csshtml);
  38.  
  39. var buttonElem = document.querySelector("#aspectratioSwitcher");
  40.  
  41. buttonElem.addEventListener("click", aspectRatioSwitch);
  42.  
  43. function aspectRatioSwitch() {
  44. var videoElem = document.querySelector("#player");
  45. if (!currentRatio) {
  46. videoElem.classList.add("aspectRatio16_9");
  47. currentRatio = "16:9";
  48. buttonElem.innerHTML = currentRatio;
  49. return;
  50. }
  51. if (currentRatio == "16:9") {
  52. videoElem.classList.remove("aspectRatio16_9");
  53. videoElem.classList.add("aspectRatio4_3");
  54. currentRatio = "4:3";
  55. buttonElem.innerHTML = currentRatio;
  56. return;
  57. }
  58. if (currentRatio == "4:3") {
  59. videoElem.classList.remove("aspectRatio4_3");
  60. currentRatio = null;
  61. buttonElem.innerHTML = "↔";
  62. return;
  63. }
  64. }
  65.  
  66. })();