yarn add detect-adblock
DetectAdbblock is a library which recognizes using of ad blocker in a browser. You can use it in case you want to show advertisment towards users or just run important script which are beign blocked.
This library is inspired by BlockAdBlock project. Main changes are that this library is simplified, completely written in ES6 and contains tests written in Jest.
To use Detect AdBlock you need just to import DetectAdblock
class to your project and then use it as a standard javascript class.
timeout
specifies number ofms
to run check for, default is 1000.enabledHandler
is a callback which is called when no ad block is recognized.disabledHandler
is a callback which is called when no ad block is not recognized.
startChecking(count)
is a method which starts checking for ad blockers. Count is the number of checks in one run. Default is 5.
import DetectAdblock from '@blueberry/detect-adblock'
const onEnabled = () => console.log('ENABLED');
const onDisabled = () => console.log('DISABLED');
const check = () => {
// It is important to check wheter the script is rendered in the browser.
if (!process.env.IS_BROWSER) return;
const detector = new DetectAdblock(500, onEnabled, onDisabled);
detector.startChecking(3);
return;
};
The package should work in all modern browsers including IE8+.