Reddit CSS

New Design with new functionalities

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

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