YouTube Video Resize Fix

此 Userscript 可以解決影片大小變形問題。 請將它與其他Userstyles / Userscripts一起使用。

目前為 2023-01-03 提交的版本,檢視 最新版本

  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.2.0
  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. let isHTMLAttrApplied = false
  61. const core = {
  62. begin() {
  63. document.addEventListener('yt-player-updated', core.hanlder, true)
  64. document.addEventListener('ytd-navigate-finish', core.hanlder, true)
  65. },
  66. hanlder() {
  67. rid1++
  68. if (rid1 > 1e9) rid1 = 9
  69. const tid = rid1
  70. window.requestAnimationFrame(() => {
  71. if (tid !== rid1) return
  72. core.runner()
  73. })
  74. },
  75. async runner() {
  76. if (!location.href.startsWith('https://www.youtube.com/watch')) return
  77.  
  78. elements.ytdFlexy = document.querySelector('ytd-watch-flexy')
  79. elements.video = document.querySelector('ytd-watch-flexy #movie_player video')
  80. if (elements.ytdFlexy && elements.video) { } else return
  81. elements.moviePlayer = elements.video.closest('#movie_player')
  82. if (!elements.moviePlayer) return
  83.  
  84. // resize Video
  85. let { ytdFlexy } = elements;
  86. if (!ytdFlexy.ElYTL) {
  87. ytdFlexy.ElYTL = 1;
  88. ytdFlexy.calculateNormalPlayerSize_ = core.resizeFunc(ytdFlexy.calculateNormalPlayerSize_, 1);
  89. ytdFlexy.calculateCurrentPlayerSize_ = core.resizeFunc(ytdFlexy.calculateCurrentPlayerSize_, 0);
  90. }
  91. ytdFlexy = null
  92.  
  93. // when video is fetched
  94. elements.video.removeEventListener('canplay', core.triggerResizeDelayed, false)
  95. elements.video.addEventListener('canplay', core.triggerResizeDelayed, false)
  96.  
  97. // when video is resized
  98. if (resizeObserver) {
  99. resizeObserver.disconnect()
  100. resizeObserver = null
  101. }
  102. if (typeof ResizeObserver === 'function') {
  103. resizeObserver = new ResizeObserver(core.triggerResizeDelayed);
  104. resizeObserver.observe(elements.moviePlayer)
  105. }
  106.  
  107. // MutationObserver:[collapsed] @ ytd-live-chat-frame#chat
  108. if (attrObserver) {
  109. attrObserver.takeRecords()
  110. attrObserver.disconnect()
  111. attrObserver = null
  112. }
  113. let chat = document.querySelector('ytd-watch-flexy ytd-live-chat-frame#chat')
  114. if (chat) {
  115. // resize due to DOM update
  116. attrObserver = new MutationObserver(core.triggerResizeDelayed);
  117. attrObserver.observe(chat, { attributes: true, attributeFilter: ["collapsed"] });
  118. chat = null
  119. }
  120.  
  121. // resize on idle
  122. core.triggerResizeDelayed()
  123. },
  124. resizeFunc(originalFunc, kb) {
  125. return function () {
  126. rid2++
  127. if (!isHTMLAttrApplied) {
  128. isHTMLAttrApplied = true
  129. Promise.resolve(0).then(() => {
  130. document.documentElement.classList.add('youtube-video-resize-fix')
  131. /*
  132. let previewHoverContainer = document.querySelector('#movie_player > div[aria-live="polite"].ytp-tooltip.ytp-bottom.ytp-preview')
  133. if(previewHoverContainer){
  134. let div = document.createElement('div');
  135. div.classList.add('ytp-tooltip','ytp-bottom','ytp-preview')
  136. previewHoverContainer.before(div)
  137. div.append(previewHoverContainer)
  138. }
  139. */
  140. }).catch(console.warn)
  141. }
  142. if (document.fullscreenElement === null) {
  143. const ytdFlexy = this
  144. if (!kb && ytdFlexy.isTwoColumns_ === false) {
  145. return { width: NaN, height: NaN }
  146. }
  147. if (kb || !core.isSkip()) {
  148. let ret = core.calculateSize();
  149. if (ret.height > 0 && ret.width > 0) {
  150. return ret
  151. }
  152. }
  153. }
  154. return originalFunc.apply(this, arguments)
  155. };
  156. },
  157. isSkip() {
  158. const { ytdFlexy } = elements
  159. return ytdFlexy.theater === true || ytdFlexy.isTwoColumns_ === false || document.fullscreenElement !== null
  160. },
  161. calculateSize_() {
  162. const { moviePlayer, video } = elements
  163. const rect1 = video.getBoundingClientRect()
  164. const rect2 = moviePlayer.getBoundingClientRect()
  165. if (rect1.width && rect1.height && rect1.width / rect1.height > 0) {
  166. let h2 = rect2.width * rect1.height / rect1.width
  167. let w2 = rect2.height * rect1.width / rect1.height
  168. return { rect1, rect2, h2, w2 }
  169. } else {
  170. return null
  171. }
  172. },
  173. calculateSize() {
  174. let rs = core.calculateSize_()
  175. if (!rs) return { width: NaN, height: NaN }
  176. const { rect1, rect2, h2, w2 } = rs
  177. if (h2 > rect2.height) {
  178. return { width: w2, height: rect2.height }
  179. } else {
  180. return { width: rect2.width, height: h2 }
  181. }
  182. },
  183. triggerResizeDelayed() {
  184. rid2++
  185. if (rid2 > 1e9) rid2 = 9
  186. const tid = rid2
  187. window.requestAnimationFrame(() => {
  188. if (tid !== rid2) return
  189. window.dispatchEvent(new Event('resize'))
  190. })
  191. }
  192. };
  193. core.begin();
  194. })();