YouTube Video Resize Fix

此 Userscript 可以解决视频大小变形问题。请将它与其他Userstyles / Userscripts一起使用。

目前为 2022-12-29 提交的版本,查看 最新版本

  1. /*
  2.  
  3. MIT License
  4.  
  5. Copyright 2022 CY Fung
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in all
  15. copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24.  
  25. */
  26. // ==UserScript==
  27. // @name YouTube Video Resize Fix
  28. // @name:ja YouTube Video Resize Fix
  29. // @name:zh-TW YouTube Video Resize Fix
  30. // @name:zh-CN YouTube Video Resize Fix
  31. // @version 0.1.3
  32. // @description This Userscript can fix the video sizing issue. Please use it with other Userstyles / Userscripts.
  33. // @description:ja この Userscript は、動画のサイズ変更の問題を修正できます。 他のユーザースタイル・ユーザースクリプトと合わせてご利用ください。
  34. // @description:zh-TW 此 Userscript 可以解決影片大小變形問題。 請將它與其他Userstyles / Userscripts一起使用。
  35. // @description:zh-CN 此 Userscript 可以解决视频大小变形问题。请将它与其他Userstyles / Userscripts一起使用。
  36. // @namespace http://tampermonkey.net/
  37. // @author CY Fung
  38. // @license MIT License
  39. // @supportURL https://github.com/cyfung1031/userscript-supports
  40. // @run-at document-start
  41. // @match https://www.youtube.com/watch*
  42. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  43. // @grant none
  44. // @unwrap
  45. // @allFrames
  46. // @inject-into page
  47. // ==/UserScript==
  48.  
  49. /* jshint esversion:8 */
  50.  
  51. (function () {
  52. 'use strict';
  53. const elements = {}
  54. let rid1 = 0
  55. let rid2 = 0
  56. /** @type {MutationObserver | null} */
  57. let attrObserver = null
  58. /** @type {ResizeObserver | null} */
  59. let resizeObserver = null
  60. const core = {
  61. begin() {
  62. document.addEventListener('yt-player-updated', core.hanlder, true)
  63. document.addEventListener('ytd-navigate-finish', core.hanlder, true)
  64. },
  65. hanlder() {
  66. rid1++
  67. if (rid1 > 1e9) rid1 = 9
  68. const tid = rid1
  69. window.requestAnimationFrame(() => {
  70. if (tid !== rid1) return
  71. core.runner()
  72. })
  73. },
  74. async runner() {
  75. if (!location.href.startsWith('https://www.youtube.com/watch')) return
  76.  
  77. elements.ytdFlexy = document.querySelector('ytd-watch-flexy')
  78. elements.video = document.querySelector('ytd-watch-flexy #movie_player video')
  79. if (elements.ytdFlexy && elements.video) { } else return
  80.  
  81. // resize Video
  82. let { ytdFlexy } = elements;
  83. if (!ytdFlexy.ElYTL) {
  84. ytdFlexy.ElYTL = 1;
  85. (function (ytdFlexy, calculateNormalPlayerSize_, calculateCurrentPlayerSize_) {
  86. ytdFlexy.calculateNormalPlayerSize_ = function () {
  87. return core.isSkip() ? calculateNormalPlayerSize_.call(this) : core.calculateSize();
  88. };
  89. ytdFlexy.calculateCurrentPlayerSize_ = function () {
  90. return core.isSkip() ? calculateCurrentPlayerSize_.call(this) : core.calculateSize();
  91. };
  92. })(ytdFlexy, ytdFlexy.calculateNormalPlayerSize_, ytdFlexy.calculateCurrentPlayerSize_);
  93. }
  94. ytdFlexy = null
  95.  
  96. // when video is fetched
  97. elements.video.removeEventListener('canplay', core.triggerResizeDelayed, false)
  98. elements.video.addEventListener('canplay', core.triggerResizeDelayed, false)
  99.  
  100. // when video is resized
  101. if (resizeObserver) {
  102. resizeObserver.disconnect()
  103. resizeObserver = null
  104. }
  105. if (typeof ResizeObserver === 'function') {
  106. resizeObserver = new ResizeObserver(core.triggerResizeDelayed);
  107. resizeObserver.observe(elements.video)
  108. }
  109.  
  110. // MutationObserver:[collapsed] @ ytd-live-chat-frame#chat
  111. if (attrObserver) {
  112. attrObserver.takeRecords()
  113. attrObserver.disconnect()
  114. attrObserver = null
  115. }
  116. let chat = document.querySelector('ytd-watch-flexy ytd-live-chat-frame#chat')
  117. if (chat) {
  118. // resize due to DOM update
  119. attrObserver = new MutationObserver(core.triggerResizeDelayed);
  120. attrObserver.observe(chat, { attributes: true, attributeFilter: ["collapsed"] });
  121. chat = null
  122. }
  123.  
  124. // resize on idle
  125. core.triggerResizeDelayed()
  126. },
  127. isSkip() {
  128. const { ytdFlexy } = elements
  129. return ytdFlexy.theater === true || ytdFlexy.isTwoColumns_ === false || document.fullscreenElement !== null
  130. },
  131. calculateSize() {
  132. const { video } = elements
  133. let elm = video.closest('#movie_player')
  134. elm = elm || video
  135. const rect = elm.getBoundingClientRect()
  136. return { width: rect.width, height: rect.height };
  137. },
  138. triggerResizeDelayed() {
  139. rid2++
  140. if (rid2 > 1e9) rid2 = 9
  141. const tid = rid2
  142. window.requestAnimationFrame(() => {
  143. if (tid !== rid2) return
  144. window.dispatchEvent(new Event('resize'))
  145. })
  146. }
  147. };
  148. core.begin();
  149. })();