Reddit Enhancement

Removes the promotion element and the "We had a server error..." banner

当前为 2024-12-24 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit Enhancement
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.5
  5. // @description Removes the promotion element and the "We had a server error..." banner
  6. // @author aspen138
  7. // @match *://www.reddit.com/*
  8. // @icon data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAQlBMVEVHcEz/RQD/RQD/QgD/RQD/RQD/RQD/RQD/RQD/RQD/////MgD/OgD/s5//z8P/a0T/5d3/VyH/iGr/qJP/mYD/+vcCA1U1AAAACnRSTlMAJP//y5WUn+ElsgVe0gAAAJFJREFUGJVtT1sOwyAMy0JpIa/C2t3/qjNQaT+zkMAmD5sIqLkwl1zpwcEPjsW3ScxMefv9m7u3WVNXdXJ9Q+BKGYRN+62miXmnMvg7WotT8SzE6ZQHHzkTL+HuIv2SKRTWkHCRC5eiJWOCSJvnNgzFWrtQ4iGuY+0wZt0jHFuWeVhPpmpwsf0PR/TaR/x9xv8CYoYGnu4Mr1kAAAAASUVORK5CYII=
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13.  
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. // Searches through all Shadow DOM roots
  19. function deepQuerySelectorAll(selector) {
  20. const nodes = [];
  21. function searchInNode(node) {
  22. if (node.shadowRoot) {
  23. const matches = node.shadowRoot.querySelectorAll(selector);
  24. if (matches.length > 0) {
  25. nodes.push(...matches);
  26. }
  27. Array.from(node.shadowRoot.children).forEach(searchInNode);
  28. }
  29. Array.from(node.children).forEach(searchInNode);
  30. }
  31. searchInNode(document);
  32. return nodes;
  33. }
  34.  
  35. // Combined removal function for both error banners and promo elements
  36. function removeElements() {
  37. // Remove error banners
  38. const banners = deepQuerySelectorAll('div.banner.error');
  39. banners.forEach(banner => {
  40. banner.remove();
  41. console.log("Server Error Banner has been removed.");
  42. });
  43.  
  44. // Remove promotional elements
  45. const promoSelectors = [
  46. 'a.w-100.block.h-100.cursor-pointer',
  47. 'shreddit-ad-post.promotedlink',
  48. 'shreddit-dynamic-ad-link',
  49. 'shreddit-comments-page-ad.promotedlink'
  50. ];
  51.  
  52. promoSelectors.forEach(selector => {
  53. const promoElements = document.querySelectorAll(selector);
  54. promoElements.forEach(element => {
  55. element.remove();
  56. console.log('Promotion element removed:', selector);
  57. });
  58. });
  59.  
  60. // Hide elements with specific rel attribute
  61. const links = document.querySelectorAll('a');
  62. links.forEach(link => {
  63. if (link.getAttribute('rel') === "noopener nofollow sponsored") {
  64. link.style.display = 'none';
  65. console.log('Link with rel "noopener nofollow sponsored" hidden');
  66. }
  67. });
  68. }
  69.  
  70. // Single MutationObserver for all operations
  71. const observer = new MutationObserver(() => {
  72. removeElements();
  73. });
  74.  
  75. // Start observing changes in the document
  76. observer.observe(document, {
  77. childList: true,
  78. subtree: true
  79. });
  80.  
  81. // Wait for the page to load and perform initial cleanup
  82. if (document.readyState === 'loading') {
  83. window.addEventListener('load', removeElements);
  84. } else {
  85. removeElements();
  86. }
  87. })();