Reddit CSS

New Design with new functionalities

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

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