Reddit CSS

New Design with new functionalities

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

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