Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. Identify the elements that represent ads on a webpage
- // In this example, we'll target elements with CSS classes that often indicate ads.
- const adClassNames = [
- 'ad',
- 'ad-banner',
- 'ad-container',
- // Add more class names as needed to cover different types of ads
- ];
- // 2. Define a function to remove ads
- function removeAds() {
- // Iterate through all elements in the DOM and check if they have ad-related class names
- const elements = document.getElementsByTagName('*');
- for (let i = 0; i < elements.length; i++) {
- const element = elements[i];
- for (let j = 0; j < adClassNames.length; j++) {
- if (element.classList.contains(adClassNames[j])) {
- element.remove();
- break; // Once an ad is removed, no need to check other class names for the same element
- }
- }
- }
- }
- // 3. Attach the removeAds function to relevant events, such as page load or DOM changes
- window.addEventListener('load', removeAds);
- // You can also listen for other events like DOMContentLoaded, scroll, or AJAX requests
- // and call removeAds() accordingly to handle dynamically loaded ads.
- // 4. (Optional) Create a button or trigger to manually remove ads
- const adBlockButton = document.createElement('button');
- adBlockButton.textContent = 'Toggle Ad Blocker';
- adBlockButton.addEventListener('click', removeAds);
- document.body.appendChild(adBlockButton);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement