Reddit CSS

New Design with new functionalities

目前為 2023-12-07 提交的版本,檢視 最新版本

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