Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

most basic adblock detection api #44

Open
K2SO4AL2SO4324H2O opened this issue Jun 29, 2021 · 2 comments
Open

most basic adblock detection api #44

K2SO4AL2SO4324H2O opened this issue Jun 29, 2021 · 2 comments

Comments

@K2SO4AL2SO4324H2O
Copy link

const adsEnabled = async () => {
 let ads = true;
    try {
      const url = `https://ads.google.com?=${new Date().getTime()}`;
      await fetch(url, { mode: "no-cors" });
    } catch (error) {
      if (error.message === "Failed to fetch") {
        ads = false;
      }
    }
return ads;
  };

// check 

const isAdEnabled = await adsEnabled();
console.log(isAdEnabled);
@tom-lord
Copy link

tom-lord commented Aug 6, 2021

This doesn't work on firefox. Here's a minimal fix:

    if (error.message === 'Failed to fetch' || error.message === 'NetworkError when attempting to fetch resource.') {
      adBlocked = true;
    }

@tisaconundrum2
Copy link

tisaconundrum2 commented Jan 27, 2023

Even simpler soultion

    <script src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script>
        $('#test').append(window.adsbygoogle?.loaded ? 'Ads not blocked' : 'Ads blocked');
    </script>

Note: This method is a little heavy handed and actually requires the user to completely disable adblock, or not have it installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants