Reddit CSS

New Design with new functionalities

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

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