Block Ads

Block ads integrated into webpages via iframes, CSS, JavaScript, Bootstrap, and other tools.

  1. // ==UserScript==
  2. // @name Block Ads
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Block ads integrated into webpages via iframes, CSS, JavaScript, Bootstrap, and other tools.
  6. // @author iamnobody
  7. // @license MIT
  8. // @match *://*/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to remove elements matching a given selector
  16. function removeElements(selector) {
  17. const elements = document.querySelectorAll(selector);
  18. elements.forEach(element => {
  19. element.remove(); // Remove the element from the DOM
  20. });
  21. }
  22.  
  23. // Remove ads integrated via iframes
  24. removeElements('iframe[src*="ad"], iframe[src*="ads"], iframe[src*="banner"]');
  25.  
  26. // Remove elements with ad-related class names
  27. removeElements('.ad, .ads, .advert, .advertisement, .banner, .promo');
  28.  
  29. // Remove elements with ad-related IDs
  30. removeElements('[id*="ad"], [id*="ads"], [id*="banner"], [id*="promo"], [id*="sponsor"]');
  31.  
  32. // Remove elements with ad-related CSS styles
  33. removeElements('[style*="ad"], [style*="ads"], [style*="banner"], [style*="promo"], [style*="sponsor"]');
  34.  
  35. // Remove elements with ad-related JavaScript events
  36. removeElements('[onmouseover*="ad"], [onmouseout*="ad"], [onmouseenter*="ad"], [onmouseleave*="ad"], [onload*="ad"], [onclick*="ad"], [onfocus*="ad"], [onblur*="ad"], [onkeypress*="ad"], [onkeydown*="ad"], [onkeyup*="ad"], [onsubmit*="ad"], [onreset*="ad"], [onchange*="ad"], [ondblclick*="ad"], [onmousedown*="ad"], [onmouseup*="ad"]');
  37.  
  38. // Remove elements with ad-related Bootstrap classes
  39. removeElements('.navbar-ad, .sidebar-ad, .footer-ad, .jumbotron-ad');
  40.  
  41. // Remove elements with ad-related attributes
  42. removeElements('[data-ad], [data-ads], [data-banner], [data-promo], [data-sponsor]');
  43.  
  44. // Function to remove ad-related elements
  45. function removeAds() {
  46. // Remove iframes
  47. document.querySelectorAll('iframe').forEach(iframe => {
  48. iframe.remove();
  49. });
  50.  
  51. // Remove script tags
  52. document.querySelectorAll('script').forEach(script => {
  53. script.remove();
  54. });
  55.  
  56. // Remove elements by class or ID
  57. const adClasses = ['ad', 'advertisement', 'ad-container'];
  58. adClasses.forEach(className => {
  59. document.querySelectorAll(`.${className}`).forEach(adElement => {
  60. adElement.remove();
  61. });
  62. });
  63.  
  64. // Remove specific elements by ID
  65. const adIds = ['ad-banner', 'ad-sidebar'];
  66. adIds.forEach(id => {
  67. const adElement = document.getElementById(id);
  68. if (adElement) {
  69. adElement.remove();
  70. }
  71. });
  72. }
  73.  
  74. // Run the ad blocking function when the page is fully loaded
  75. window.addEventListener('load', function() {
  76. removeAds();
  77. });
  78. })();