Reddit Sidebar Cleaner + Content Expander

Removes Reddit's right sidebar & expands main content - The perfect companion for Reddit Multi-Column

  1. // ==UserScript==
  2. // @name Reddit Sidebar Cleaner + Content Expander
  3. // @namespace https://gf.qytechs.cn/en/scripts/531104-reddit-sidebar-cleaner-content-expander
  4. // @version 1.3
  5. // @description Removes Reddit's right sidebar & expands main content - The perfect companion for Reddit Multi-Column
  6. // @match *://*.reddit.com/*
  7. // @icon https://i.ibb.co/6cH6j4d2/Reddit-Sidebar-Cleaner-Content-Expander.png
  8. // @grant GM_addStyle
  9. // @run-at document-start
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // CSS: Hide sidebar elements on non-comment pages via a class on <html>
  17. const sidebarCSS = `
  18. html:not(.reddit-comments) #right-sidebar-container,
  19. html:not(.reddit-comments) .right-sidebar,
  20. html:not(.reddit-comments) [id^="right-sidebar"],
  21. html:not(.reddit-comments) [class*="sidebar-container"] {
  22. display: none !important;
  23. width: 0 !important;
  24. visibility: hidden !important;
  25. opacity: 0 !important;
  26. transition: none !important;
  27. }
  28. .main-container,
  29. [role="main"] {
  30. grid-template-columns: 1fr !important;
  31. margin-right: 0 !important;
  32. padding-right: 0 !important;
  33. }
  34. `;
  35. GM_addStyle(sidebarCSS);
  36.  
  37. // Immediately update <html> with a flag if on a comment page.
  38. const updatePageClass = () => {
  39. if (window.location.pathname.includes('/comments/')) {
  40. document.documentElement.classList.add('reddit-comments');
  41. } else {
  42. document.documentElement.classList.remove('reddit-comments');
  43. }
  44. };
  45. updatePageClass();
  46.  
  47. // Intercept SPA navigation by patching history methods.
  48. const _pushState = history.pushState;
  49. history.pushState = function() {
  50. _pushState.apply(history, arguments);
  51. updatePageClass();
  52. };
  53. const _replaceState = history.replaceState;
  54. history.replaceState = function() {
  55. _replaceState.apply(history, arguments);
  56. updatePageClass();
  57. };
  58. window.addEventListener('popstate', updatePageClass);
  59.  
  60. // MutationObserver: If the extension injects sidebar elements later, hide them immediately.
  61. const observer = new MutationObserver(mutations => {
  62. mutations.forEach(mutation => {
  63. mutation.addedNodes.forEach(node => {
  64. if (node.nodeType === Node.ELEMENT_NODE) {
  65. // Check if the added node itself is a sidebar element.
  66. if (node.matches && node.matches('#right-sidebar-container, .right-sidebar, [id^="right-sidebar"], [class*="sidebar-container"]')) {
  67. node.style.display = 'none';
  68. }
  69. // Also check for any matching children within the added node.
  70. node.querySelectorAll('#right-sidebar-container, .right-sidebar, [id^="right-sidebar"], [class*="sidebar-container"]').forEach(el => {
  71. el.style.display = 'none';
  72. });
  73. }
  74. });
  75. });
  76. });
  77. observer.observe(document.documentElement, { childList: true, subtree: true });
  78. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址