https://github.com/hubspotwebteam/adblock-detect
Script to intercept if common strategies from AD Blockers are detected in the browser.
https://github.com/hubspotwebteam/adblock-detect
Last synced: 11 months ago
JSON representation
Script to intercept if common strategies from AD Blockers are detected in the browser.
- Host: GitHub
- URL: https://github.com/hubspotwebteam/adblock-detect
- Owner: HubSpotWebTeam
- Created: 2022-06-10T14:35:02.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-01-10T21:12:14.000Z (over 2 years ago)
- Last Synced: 2024-11-22T16:47:00.600Z (over 1 year ago)
- Language: TypeScript
- Size: 336 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# AdBlocker Detect Script
## Index
- [Development](#development)
- [How to use it](#how-to-use-it)
- [A global variable](#a-global-variable)
## Development
If you need to build the code for this package, you just need to either run the `npm run build` command, or if you want to build it in watch mode, just run `npm run dev`.
## How to use it
First thing is to install the package, as a dependency of your project.
```bash
npm install --save @hs-web-team/adblocker-detect
```
Once installed you need to simply import the `checkAdblock` function in your module. Here an example:
```javascript
import { checkAdblock } from '@hs-web-team/adblocker-detect';
/**
* Checks the various strategies to detect an AdBlocker
*/
const hasAdBlockerWrapper = async () => {
const adblocker = await checkAdblock();
if (adblocker) {
const adblockerElement = document.createElement('div');
adblockerElement.id = 'adblocker-detected';
adblockerElement.innerHTML = `
Adblocker detected
Your browser is blocking ads.
`;
document.body.appendChild(adblockerElement);
}
};
hasAdBlockerWrapper();
```
And that's it, you are good to go!
### A global variable
Once called for the first time, a global variable is created, which is used to store the state of the results of the checks. You can invoke the state by calling `window.hs_hasAdBlocker`.