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