Youtube sticky Show Less button

Makes SHOW LESS button to be "sticky" to the video description section, so you can easily fold a long description without scrolling it all the way to its bottom.

安裝腳本?
作者推薦腳本

您可能也會喜歡 Youtube subtitles under video frame

安裝腳本
  1. /*
  2. Youtube sticky Show Less button: Makes SHOW LESS button to be "sticky"
  3. to the video description section, so you can easily fold a long description
  4. without scrolling it all the way to its bottom.
  5. Copyright (C) 2023 T1mL3arn
  6.  
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19.  
  20. // ==UserScript==
  21. // @name Youtube sticky Show Less button
  22. // @description Makes SHOW LESS button to be "sticky" to the video description section, so you can easily fold a long description without scrolling it all the way to its bottom.
  23. // @description:RU Делает кнопку СВЕРНУТЬ в описании видео "липкой". Чтобы свернуть длинное описание теперь не нужно прокручивать это описание в самый низ.
  24. // @namespace https://github.com/t1ml3arn-userscript-js
  25. // @version 1.3.1
  26. // @match https://www.youtube.com/*
  27. // @match https://youtube.com/*
  28. // @noframes
  29. // @grant none
  30. // @author T1mL3arn
  31. // @homepageURL https://github.com/t1ml3arn-userscript-js/Youtube-sticky-SHOW-LESS-button
  32. // @supportURL https://github.com/t1ml3arn-userscript-js/Youtube-sticky-SHOW-LESS-button/issues
  33. // @license GPL-3.0-only
  34. // ==/UserScript==
  35.  
  36.  
  37. const SHOWLESS_BTN_WRAP_CLS = 'sticky-show-less-btn-wrap';
  38. const STICKY_STYLE_ELT_ID = 'sticky-states-css'
  39.  
  40. const STICKY_STYLESHEET_CONTENT = `
  41.  
  42. #description-inline-expander {
  43. /*
  44. To make stickiness work I have to set "overflow: visible" on this element.
  45. Without this SHOW LESS button sticks with wrong way and does not work as intended.
  46.  
  47. Sticky elt sticks to its nearest ancestor that has a "scrolling mechanism"
  48. (created when overflow is hidden, scroll, auto, or overlay)
  49. So, disabling "show less" button parent's "scrolling mechanism"
  50. it is possible to make it work as expected.
  51.  
  52. Still, I neither know nor understand how this shit works.
  53.  
  54. See more:
  55. - https://developer.mozilla.org/en-US/docs/Web/CSS/position#sticky
  56. - https://uxdesign.cc/position-stuck-96c9f55d9526#b2ca
  57. */
  58. overflow: visible !important;
  59. }
  60.  
  61. .${SHOWLESS_BTN_WRAP_CLS} {
  62. position: sticky;
  63. bottom: 50px;
  64. text-align: right;
  65. bottom: 50%;
  66. pointer-events: none;
  67. z-index: 999;
  68. }
  69.  
  70. tp-yt-paper-button#collapse {
  71. pointer-events: initial;
  72. padding: 6px 16px;
  73. background: darkseagreen;
  74. color: white;
  75. margin-top: 0;
  76. }
  77. `;
  78.  
  79. let SETTINGS = {
  80. videoDescriptionSelector: 'ytd-video-secondary-info-renderer',
  81. videoTitleSelector: 'div#info.ytd-watch-flexy',
  82. showLessBtnSelector: 'ytd-expander.ytd-video-secondary-info-renderer tp-yt-paper-button#less.ytd-expander',
  83. }
  84.  
  85. function initOldDesignSettings() {
  86. const css = `
  87. ytd-page-manager {
  88. /*
  89. To make stickiness work I have to set "overflow: visible" on this element.
  90. Without this SHOW LESS button sticks with wrong way and does not work as intended.
  91. See more:
  92. - https://developer.mozilla.org/en-US/docs/Web/CSS/position#sticky
  93. - https://uxdesign.cc/position-stuck-96c9f55d9526#b2ca
  94. */
  95. overflow: visible !important;
  96. }
  97. .${SHOWLESS_BTN_WRAP_CLS} {
  98. position: sticky;
  99. bottom: 50px;
  100. text-align: right;
  101. bottom: 50%;
  102. pointer-events: none;
  103. }
  104. ytd-video-secondary-info-renderer tp-yt-paper-button#less.ytd-expander {
  105. pointer-events: initial;
  106. padding: 6px 16px;
  107. background: darkseagreen;
  108. }
  109. ytd-video-secondary-info-renderer tp-yt-paper-button#less.ytd-expander > .less-button {
  110. color: white;
  111. margin-top: 0;
  112. }
  113. `;
  114. SETTINGS = {
  115. ...SETTINGS,
  116. videoDescriptionSelector: 'ytd-video-secondary-info-renderer',
  117. videoTitleSelector: 'div#info.ytd-watch-flexy',
  118. showLessBtnSelector: 'ytd-expander.ytd-video-secondary-info-renderer tp-yt-paper-button#less.ytd-expander',
  119. css,
  120. }
  121. }
  122.  
  123. function addCss(css, id) {
  124. const style = document.head.appendChild(document.createElement('style'))
  125. style.textContent = css;
  126. style.id = id;
  127. }
  128.  
  129. function getVisibleElt(selector) {
  130. return Array.from(document.querySelectorAll(selector)).find(e => e.offsetParent != null)
  131. }
  132.  
  133. function fixScroll() {
  134. if (areCommentsVisible())
  135. preserveCommentsOnScreen()
  136. else if (isDescriptionTopEdgeOutView())
  137. scrollDescriptionIntoView();
  138. else {
  139. console.debug('do nothing with scroll')
  140. }
  141. }
  142.  
  143. function areCommentsVisible() {
  144. const vpHeight = window.visualViewport.height
  145. const commentsTop = getVisibleElt('ytd-comments').getBoundingClientRect().top
  146.  
  147. return commentsTop < vpHeight;
  148. }
  149.  
  150. function preserveCommentsOnScreen() {
  151. const descriptionElt = document.querySelector(SETTINGS.videoDescriptionSelector)
  152. // scrollOffset must not be negative!
  153. const scrollOffset = Math.abs(descriptionElt.getBoundingClientRect().height - descriptionHeight)
  154. let { scrollX, scrollY } = window;
  155. console.debug('preserve comments:', scrollY, scrollOffset, scrollY - scrollOffset)
  156.  
  157. scrollY = scrollY - scrollOffset;
  158. window.scrollTo(scrollX, scrollY)
  159. }
  160.  
  161. function isDescriptionTopEdgeOutView() {
  162. const descriptionElt = document.querySelector(SETTINGS.videoTitleSelector);
  163.  
  164. return descriptionElt.getBoundingClientRect().top < 0
  165. }
  166.  
  167. function scrollDescriptionIntoView() {
  168. console.debug('scroll description into view')
  169. document.querySelector(SETTINGS.videoTitleSelector).scrollIntoView({ behavior: 'smooth' })
  170. }
  171.  
  172. let descriptionHeight;
  173.  
  174. function saveDescriptionHeight() {
  175. // saving initial description elt height (it is needed to fix scroll position)
  176. const descriptionElt = document.querySelector(SETTINGS.videoDescriptionSelector)
  177. descriptionHeight = descriptionElt.getBoundingClientRect().height;
  178.  
  179. // at saveDescriptionHeight() call height might be not actual,
  180. // and delaying the reading helps
  181. setTimeout(() => {
  182. descriptionHeight = document.querySelector(SETTINGS.videoDescriptionSelector).getBoundingClientRect().height
  183. }, 0);
  184. }
  185.  
  186. function enchanceShowLessButton() {
  187. for (const showLessBtn of document.querySelectorAll(SETTINGS.showLessBtnSelector)) {
  188. const showLessParent = showLessBtn.parentElement
  189. const btnWrap = document.createElement('div')
  190. btnWrap.appendChild(showLessBtn)
  191. // I use wrap to intercept clicks in CAPTURE phase
  192. // to calcalute scroll offset BEFORE youtube hides the description
  193. btnWrap.addEventListener('click', fixScroll, true)
  194. const stickyWrap = document.createElement('div');
  195. stickyWrap.classList.add(SHOWLESS_BTN_WRAP_CLS)
  196. stickyWrap.appendChild(btnWrap);
  197. // add sticky wrapper (with showless button) to video description element
  198. showLessParent.appendChild(stickyWrap)
  199. }
  200.  
  201. }
  202.  
  203. function init() {
  204.  
  205. // Looks like 'yt-page-data-updated' is the event I need to listen
  206. // to know exactly when youtube markup is ready to be queried.
  207. document.addEventListener('yt-page-data-updated', _ => {
  208. // Script should work only for pages with a video,
  209. // such pages have url like https://www.youtube.com/watch?v=25YbRHAc_h4
  210. if (window.location.search.includes('v=')) {
  211.  
  212. // settings for the actual design
  213. SETTINGS = {
  214. videoDescriptionSelector: '#above-the-fold.ytd-watch-metadata',
  215. videoTitleSelector: '#above-the-fold.ytd-watch-metadata',
  216. showLessBtnSelector: 'tp-yt-paper-button#collapse',
  217. css: STICKY_STYLESHEET_CONTENT,
  218. }
  219. addCss(SETTINGS.css, STICKY_STYLE_ELT_ID)
  220.  
  221. try {
  222. saveDescriptionHeight()
  223. enchanceShowLessButton()
  224. } catch (e) {}
  225.  
  226. setTimeout(() => {
  227. // if for some reason there is old design in use
  228.  
  229. const elt = document.querySelector('ytd-video-secondary-info-renderer.ytd-watch-flexy')
  230. if (elt?.offsetParent != null) {
  231. console.log('OLD design detected');
  232.  
  233. initOldDesignSettings()
  234. document.getElementById(STICKY_STYLE_ELT_ID)?.remove();
  235. addCss(SETTINGS.css, STICKY_STYLE_ELT_ID)
  236. saveDescriptionHeight()
  237. enchanceShowLessButton()
  238. }
  239. }, 125);
  240. }
  241. })
  242. }
  243.  
  244. init()