Last active
January 20, 2023 19:42
-
-
Save ravindu9701/d51b90655b1b51fee3ea629716084ddb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Thank you for pointing that out. I will change it.
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.
and renaming the function at line 4.
@LCweb-ita
Can you write your final code? Thank you.
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
Hey Ravindu, thank you for sharing !
If
Blocked
is a constant you're not supposed to alter it though... You should uselet
instead ofconst
IMHO