@horat1us/detect-ad-block
v1.0.0
Published
Browser Ad Block add-ons detector
Downloads
62
Maintainers
Readme
Detect Ad Block
Simple module to detect advertising block add-on in browser using Promise.
- Promise-based
- TypeScript
- ESM/CommonJS support
Require Promise to be supported or polifylled.
Inspired by [email protected].
Installation
Using npm:
npm i @horat1us/detect-ad-block@^1.0
Usage
import { DetectAdBlock } from "@horat1us/detect-ad-block";
function detect() {
const detectAdBlock = DetectAdBlock();
detectAdBlock.perform({ timeout: 1000 })
.then((result) => {
console.log(result ? `AdBlock Detected` : `AdBlock Not Found`);
})
.catch((error) => {
console.error(`Error while AdBlock checking: `, error);
});
}
const timeout = 5000;
// You may use window.onload, document.onreadystatechange etc.
if (window.requestIdleCallback) {
window.requestIdleCallback(detect, timeout);
} else {
setTimeout(detect, timeout)
}
Note: You should not run adblock checking directly on or after page load.
DetectPlugin
You may use built-in plugins:
- http:ajax (default) - http request block detection using XMLHttpRequest (AJAX)
- html (default) - html element block detection
- http:script - http request block detection using script tag (<script src="/ad/.... />)
Also, you may implement custom plugin using DetectPlugin interface.
import { DetectAdBlock, plugins } from "@horat1us/detect-ad-block";
const detectAdBlock = DetectAdBlock([
// your plugins list
plugins.ajax,
plugins.script,
plugins.html,
// using built-in plugin with custom options
plugins.Html({
name: "custom-html",
baitStyle: "...",
baitClass: "...",
interval: 100,
timeout: 950,
}),
// or custom plugin
{
name: "custom-plugin",
version: 1,
detect(resolve) {
const result = performSomeCheck();
resolve(result);
},
},
])
Contributors
- Alexander Letnikow
- Valentin Allaire - as author of FuckAdBlock