Remove Promotion Element at Reddit Site

Removes the promotion element from the page

目前為 2024-11-23 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Remove Promotion Element at Reddit Site
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.3
  5. // @description Removes the promotion element from the page
  6. // @author aspen138
  7. // @match *://www.reddit.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12.  
  13.  
  14.  
  15.  
  16. (function() {
  17. 'use strict';
  18.  
  19. // Wait for the page to load fully
  20. window.addEventListener('load', function() {
  21. // Select the promotion element using multiple possible classes or tags
  22. const promoSelectors = [
  23. 'a.w-100.block.h-100.cursor-pointer',
  24. 'shreddit-ad-post.promotedlink',
  25. 'shreddit-dynamic-ad-link',
  26. 'shreddit-comments-page-ad.promotedlink' // Added new selector
  27. ];
  28.  
  29. // Function to remove elements matching selectors
  30. function removePromoElements() {
  31. promoSelectors.forEach(selector => {
  32. const promoElements = document.querySelectorAll(selector);
  33. promoElements.forEach(element => {
  34. element.remove();
  35. console.log('Promotion element removed:', selector);
  36. });
  37. });
  38. }
  39.  
  40. // Function to hide elements with a specific rel attribute value
  41. function hideElementsWithRel() {
  42. // Select all links on the page
  43. const links = document.querySelectorAll('a');
  44.  
  45. // Iterate through all links
  46. links.forEach(link => {
  47. // Check if the rel attribute matches "noopener nofollow sponsored"
  48. if (link.getAttribute('rel') === "noopener nofollow sponsored") {
  49. // Hide the element
  50. link.style.display = 'none';
  51. console.log('Link with rel "noopener nofollow sponsored" hidden');
  52. }
  53. });
  54. }
  55.  
  56. // Initial run of both functions
  57. removePromoElements();
  58. hideElementsWithRel();
  59.  
  60. // Optional: Set up a MutationObserver to handle dynamically loaded content
  61. const observer = new MutationObserver(function(mutations) {
  62. removePromoElements();
  63. hideElementsWithRel();
  64. });
  65.  
  66. // Start observing the document with the configured parameters
  67. observer.observe(document.body, {
  68. childList: true,
  69. subtree: true
  70. });
  71. });
  72. })();