I
I
inquir2021-06-11 13:54:58
JavaScript
inquir, 2021-06-11 13:54:58

How to check ad blocking?

We need a script that will check if the user has ad blocking enabled.
I want to implement this on my website

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex, 2021-06-11
@Kozack

Eat to download something that is very likely to be blocked. Check if the resource was loaded successfully.

V
Vovash, 2021-06-11
@V0vash

faced with a similar need, tk. API got into "ru AdList", solved in this way:

export default function detectAdBlock(){
    return new Promise((resolve) => {
        const adElement = document.createElement("div");
        adElement.classList.add('adsbygoogle');
        adElement.style.cssText = 'height: 1px; width: 1px; background-color: transparent';
        document.body.appendChild(adElement);

        const adElementStyle = getComputedStyle(adElement, null);
        window.setTimeout(()=>{
            if(adElementStyle.display === 'none'){
                resolve(true)
            }else{
                resolve(false)
            }
            document.body.removeChild(adElement);
        }, 20)
    })
}

it should be taken into account that extensions have a higher priority than scripts => in my case, I knocked out the requirement to add the site to the whitelist, because. otherwise, the wonderful "AdList" regular expressions did not allow receiving data from the API

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question