Skip to content

Instantly share code, notes, and snippets.

@ravindu9701
Last active January 20, 2023 19:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ravindu9701/d51b90655b1b51fee3ea629716084ddb to your computer and use it in GitHub Desktop.
Save ravindu9701/d51b90655b1b51fee3ea629716084ddb to your computer and use it in GitHub Desktop.
async function checkForAdBlocker() {
let Blocked;
async function Request() {
try {
return fetch(
new Request(
"https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js", {
method: 'HEAD',
mode: 'no-cors'
}))
.then(function(response) {
// There is no AdBlocker
Blocked = false;
return Blocked;
}).catch(function(e) {
// Failed, Because of an AdBlocker
Blocked = true;
return Blocked;
});
} catch (error) {
console.log(error);
Blocked = true;
return Blocked;
}
}
return Blocked !== undefined ? Blocked : await Request();
}
const usingBlocker = await checkForAdBlocker();
@TangoMan75
Copy link

Hey Ravindu, thank you for sharing !
If Blocked is a constant you're not supposed to alter it though... You should use let instead of const IMHO

@ravindu9701
Copy link
Author

@TangoMan75 Thank you for pointing that out. I will change it.

@LCweb-ita
Copy link

Great trick, but your code throws two errors on firefox:

  • "await" must be used with async function > line 31
  • "Request" is not a constructor > line 28

I solved it using

checkForAdBlocker().then(response => {do-something}); at line 31

and renaming the function at line 4.

@stefanmm
Copy link

and renaming the function at line 4.
@LCweb-ita
Can you write your final code? Thank you.

@dchubad
Copy link

dchubad commented Jan 20, 2023

How is this script used? there is no readme file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment