Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login

POZE MRRRR'S FORUM (VIEW ONLY)

Nu sunteti logat.
Nou pe simpatie:
Ioanacatalina 25 ani
Femeie
25 ani
Salaj
cauta Barbat
25 - 61 ani
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides / [CHROME, TAMPERMONKEY] Block Banners in Webpages Moderat de TRaP, TonyTzu
Autor
Mesaj Pagini: 1
Mrrrr
AdMiN

Inregistrat: acum 19 ani
Postari: 2374
There is a div id="floatingBanner" on a website that is delayed for a few seconds after the page loads.

I colored commented code that is meant to be actual code in this color. Comments are normally colored in this color. Text in no specific color is default Tampermonkey userscript text, or active text in my script - the GM_addStyle line is the one I am using to block the banner.


// ==UserScript==
// @name         Block banner
// @namespace    Tampermonkey
// @version      v1.0
// @description  blocks a banner on your desired website
// @author       Mrrrr
// @match        https://WEBSITE GOES HERE/*
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // This works with additional @grant GM_addStyle
    GM_addStyle('#floatingBanner { display: none !important; }');

    // This works with no need to grant anything
    // document.getElementById('floatingBanner')?.remove();

    /* If the id doesn't exist, you can use the following approaches:

1. By Class Name
If it has a specific class (like <div class="ad-banner-overlay">):

    document.querySelector('.ad-banner-overlay')?.remove();

2. By Attribute (Matches part of a class, src, or data attribute)
Useful if the site uses dynamic or randomized class names, but part of it stays consistent:
- Target any div where the class contains the word "floating"

    document.querySelector('div[class*="floating"]')?.remove();
- Target an element with a specific custom data attribute
    document.querySelector('[data-type="popup"]')?.remove();

3. By HTML Tag & Position
If it's always the last <div> inside a specific container or the body, remove the last child inside <body>

    document.body.lastElementChild?.remove();

4. By Text Content
If the banner always contains specific text (e.g., "Subscribe to our newsletter"):

    const banner = Array.from(document.querySelectorAll('div'))
    .find(el => el.textContent.includes('Subscribe to our newsletter'));
    banner?.remove();


5. For Delayed/Dynamic Elements
If it pops up 2–3 seconds after page load:
- Hides ANY div with a class containing "floating" as soon as it appears

    GM_addStyle('div[class*="floating"] { display: none !important; }');
- If GM_addStyle won't work for some reason, you need more complex code:

5.1. MutationObserver (The Professional Way)
This listens to the DOM in real-time and deletes the element the exact millisecond it gets added to the page. No guessing delay times.
Fixed timeouts can fail if your internet connection is slow or fast on a given day. MutationObserver catches it immediately regardless of network speed without running CPU-heavy loops.


const observer = new MutationObserver(() => {
    const banner = document.getElementById('floatingBanner'); // or querySelector('.your-class')
    if (banner) {
        banner.remove();
        observer.disconnect(); // Stop watching once found and removed
    }
});

// Start observing changes in the document body
observer.observe(document.body, { childList: true, subtree: true });

5.2. setInterval Polling (The Simple Way)
Checks every 500 milliseconds for the element. Once found, it removes it and stops the timer.


const checkInterval = setInterval(() => {
    const banner = document.querySelector('.ad-banner'); // replace with your selector
    if (banner) {
        banner.remove();
        clearInterval(checkInterval); // Stop checking
    }
}, 500);


// Safety net: stop checking after 10 seconds if it never appears
setTimeout(() => clearInterval(checkInterval), 10000);

5.3. Fixed setTimeout (Quick & Dirty)
If you know it always takes about 3 seconds, you can just wait 3 seconds before running the removal.


setTimeout(() => {
    document.querySelector('.ad-banner')?.remove();
}, 3000);


*/
})();

Source: Gemini


_______________________________________


pus acum 3 zile
   
Pagini: 1  

Mergi la