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