Reddit - Big Card View

Makes the card view on Reddit bigger and improves ways content is displayed to go along with that.

目前为 2024-10-01 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit - Big Card View
  3. // @namespace 1N07
  4. // @version 0.3
  5. // @description Makes the card view on Reddit bigger and improves ways content is displayed to go along with that.
  6. // @author 1N07
  7. // @license MIT
  8. // @match https://www.reddit.com/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=reddit.com
  10. // @require https://update.greasyfork.org/scripts/511046/1457685/DeindentTemplateLiteralString.js
  11. // @run-at document-body
  12. // @grant GM_addStyle
  13. // @grant GM_addElement
  14. // @grant GM_registerMenuCommand
  15. // @grant GM_unregisterMenuCommand
  16. // @grant GM_getValue
  17. // @grant GM_setValue
  18. // ==/UserScript==
  19.  
  20. (() => {
  21. //initialize settings
  22. let MainContainerWidthHandle;
  23. let MainContainerWidth = GM_getValue("MainContainerWidth", "75%");
  24. SetMainContainerWidthHandle();
  25. let PostImageHeightHandle;
  26. let PostImageHeight = GM_getValue("PostImageHeight", "70vh");
  27. SetPostImageHeightHandle();
  28.  
  29. //add main stylesheet to DOM
  30. GM_addStyle(`
  31. .subgrid-container {
  32. max-width: calc(100vw - 272px);
  33. width: calc(100vw - 272px);
  34. }
  35. .main-container {
  36. justify-content: center;
  37. }
  38. #main-content {
  39. max-width: calc(100% - 1rem - 316px);
  40. width: ${MainContainerWidth};
  41. }
  42. #right-sidebar-container:has(> aside) {
  43. display: none;
  44. }
  45. #main-content:has(+ #right-sidebar-container > aside) {
  46. max-width: 100%;
  47. }
  48.  
  49. /*Single image post height*/
  50. #main-content [slot='post-media-container'] shreddit-aspect-ratio{
  51. --max-height: min(100%, ${PostImageHeight}) !important;
  52. }
  53.  
  54. #main-content [slot='post-media-container'] gallery-carousel ul {
  55. column-gap: 5px;
  56. }
  57.  
  58. #main-content [slot='post-media-container'] gallery-carousel ul > li {
  59. visibility: visible !important;
  60. }
  61. `);
  62.  
  63. //start tracking multi image carousels to improve on how they are displayed.
  64. setInterval(() => {
  65. const carousels = document.querySelectorAll(
  66. "#main-content [slot='post-media-container'] gallery-carousel:not(.rbcv-carousel-height-set)",
  67. );
  68. for (const carousel of carousels) {
  69. const faceplate =
  70. carousel.shadowRoot?.querySelector("faceplate-carousel");
  71. if (faceplate) {
  72. faceplate.style.maxHeight = "70vh";
  73. faceplate.style.height = "70vh";
  74.  
  75. carousel.classList.add("rbcv-carousel-height-set");
  76.  
  77. const carouselImgs = carousel.querySelectorAll(
  78. "ul > li img:not(.rbcv-image-handled)",
  79. );
  80. for (const carouselImg of carouselImgs) {
  81. if (!TrySetCarouselImgWidth(carouselImg)) {
  82. carouselImg.onload = (e) => {
  83. e.target.onload = null;
  84. TrySetCarouselImgWidth(e.target);
  85. };
  86. }
  87. if (!carouselImg.src)
  88. carouselImg.src = carouselImg.getAttribute("data-lazy-src");
  89. carouselImg.classList.add("rbcv-image-handled");
  90. }
  91. }
  92. }
  93. }, 500);
  94.  
  95. //=== FUNCTIONS ===//
  96. function TrySetCarouselImgWidth(img) {
  97. //TODO: check if image is too wide for carousel, if so, set width so it fits, preserving ratio. i.e. set smaller height.
  98. if (img?.naturalWidth > 0) {
  99. const naturalRatio = img.naturalWidth / img.naturalHeight;
  100. const calculatedWidth = img.clientHeight * naturalRatio;
  101.  
  102. if (img.parentNode?.tagName === "LI") {
  103. const slot = img.parentNode.getAttribute("slot");
  104. const postId = img.closest("gallery-carousel").getAttribute("post-id");
  105. GM_addElement(img.parentNode, "style", {
  106. class: "carousel-image-widths",
  107. textContent: `gallery-carousel[post-id="${postId}"] li[slot=${slot}] { width: ${calculatedWidth}px !important; }`,
  108. });
  109. }
  110. return true;
  111. }
  112.  
  113. return false;
  114. }
  115. function SetMainContainerWidthHandle() {
  116. GM_unregisterMenuCommand(MainContainerWidthHandle);
  117.  
  118. MainContainerWidthHandle = GM_registerMenuCommand(
  119. `Set main reddit content container width (${MainContainerWidth}) -click to change-`,
  120. () => {
  121. const newMainContainerWidth = prompt(
  122. Deindent(`
  123. Set main reddit content container width.
  124. Values that would result in a main container wider than the viewport, fallback to 100% width.
  125. Use any valid CSS unit value.
  126. `),
  127. MainContainerWidth,
  128. );
  129. if (newMainContainerWidth) {
  130. MainContainerWidth = newMainContainerWidth;
  131. GM_setValue("MainContainerWidth", MainContainerWidth);
  132. SetMainContainerWidthHandle();
  133.  
  134. if (
  135. confirm(
  136. 'Press "OK" to refresh the page to apply new settings or "cancel" refresh and apply later.',
  137. )
  138. ) {
  139. location.reload();
  140. }
  141. }
  142. },
  143. );
  144. }
  145. function SetPostImageHeightHandle() {
  146. GM_unregisterMenuCommand(PostImageHeightHandle);
  147.  
  148. PostImageHeightHandle = GM_registerMenuCommand(
  149. `Set post image height (${PostImageHeight}) -click to change-`,
  150. () => {
  151. const newPostImageHeight = prompt(
  152. Deindent(`
  153. Set post image height.
  154. Use any valid CSS unit value.
  155. `),
  156. PostImageHeight,
  157. );
  158. if (newPostImageHeight) {
  159. PostImageHeight = newPostImageHeight;
  160. GM_setValue("PostImageHeight", PostImageHeight);
  161. SetPostImageHeightHandle();
  162.  
  163. if (
  164. confirm(
  165. 'Press "OK" to refresh the page to apply new settings or "cancel" refresh and apply later.',
  166. )
  167. ) {
  168. location.reload();
  169. }
  170. }
  171. },
  172. );
  173. }
  174. })();