Arslay_Three

Untitled

Oct 11th, 2026 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.30 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Reddit Gallery Justified Grid & Full-Res Opener
  3. // @namespace https://tampermonkey.net/
  4. // @version 5.4
  5. // @description Эргономичная укладка фото на Reddit в плитку + открытие оригиналов по ЛКМ и колесику для любых постов
  6. // @author You
  7. // @match https://*.reddit.com/*
  8. // @grant GM_addStyle
  9. // @grant GM_openInTab
  10. // @run-at document-idle
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. const css = `
  17. /* 1. Снимаем обрезку по высоте для одиночных картинок в ленте */
  18. div[slot="post-media-container"] {
  19. height: auto !important;
  20. max-height: none !important;
  21. overflow: visible !important;
  22. }
  23.  
  24. div[slot="post-media-container"] img {
  25. cursor: pointer !important;
  26. max-height: 85vh !important;
  27. object-fit: contain !important;
  28. }
  29.  
  30. /* Обертка-ссылка для одиночных картинок (display: contents не ломает верстку) */
  31. .reddit-single-img-link {
  32. display: contents !important;
  33. cursor: pointer !important;
  34. }
  35.  
  36. /* 2. Контейнер умной укладки (заполняет 100% ширины) */
  37. .reddit-custom-grid {
  38. --row-height: 220px;
  39. display: flex !important;
  40. flex-wrap: wrap !important;
  41. gap: 8px !important;
  42. width: 100% !important;
  43. margin: 8px 0 16px 0 !important;
  44. box-sizing: border-box !important;
  45. }
  46.  
  47. /* Ячейка плитки */
  48. .reddit-custom-grid-item {
  49. position: relative !important;
  50. flex-grow: var(--ar, 1) !important;
  51. flex-shrink: 1 !important;
  52. flex-basis: calc(var(--ar, 1) * var(--row-height)) !important;
  53. aspect-ratio: var(--ar, 1) !important;
  54. max-height: 520px !important;
  55. min-width: 120px !important;
  56. border-radius: 8px !important;
  57. overflow: hidden !important;
  58. background: #141414 !important;
  59. box-shadow: 0 1px 4px rgba(0,0,0,0.25) !important;
  60. }
  61.  
  62. .reddit-custom-grid-item a {
  63. display: block !important;
  64. width: 100% !important;
  65. height: 100% !important;
  66. cursor: pointer !important;
  67. }
  68.  
  69. .reddit-custom-grid-item img {
  70. width: 100% !important;
  71. height: 100% !important;
  72. object-fit: cover !important;
  73. display: block !important;
  74. transition: transform 0.2s ease, filter 0.2s ease !important;
  75. }
  76.  
  77. .reddit-custom-grid-item:hover img {
  78. transform: scale(1.03) !important;
  79. filter: brightness(1.06) !important;
  80. }
  81.  
  82. /* Бейдж с номером фото */
  83. .reddit-custom-grid-badge {
  84. position: absolute !important;
  85. bottom: 6px !important;
  86. right: 6px !important;
  87. background: rgba(0, 0, 0, 0.72) !important;
  88. color: #fff !important;
  89. font-size: 11px !important;
  90. font-weight: 600 !important;
  91. padding: 2px 6px !important;
  92. border-radius: 4px !important;
  93. pointer-events: none !important;
  94. font-family: sans-serif !important;
  95. backdrop-filter: blur(2px) !important;
  96. }
  97. `;
  98.  
  99. if (typeof GM_addStyle !== 'undefined') {
  100. GM_addStyle(css);
  101. } else {
  102. const style = document.createElement('style');
  103. style.textContent = css;
  104. document.head.appendChild(style);
  105. }
  106.  
  107. // Получение ссылки на оригинальное фото
  108. function getFullImageUrl(post, mediaContainer, img) {
  109. let fullUrl = null;
  110. const contentHref = post?.getAttribute('content-href');
  111. if (contentHref && (contentHref.includes('i.redd.it') || /\.(jpg|jpeg|png|webp|gif)(\?.*)?$/i.test(contentHref))) {
  112. fullUrl = contentHref;
  113. }
  114.  
  115. if (!fullUrl) {
  116. const lightboxImg = mediaContainer?.querySelector('.lightboxed-content img');
  117. if (lightboxImg?.src) fullUrl = lightboxImg.src;
  118. }
  119.  
  120. if (!fullUrl && img?.src) {
  121. if (img.src.includes('preview.redd.it')) {
  122. fullUrl = img.src.replace(/https:\/\/(?:preview|i)\.redd\.it\/([^?]+).*/, 'https://i.redd.it/$1');
  123. } else {
  124. fullUrl = img.src;
  125. }
  126. }
  127. return fullUrl;
  128. }
  129.  
  130. // 1. Обработка галерей (много картинок -> в умную плитку)
  131. function processGallery(carousel) {
  132. if (carousel.dataset.customGridDone === 'true') return;
  133.  
  134. const listItems = carousel.querySelectorAll('ul > li');
  135. if (!listItems || listItems.length === 0) return;
  136.  
  137. const images = [];
  138. listItems.forEach(li => {
  139. const fullImg = li.querySelector('.lightboxed-content img');
  140. const figureImg = li.querySelector('figure img.media-lightbox-img') || li.querySelector('figure img');
  141.  
  142. const fullUrl = fullImg?.src || figureImg?.src;
  143. const previewUrl = figureImg?.src || fullUrl;
  144.  
  145. const width = parseInt(figureImg?.getAttribute('width')) || parseInt(fullImg?.getAttribute('width')) || 0;
  146. const height = parseInt(figureImg?.getAttribute('height')) || parseInt(fullImg?.getAttribute('height')) || 0;
  147. const ar = (width && height) ? (width / height).toFixed(3) : null;
  148.  
  149. if (previewUrl) {
  150. images.push({
  151. full: fullUrl,
  152. preview: previewUrl,
  153. alt: figureImg?.alt || '',
  154. ar: ar
  155. });
  156. }
  157. });
  158.  
  159. if (images.length > 0) {
  160. carousel.dataset.customGridDone = 'true';
  161.  
  162. const grid = document.createElement('div');
  163. grid.className = 'reddit-custom-grid';
  164.  
  165. images.forEach((imgData, index) => {
  166. const item = document.createElement('div');
  167. item.className = 'reddit-custom-grid-item';
  168.  
  169. if (imgData.ar) {
  170. item.style.setProperty('--ar', imgData.ar);
  171. }
  172.  
  173. const link = document.createElement('a');
  174. link.href = imgData.full;
  175. link.target = '_blank';
  176. link.title = 'Открыть оригинал';
  177. link.addEventListener('click', (e) => e.stopPropagation());
  178. link.addEventListener('auxclick', (e) => e.stopPropagation());
  179.  
  180. const img = document.createElement('img');
  181. img.src = imgData.preview;
  182. img.alt = imgData.alt;
  183. img.loading = 'lazy';
  184.  
  185. if (!imgData.ar) {
  186. img.onload = () => {
  187. if (img.naturalWidth && img.naturalHeight) {
  188. const naturalAr = (img.naturalWidth / img.naturalHeight).toFixed(3);
  189. item.style.setProperty('--ar', naturalAr);
  190. }
  191. };
  192. }
  193.  
  194. const badge = document.createElement('span');
  195. badge.className = 'reddit-custom-grid-badge';
  196. badge.textContent = `${index + 1}/${images.length}`;
  197.  
  198. link.appendChild(img);
  199. item.appendChild(link);
  200. item.appendChild(badge);
  201. grid.appendChild(item);
  202. });
  203.  
  204. carousel.style.setProperty('display', 'none', 'important');
  205. carousel.parentNode.insertBefore(grid, carousel);
  206. }
  207. }
  208.  
  209. // 2. Обработка постов с 1 картинкой (оборачиваем в нативную ссылку <a> для поддержки колесика)
  210. function processSingleImages() {
  211. const containers = document.querySelectorAll('div[slot="post-media-container"]');
  212. containers.forEach(container => {
  213. if (container.dataset.singleDone === 'true') return;
  214. if (container.querySelector('.reddit-custom-grid')) return;
  215. if (container.querySelector('shreddit-player')) return;
  216. if (container.querySelector('gallery-carousel')) return;
  217.  
  218. const img = container.querySelector('img.media-lightbox-img') ||
  219. container.querySelector('figure img') ||
  220. container.querySelector('img');
  221. if (!img) return;
  222.  
  223. const post = container.closest('shreddit-post');
  224. const fullUrl = getFullImageUrl(post, container, img);
  225. if (!fullUrl) return;
  226.  
  227. container.dataset.singleDone = 'true';
  228.  
  229. if (img.parentElement && img.parentElement.classList.contains('reddit-single-img-link')) return;
  230.  
  231. const link = document.createElement('a');
  232. link.className = 'reddit-single-img-link';
  233. link.href = fullUrl;
  234. link.target = '_blank';
  235. link.title = 'Открыть оригинал';
  236.  
  237. link.addEventListener('click', (e) => e.stopPropagation());
  238. link.addEventListener('auxclick', (e) => e.stopPropagation());
  239.  
  240. img.parentNode.insertBefore(link, img);
  241. link.appendChild(img);
  242. });
  243. }
  244.  
  245. function checkAllPosts() {
  246. document.querySelectorAll('gallery-carousel').forEach(processGallery);
  247. processSingleImages();
  248. }
  249.  
  250. // Предотвращаем появление значка кругового автоскролла при клике колёсиком по картинке
  251. document.addEventListener('mousedown', (e) => {
  252. if (e.button === 1 && e.target.closest('div[slot="post-media-container"] img')) {
  253. e.stopPropagation();
  254. }
  255. }, true);
  256.  
  257. const observer = new MutationObserver(() => {
  258. checkAllPosts();
  259. });
  260.  
  261. observer.observe(document.body, { childList: true, subtree: true });
  262. checkAllPosts();
  263. })();
Advertisement
Add Comment
Please, Sign In to add comment