Reddit CSS

New Design with new functionalities

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

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