Reddit CSS

New Design with new functionalities

当前为 2023-08-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit CSS
  3. // @namespace https://www.reddit.com
  4. // @version 5.6
  5. // @description New Design with new functionalities
  6. // @author Agreasyforkuser
  7. // @match https://*.reddit.com/*
  8. // @icon https://www.redditstatic.com/desktop2x/img/favicon/android-icon-192x192.png
  9. // @license MIT
  10. // @grant GM_addStyle
  11. // ==/UserScript==
  12.  
  13.  
  14. /////////////////// sort by new ////////////////////////////////////////////////////////////////////////
  15.  
  16. 'use strict';
  17. const re = /https?:\/\/(?:www\.|old\.|new\.)?reddit\.com/i;
  18. for (var i=0, l=document.links.length; i<l; i++) {
  19. if (re.test(document.links[i].href)) {
  20. var path = document.links[i].pathname;
  21. if (path === '/' || path.startsWith('/r/')) {
  22. var pathlen = path.split('/').length - 1 - (path.endsWith('/') ? 1 : 0);
  23. if ((pathlen <= 2) && (document.links[i].closest('.tabmenu') === null)) {
  24. document.links[i].href += path.endsWith('/') ? 'new/' : '/new/';
  25. }
  26. }
  27. }
  28. }
  29. //////////////////////////////// over 18 button ///////////////////////////////////////////////////////
  30.  
  31. const button = document.querySelector('button.c-btn.c-btn-primary[type="submit"][name="over18"][value="yes"]');
  32. if (button) {button.click();}
  33.  
  34. ///////////////////////////////////////////////////////////////////////////////////////////////////
  35. (function() {
  36. 'use strict';
  37.  
  38. // Define the CSS to enlarge the thumbnails and modify the styling
  39. var customCSS = `
  40.  
  41.  
  42. .email-collection-banner, .email-verification-tooltip, #eu-cookie-policy {display:none !important}
  43.  
  44. .infobar.listingsignupbar, .infobar.commentsignupbar {display: none !important;}
  45. .infobar, .timeout-infobar {border:none}
  46. .help-hoverable {display:none}
  47.  
  48. /* Video Controls for Mobile */
  49. video::-webkit-media-controls-panel {background: none; opacity:.7}
  50. video::-webkit-media-controls-overlay-play-button {opacity: 0}
  51.  
  52. /* Flair Labels */
  53. .flairrichtext {border-radius: 0px;border:none}
  54. .linkflairlabel {border-radius: 0px;border:none}
  55. .stamp {background: white;border-radius: 0;padding:0}
  56.  
  57.  
  58.  
  59.  
  60. /* Buttons */
  61. button, .preftable select, input[type="submit"] {border-radius:0}
  62.  
  63. /* Subreddit Name */
  64. body.with-listing-chooser #header .pagename {position:inherit; color: white}
  65. .pagename a {color: black; margin-left:4px; margin-right:4px}
  66. .pagename {background-color:white; font-variant: normal; border-radius:0px; margin: 0;}
  67.  
  68. /* Increase the width and height of the thumbnail */
  69. .thumbnail img {
  70. width: 180px !important;
  71. height: auto !important;
  72. }
  73.  
  74. /* Adjust the size of the thumbnail container */
  75. .thumbnail {
  76. width: 180px !important;
  77. height: auto !important;
  78. position: relative;
  79. margin-bottom: 0;
  80. }
  81.  
  82. /* Gallery Posts */
  83. .media-gallery img {border: 1px solid black}
  84.  
  85. /* no margin left for expanded media */
  86. .entry {margin-left:0}
  87.  
  88. /* size videos correctly */
  89. .no-constraints-when-pinned {min-width:0 !important; min-height: 0 !important;}
  90.  
  91. /* Videoplayer Background */
  92. .reddit-video-player-root {background: #0000;}
  93.  
  94. /* Video Duration Info */
  95. .duration-overlay {font-size: 10pt; font-weight: bold;}
  96. .duration-overlay {border-top-left-radius: 50px;border-top-right-radius: 50px;}
  97.  
  98.  
  99. /* OP indicator */
  100. .tagline .submitter, .search-result-meta .submitter {color: #228822}
  101.  
  102. /* sort menu*/
  103. .menuarea {border:none}
  104. .dropdown.lightdrop .selected {text-decoration: none}
  105.  
  106. /* Post */
  107. .thing .title {font-weight: normal;color: #000; ;margin-bottom: 15px;}
  108. .subreddit {font-weight: bold;font-size: larger;color: #000;}
  109. .link .flat-list {font-size:larger}
  110. .link.last-clicked {border: 3px dotted red;}
  111.  
  112. /* Text Posts Background*/
  113. .link .usertext-body .md {background: none; border: 1px dotted gray}
  114.  
  115. /* Post Details */
  116. .entry .buttons li a {color:#000000c7; font-weight:normal}
  117.  
  118. .entry .buttons li {background: #0000000d}
  119. .entry .buttons li:hover {opacity:1 !important} //revert graying-out function on hover
  120.  
  121. .tagline {color: #8880; margin-bottom: 15px;}
  122. .tagline .live-timestamp {color: #369}
  123. .domain {display: none;}
  124. .reportbtn {display:none}
  125. .post-crosspost-button {display:none}
  126. .post-sharing-button {display:none}
  127. .buttons .give-gold.gold-give-gold {display: none}
  128. .hide-button {display:none !important}
  129. .expando-button {opacity:0.5;float:right}
  130.  
  131.  
  132. /* Header Font Size and Icon */
  133. #header-bottom-left {font-size: large;}
  134. #header-img.default-header:not(:hover) {opacity: 0}
  135.  
  136. /* Upper Bars */
  137. #sr-header-area:not(:hover) {background: none !important;border-bottom: none; opacity:0.3 }
  138. #sr-header-area .redesign-beta-optin {display:none}
  139. #sr-more-link {background-color: #cee3f8 }
  140.  
  141. /* Hide Username */
  142. .user a:not(:hover) {opacity: 0;}
  143.  
  144.  
  145.  
  146. .pref-lang {font-weight: normal}
  147. #header-bottom-right:not(:hover) {background: none; border-radius:0}
  148. #header-bottom-right {background: white; border-radius:0}
  149. #header-bottom-right {font-size: larger;}
  150. #header {border:none}
  151.  
  152.  
  153. .separator {color: transparent;}
  154. #header-bottom-left {margin-top: -10px;}
  155. a[href="https://old.reddit.com/wiki/"].choice {display: none;}
  156. .tabmenu li a {background:none;opacity:0.6;font-weight: normal}
  157. .tabmenu li a:hover {background:white}
  158. .tabmenu li.selected a {opacity:1;font-weight: bold;font-size: x-large; background-color: white; border: none}
  159. .tabmenu li.selected a {text-transform: uppercase}
  160.  
  161.  
  162.  
  163.  
  164. /* Sidebar */
  165. .sidecontentbox .content {border: none; background: none}
  166. .sidebox .spacer {display: none}
  167. .premium-banner {display: none}
  168. .giftgold {display: none}
  169. .titlebox .bottom {border:none}
  170. #searchexpando, .linkinfo, .linkinfo .shortlink input {border: none}
  171. .morelink .nub {display: none}
  172. .morelink {border: none; background: #eff7ff;background-image:none}
  173. .toggle .option {border: none; border-radius:0}
  174. .c-btn-primary {border: none; border-radius: 0;}
  175. .subscription-box:not(:hover) {opacity: 0}
  176.  
  177.  
  178.  
  179. /* Space between Posts */
  180. .link {margin-bottom: 0px;}
  181.  
  182. /* Expanded Post Margins */
  183. .expando {margin: 0}
  184.  
  185. /* Gallery Buttons */
  186. .media-gallery .gallery-nav-bar {font-size:large}
  187.  
  188.  
  189. /* Rank / Scores */
  190. .link .rank {display: none}
  191. .arrow.up {display: none}
  192. .arrow.down {display: none}
  193.  
  194. /* Comment Page */
  195. .panestack-title { padding-bottom: 0px; border-bottom: none;}
  196. .commentarea .menuarea {font-size: large;}
  197. .comment .author {font-size: larger;float:left}
  198. .comment .expand {font-size: large;float:left; opacity:1;margin: 0 !important;padding: 0 !important; opacity:0}
  199. .comment .expand {width:6px}
  200. .comment.collapsed .expand { opacity:1;width:auto}
  201. .comment .midcol {z-index:9999}
  202.  
  203. .commentarea > .usertext:not(:hover) {opacity:0.2; height: 18px}
  204. .commentarea .entry .buttons:not(:hover) {opacity:0;}
  205. .comments-page #siteTable .thing {display: flex !important;border: none;}
  206. .comment .tagline {color: #00000069 ; margin-bottom: 15px; text-align:left}
  207. .comment .child {border-left: dotted 3px #000000}
  208. .comment .score {font-size: larger}
  209. .comments-page .arrow.up {display:block}
  210. .comments-page .arrow.down {display:block}
  211. .pinnable-content.pinned {background-color: #FFFFFFF7 !important;box-shadow: none !important}
  212. .reddiquette {display: none}
  213.  
  214. /* Bottom Page */
  215. .footer {display:none}
  216. .footer-parent {opacity:0}
  217. .debuginfo {display:none}
  218. .bottommenu {opacity:0}
  219.  
  220. /* promoted posts/ads */
  221. .link.promotedlink.promoted, .link.promotedlink.external {display:none !important}
  222.  
  223. /* submissions */
  224. .formtabs-content .infobar {border:none}
  225. .content.submit .info-notice {display: none;}
  226. #items-required {display: none;}
  227.  
  228.  
  229.  
  230. /* Navbar */
  231. .nav-buttons {
  232. display:table;
  233. bottom: 0 !important;
  234. right: 0 !important;
  235. text-align: center !important;
  236. border: 3px solid;
  237. border-color: #cee3f8;
  238. border-top-left-radius: 15px;
  239. background-color: #cee3f8;
  240. font-size: larger;
  241. z-index: 9998 !important;
  242. }
  243. .nextprev a {background:none !important}
  244.  
  245.  
  246.  
  247. `;
  248.  
  249. // Add the custom CSS to the page
  250. GM_addStyle(customCSS);
  251.  
  252. ////////////////////// Remove thumbnails from posts without thumbnails////////////////////////
  253.  
  254. function removePostsWithoutImages() {
  255. var posts = document.querySelectorAll('.thumbnail');
  256. for (var i = 0; i < posts.length; i++) {
  257. if (!posts[i].querySelector('img')) {
  258. posts[i].parentNode.removeChild(posts[i]);
  259. }
  260. }
  261. }
  262. setInterval(removePostsWithoutImages, 1000);
  263. })();
  264.  
  265.  
  266. ////////////////////////////////////// comments page //////////////////////////////////
  267.  
  268. var commentpage = /https:\/\/.*\.reddit\.com\/.*\/comments\/.*/;
  269. if (commentpage.test(window.location.href)) {
  270. GM_addStyle(`.reportbtn {display:block}
  271. .post-crosspost-button {display:block}
  272. a.comments {display:none}
  273. /* .buttons .give-gold.gold-give-gold {display:block} */
  274. .expando-button {opacity: 1 }
  275. .hide-button {display:block !important}
  276.  
  277. a.embed-comment {display: none}
  278. /* a.bylink {display: none} */
  279.  
  280.  
  281. .thumbnail,.thumbnail img {width: 100px !important;height: 100px !important;}
  282. #sr-header-area {display:none}
  283.  
  284. /* without this voting comments on mobile is impossible */
  285. .entry {margin-left:4px}
  286.  
  287. `
  288. );
  289.  
  290. ////////////////////////////////// Download Button ////////////////////////////////////
  291.  
  292.  
  293. 'use strict';
  294.  
  295. function downloadVideo() {
  296. var postUrl = window.location.href;
  297. var baseUrl = 'https://rapidsave.com/info?url=';
  298. var downloadUrl = baseUrl + encodeURIComponent(postUrl);
  299. window.open(downloadUrl, '_blank');
  300. }
  301.  
  302. function createDownloadButton() {
  303. var listItem = document.createElement('li');
  304. listItem.classList.add('reddit-video-download-button');
  305.  
  306. var button = document.createElement('button');
  307. button.innerHTML = 'Download';
  308. button.style.color = '#000000c7'; //same as .entry .buttons li a
  309. button.style.background = 'none';
  310. button.style.border = 'none';
  311. button.style.fontSize = 'inherit';
  312. // button.style.borderRadius = '10px';
  313. // button.style.fontWeight = 'bold';
  314. button.onclick = downloadVideo;
  315.  
  316. listItem.appendChild(button);
  317.  
  318. var buttonsList = document.querySelector('ul.buttons');
  319. if (buttonsList) {
  320. buttonsList.appendChild(listItem);
  321. }
  322. }
  323.  
  324. createDownloadButton();
  325.  
  326.  
  327.  
  328. /////////////////////add profile pictures next to comments ///////////////////////
  329.  
  330. const addAvatars = async (root = document) => {
  331. Array.from(root.querySelectorAll('.thing:not(.morechildren)')).forEach(async (thing) => {
  332. if (!thing) return;
  333. if (thing.hasAttribute('data-reddit-profile-picture')) return;
  334. const img = document.createElement('img');
  335. img.classList.add('reddit-profile-picture');
  336. img.style.height = '22px';
  337. img.style.width = '22px';
  338. img.style.float = 'left';
  339. img.style.margin = '0px';
  340. thing.insertBefore(img, thing.querySelector('.entry'));
  341. thing.setAttribute('data-reddit-profile-picture', 1);
  342. if (!thing.id) return;
  343. const authorElement = thing.querySelector('.author');
  344. if (authorElement && authorElement.href) {
  345. const xhr = new XMLHttpRequest();
  346. xhr.open('GET', `${authorElement.href}/about.json`);
  347. xhr.addEventListener('load', async () => {
  348. if (xhr.status === 200) {
  349. try {
  350. const profile = JSON.parse(xhr.responseText).data;
  351. const ta = document.createElement('textarea');
  352. ta.innerHTML = profile.icon_img;
  353. img.src = ta.value;
  354. } catch (error) {
  355. // Error parsing JSON or extracting URL
  356. console.error('Error parsing JSON or extracting URL:', error);
  357. removeAvatar(img);
  358. }
  359. } else {
  360. // Non-200 status, handle error
  361. console.error('Error fetching user data:', xhr.status, xhr.statusText);
  362. removeAvatar(img);
  363. }
  364. });
  365. xhr.addEventListener('error', () => {
  366. // Network error
  367. console.error('Network error while fetching user data.');
  368. removeAvatar(img);
  369. });
  370. xhr.send();
  371. }
  372. });
  373. };
  374.  
  375. const removeAvatar = (imgElement) => {
  376. if (imgElement && imgElement.parentNode) {
  377. imgElement.parentNode.removeChild(imgElement);
  378. }
  379. };
  380.  
  381. addAvatars();
  382.  
  383. const mo = new MutationObserver((muts) => {
  384. muts.forEach((mut) => {
  385. Array.from(mut.addedNodes).forEach((node) => {
  386. if (node instanceof HTMLElement) {
  387. addAvatars();
  388. }
  389. });
  390. });
  391. });
  392. mo.observe(document.body, { childList: true, subtree: true });
  393. };
  394.  
  395. ////////////////////////////// userpage ///////////////////////////////////////////////////////////////////
  396.  
  397. var userpage = /https:\/\/.*\.reddit\.com\/user\/.*/;
  398. if (userpage.test(window.location.href)) {
  399.  
  400. GM_addStyle(`.comment, .content .details {border-bottom: 2px solid #5f99cf !important}`);
  401.  
  402. const addProfilePictures = async (root = document) => {
  403. Array.from(root.querySelectorAll('.pagename')).forEach(async (pagename) => {
  404. if (!pagename) return;
  405. if (pagename.hasAttribute('data-reddit-profile-picture')) return;
  406.  
  407. const username = pagename.textContent.trim();
  408. if (!username) return;
  409.  
  410. const img = document.createElement('img');
  411. img.classList.add('reddit-profile-picture');
  412. img.style.height = '35px';
  413. img.style.width = '35px';
  414. img.style.verticalAlign = 'middle';
  415.  
  416.  
  417. pagename.parentNode.insertBefore(img, pagename.nextSibling);
  418.  
  419. pagename.setAttribute('data-reddit-profile-picture', 1);
  420.  
  421. const xhr = new XMLHttpRequest();
  422. xhr.open('GET', `https://www.reddit.com/user/${username}/about.json`);
  423. xhr.addEventListener('load', async () => {
  424. const profile = JSON.parse(xhr.responseText).data;
  425. const ta = document.createElement('textarea');
  426. ta.innerHTML = profile.icon_img;
  427. img.src = ta.value;
  428. });
  429. xhr.send();
  430. });
  431. };
  432.  
  433. addProfilePictures();
  434.  
  435. const mo = new MutationObserver((muts) => {
  436. muts.forEach((mut) => {
  437. Array.from(mut.addedNodes).forEach((node) => {
  438. if (node instanceof HTMLElement) {
  439. addProfilePictures(node);
  440. }
  441. });
  442. });
  443. });
  444.  
  445. mo.observe(document.body, { childList: true, subtree: true });
  446. };
  447.  
  448.  
  449. //////////////////////////////////////////// red numbers if score is less than one //////////////////////////////////////
  450.  
  451. if (userpage.test(window.location.href) || commentpage.test(window.location.href)) {
  452. (function() {
  453. 'use strict';
  454.  
  455. // Function to check if the text starts with a "-" or "0"
  456. function startsWithNegativeSymbolOrZero(text) {
  457. return text.trim().startsWith('-') || text.trim().startsWith('0');
  458. }
  459.  
  460. // Get all elements with class "score likes"
  461. var scoreElements = document.querySelectorAll('.score');
  462.  
  463. // Iterate over each score element using forEach
  464. scoreElements.forEach(function(scoreElement) {
  465. var scoreText = scoreElement.textContent.trim();
  466.  
  467. // Check if the score starts with a "-" or "0"
  468. if (startsWithNegativeSymbolOrZero(scoreText)) {
  469. // Change the color to red
  470. scoreElement.style.color = 'red';
  471. scoreElement.style.fontWeight = 'bold';
  472. } else if (parseFloat(scoreText) > 1) {
  473. // Change the color to green
  474. scoreElement.style.color = 'green';
  475. scoreElement.style.fontWeight = 'bold';
  476. }
  477. });
  478. })();
  479. }
  480.  
  481. /////////////////////////grey out comment-buttons if no comments were written /////////////////////////////////
  482.  
  483.  
  484. if (!commentpage.test(window.location.href)) {
  485.  
  486. 'use strict';
  487.  
  488. const removeCommentsCount = element => {
  489. const text = element.textContent.trim();
  490. if (!isNaN(parseInt(text.charAt(0), 10))) return;
  491. element.closest('.entry .buttons li').style.opacity = '0.2';
  492. };
  493.  
  494. const commentElements = document.querySelectorAll('a.comments');
  495. commentElements.forEach(removeCommentsCount);
  496.  
  497. const observer = new MutationObserver(mutationsList => {
  498. for (const mutation of mutationsList) {
  499. if (mutation.type === 'childList') {
  500. const newCommentElements = mutation.addedNodes;
  501. newCommentElements.forEach(node => {
  502. if (node instanceof HTMLElement) {
  503. const element = node.querySelector('a.comments');
  504. if (element) removeCommentsCount(element);
  505. }
  506. });
  507. }
  508. }
  509.  
  510. });
  511.  
  512. observer.observe(document.body, { childList: true, subtree: true });
  513. };
  514.  
  515. /////////////////////////////////////////////////Thumbnail Click Functionality //////////////////
  516.  
  517. // Custom CSS for the clicked thumbnail
  518. const customCSS = `
  519. /* Makes Clicking Easier */
  520. .thumbnail {z-index:99}
  521.  
  522. .thumbnail.clicked {
  523. width: 80px !important;
  524. height: 80px !important;
  525. border-radius: 15px;
  526. }
  527.  
  528. .thumbnail.clicked img {
  529. width: 80px !important;
  530. height: 80px !important;
  531. }
  532.  
  533. .thing.clicked .arrow.up {display: block}
  534. .thing.clicked .arrow.down {display:block}
  535. `;
  536.  
  537. function expandPostOnElementClick(element) {
  538. const expandoButton = element.closest('.thing').querySelector('.expando-button');
  539.  
  540. if (expandoButton) {
  541. expandoButton.click();
  542. }
  543. }
  544.  
  545. function handleElementClick(event) {
  546. event.stopPropagation();
  547. event.preventDefault();
  548.  
  549. const element = event.currentTarget;
  550. const thumbnail = element.closest('.thumbnail');
  551. const thing = element.closest('.thing');
  552.  
  553. // Check if the thumbnail has the 'clicked' class
  554. const isClicked = thumbnail.classList.contains('clicked');
  555.  
  556. if (isClicked) {
  557. // Remove the 'clicked' class to revert the changes
  558. thumbnail.classList.remove('clicked');
  559. thing.classList.remove('clicked');
  560. } else {
  561. // Add 'clicked' class to the thumbnail
  562. thumbnail.classList.add('clicked');
  563. thing.classList.add('clicked');
  564. }
  565.  
  566. expandPostOnElementClick(element);
  567. }
  568.  
  569. function attachClickListenersToElements() {
  570. const elements = document.querySelectorAll('.thumbnail, img[src*="external-preview.redd.it"]');
  571.  
  572. elements.forEach((element) => {
  573. element.addEventListener('click', handleElementClick);
  574. });
  575. }
  576.  
  577. // Add custom CSS styles
  578. GM_addStyle(customCSS);
  579.  
  580. // Attach click listeners to elements initially
  581. attachClickListenersToElements();
  582.  
  583. // Create a MutationObserver to monitor the page for changes
  584. const observer = new MutationObserver(() => {
  585. // Attach click listeners to elements whenever new content is added to the page
  586. attachClickListenersToElements();
  587. });
  588.  
  589. // Start observing the document for changes
  590. observer.observe(document, { childList: true, subtree: true });
  591.  
  592. /////////////////////Toggle Nav-Bar On Scroll /////////////////////////////////////////////////////////////
  593.  
  594.  
  595. 'use strict';
  596.  
  597. var navButtons = document.getElementsByClassName('nav-buttons');
  598. if (navButtons.length > 0) {
  599. var nav = navButtons[0];
  600. nav.style.position = 'fixed';
  601. }
  602.  
  603. //var headerHeight = header.offsetHeight;
  604. var lastScrollTop = 0;
  605.  
  606.  
  607. window.addEventListener('scroll', function() {
  608. var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
  609. var windowHeight = window.innerHeight;
  610. var documentHeight = document.documentElement.scrollHeight;
  611.  
  612. if (scrollTop === 0) {
  613. // Reached the top of the page
  614. nav.style.opacity = '1';
  615. nav.style.pointerEvents = 'auto';
  616. } else {
  617. // Scrolling up
  618. nav.style.opacity = '.8';
  619. nav.style.pointerEvents = 'auto';
  620. }
  621.  
  622. if (scrollTop + windowHeight + 10 >= documentHeight) {
  623. // Reached the end of the page
  624. nav.style.opacity = '1';
  625. nav.style.pointerEvents = 'auto';
  626. } else {
  627. if (scrollTop > lastScrollTop) {
  628. // Scrolling down
  629. nav.style.opacity = '0';
  630. nav.style.pointerEvents = 'none';
  631. }
  632. }
  633.  
  634. lastScrollTop = scrollTop;
  635. });
  636.  
  637.  
  638. /////////////////////////////////////// remove some tabs /////////////////////////////////////////////////
  639.  
  640. (function() {
  641. 'use strict';
  642.  
  643. // Function to remove the elements containing the text "gilded" or "best" from .tabmenu
  644. function removeGildedAndBestTabs() {
  645. const tabmenu = document.querySelector('.tabmenu');
  646. if (!tabmenu) return;
  647.  
  648. const tabs = tabmenu.querySelectorAll('a');
  649. for (let i = 0; i < tabs.length; i++) {
  650. const tab = tabs[i];
  651. const tabText = tab.textContent.toLowerCase();
  652. if (tabText.includes('gilded') || tabText.includes('best')) {
  653. tab.remove();
  654. }
  655. }
  656. }
  657.  
  658. // Call the function to remove the "gilded" and "best" tabs after the page has loaded
  659. window.addEventListener('load', removeGildedAndBestTabs);
  660. })();
  661.  
  662. ////////////////////////////////////// Subreddit Icon next to Subreddit name /////////////////////////
  663.  
  664. (function() {
  665. 'use strict';
  666.  
  667. function setSubredditIcon() {
  668. const pagenameLink = document.querySelector('.pagename');
  669. const subredditIcon = document.createElement('img');
  670. subredditIcon.style.verticalAlign = 'middle';
  671. subredditIcon.style.width = 'auto';
  672. subredditIcon.style.height = '35px';
  673.  
  674. const srName = getSrName();
  675.  
  676.  
  677. const srDataUrl = `https://www.reddit.com/r/${srName}/about.json`;
  678. fetch(srDataUrl)
  679. .then(response => response.json())
  680. .then(data => {
  681. const communityIcon = cleanUpCommunityIcon(data.data.community_icon);
  682. const iconUrl = communityIcon || data.data.icon_img || data.data.header_img;
  683. if (!iconUrl || iconUrl.length === 0)
  684. {
  685. return;
  686. }
  687. subredditIcon.src = iconUrl;
  688. pagenameLink.parentNode.insertBefore(subredditIcon, pagenameLink.nextSibling);
  689. })
  690.  
  691. }
  692.  
  693. function getSrName() {
  694. const srNameRegex = /https:[/][/](www|old|new)[.]reddit[.]com[/]r[/](\w+)/g;
  695. const match = srNameRegex.exec(document.location.href);
  696.  
  697. return match[2];
  698. }
  699.  
  700. function cleanUpCommunityIcon(url) {
  701. if (!url || url.length === 0) {
  702. return url;
  703. }
  704. function htmlDecode(input) {
  705. const doc = new DOMParser().parseFromString(input, 'text/html');
  706. return doc.documentElement.textContent;
  707. }
  708. const decodedUrl = htmlDecode(url);
  709. return decodedUrl;
  710. }
  711.  
  712. setSubredditIcon();
  713. })();
  714.  
  715.  
  716.  
  717.