Wowhead Ad Blocker

Block ads on Wowhead.com using Adblock Plus rules(2024-07-18 Updated)

  1. // ==UserScript==
  2. // @name Wowhead Ad Blocker
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  5. // @description Block ads on Wowhead.com using Adblock Plus rules(2024-07-18 Updated)
  6. // @author Bing Ma
  7. // @match https://www.wowhead.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to remove elements by selector
  16. function removeElementsBySelector(selector) {
  17. let elements = document.querySelectorAll(selector);
  18. elements.forEach(element => element.remove());
  19. }
  20. // Function to hide elements by selector
  21. function hideElementsBySelector(selector) {
  22. let elements = document.querySelectorAll(selector);
  23. elements.forEach(element => {
  24. element.style.setProperty('display', 'none', 'important');
  25. });
  26. }
  27. // List of ad selectors to block (from your Adblock Plus rules)
  28. const adSelectors = [
  29. 'DIV[class="blocks"]',
  30. 'DIV[class="sidebar-wrapper"]',
  31. 'DIV[class="zaf-unit-wrapper"]',
  32. 'DIV[class="pb-stream"]'
  33. ];
  34.  
  35. // Run the ad blocker on page load
  36. window.addEventListener('load', () => {
  37. adSelectors.forEach(selector => {
  38. removeElementsBySelector(selector);
  39. hideElementsBySelector(selector);
  40. });
  41. });
  42.  
  43. // Run the ad blocker periodically to catch dynamically loaded ads
  44. setInterval(() => {
  45. adSelectors.forEach(selector => {
  46. removeElementsBySelector(selector);
  47. hideElementsBySelector(selector);
  48. });
  49. }, 1000);
  50.  
  51. // Mutation observer to catch ads added dynamically
  52. const observer = new MutationObserver((mutations) => {
  53. mutations.forEach((mutation) => {
  54. mutation.addedNodes.forEach((node) => {
  55. if (node.nodeType === 1) { // Ensure it's an element
  56. adSelectors.forEach(selector => {
  57. if (node.matches(selector) || node.querySelector(selector)) {
  58. node.remove();
  59. }
  60. });
  61. }
  62. });
  63. });
  64. });
  65.  
  66. // Start observing the document for added nodes
  67. observer.observe(document.body, { childList: true, subtree: true });
  68.  
  69. })();