sitexw / BlockAdBlock

Allows you to detect the extension AdBlock (and other)
http://fuckadblock.sitexw.fr/blockadblock/
MIT License
614 stars 119 forks source link

most basic adblock detection api #44

Open K2SO4AL2SO4324H2O opened 3 years ago

K2SO4AL2SO4324H2O commented 3 years ago
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 commented 3 years ago

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 commented 1 year ago

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.