( Credit to Marat Tanalin for helping me with the event handling.) I find that it helps me avoid some addictive news websites that make me depressed. No point, really, could have just created a gist, but instead I created a Github project, but still, check it out and star it if it helped you.ĪbDetector: Simple vanilla JavaScript AdBlock Detector.I use this script to block access to a one or more websites with vanilla JavaScript. This script works for most (if not all) ad blockers. If one of these tests is true, it then displays the ab-message element to adblock users. Then it runs multiple checks on that iframe (if it was loaded successfully or if its style was modified). (It is randomly generated because some adblocks are smart, at some point, they realize a link is fake). A little explanation of how this script worksįirst, it appends an iframe with a source of a randomly generated link. Note the inline style added to hide it originally (Of course, you can also do this from your own CSS file).Īlso note that it takes 500ms, that's because it has to wait for the adblocker to do its thing or it won't work. Now you can use the ab-message id wherever you want to display a message to AdBlock users: Var someAd = document.getElementById('some-ad') ĭocument.getElementById('ab-message').style.display = 'block' I suppose (but cannot test) that it will give an error if the resource could not be loaded (because it was blocked). Downside is that if someone decides to block that annoying background image, it will hide your content too.Īs far as a script goes, you might be able to load the ad with an ajax request. This is not useful for an ad (not clickable), but might help you display an other message. You could however add a background-image with content. There will always be a smart CSS selector that will -just- select your element. There is no real way of going around this. However, while AdBlock might not be able to block your ad from loading, it might still be able to hide it. It would be possible to rewrite such an url on the server to point to your ad. A rule would either match no ads, or too many resources. As far as I am aware, a rule in AdBlock cannot contain a regex. , where the image name will be random every time and without a common prefix). You could disguise your request in a way that it will never be 'matchable' (e.g. So what exactly could you do to be 'smarter' than AdBlock?
0 Comments
Leave a Reply. |