Vanilla JavaScript binding to gdpr-guard as efficiently and easily as possible, based on data provided in the DOM
You can have a look at the demo to see it in action and inspect the DOM.
You can also have a look at the docs.
Via NPM: npm i -S html-gdpr-guard
Via Yarn: yarn add html-gdpr-guard
As a 3rd-party script tag:
<script src="/path/to/dist/index.js"></script>
Using modules:
import {
restoreHtmlGdprManager,
//
HtmlGdprGuardError,
NoManagerDefinitionError,
NoNameError,
NoCheckboxError,
} from "html-gdpr-guard"
Using commonjs:
const {
restoreHtmlGdprManager,
//
HtmlGdprGuardError,
NoManagerDefinitionError,
NoNameError,
NoCheckboxError,
} = require("html-gdpr-guard");
In the browser:
const {
restoreHtmlGdprManager,
//
HtmlGdprGuardError,
NoManagerDefinitionError,
NoNameError,
NoCheckboxError,
} = htmlGdprGuard;
To restore your manager instance, you will need an instance of GdprSavior
.
The simplest one is gdpr-guard-local which relies on local storage.
The simplest way to restore your manager is to let the library do everything:
const gdprSavior = getMyGdprSavior();
const manager = await restoreHtmlGdprManager(gdprSavior);
If it fails, it will throw an instance of HtmlGdprGuardError
which can be one of the following:
NoManagerDefinitionError
NoNameError
NoCheckboxError
A guard's data is scoped within the root element that is marked using the data-gdpr-guard
attribute.
That attribute can contain the name of your guard, otherwise the first element that has a data-gdpr-guard-name
will be used to retrieve the guard's name. It is mandatory
The guard's description can be added via a data-gdpr-guard-description
with a value on the root element, or
you can tag an element with data-gdpr-guard-description
whose textContent
will be treated as its description.
It is optional
It will default to an empty string. Note that if the description is the attribute's value it won't be shown to the user.
The guard's storage can be added via a data-gdpr-guard-storage
attribute whose value should be the key in the GdprStorage
enum. It is optional
The checkbox that controls the guard is resolved (among the guard's children) in the following order based on the guard's name:
name
attribute is the guard's namedata-gdpr-checkbox
attributeTo mark the guard as required you can:
gdpr-guard-required
attribute on the guard's root elementrequired
attribute on the guard's checkboxNote that, just like the way gdpr-guard
handles required guards, if a parent is required
then all of its children will be as well.
You don't have to make your checkbox required and disabled, it will be all synced up as the guard is parsed.
A group behaves exactly like a guard, except that its root is marked using data-gdpr-group
instead of data-gdpr-guard
.
The manager behaves a little bit like a guard, but has specific attributes:
data-gdpr
is used to find the root of your manager, unless you provide it in restoreHtmlGdprManager
's options. Its value is treated as the manager's name. You don't have to give it a value. You can skip it altogether.data-gdpr-manager
that can be used to store the name of the manager if you didn't provide a value to the data-gdpr
attribute (or the attribute itself)You can pass a set of options as the second argument of restoreHtmlGdprManager
. Let's inspect them one by one.
gdprEl
HTMLElementThe html element that will serve as root of the manager's definitions. By default, it will use document.querySelector("[data-gdpr]")
.
If the manager can't be found, it'll throw a NoManagerDefinitionError
.
autoCloseBanner
boolWhether the banner should be automatically closed once the manager has been restored if the consent banner has already been shown.
bindEventHandlersHook
functionA callback that can be used to attach event listeners to the manager's GdprManagerEventHub
.
The callback's signature is the following:
type BindEventsCallback = (eventsHub: GdprManagerEventHub) => void;
For instance:
await restoreHtmlGdprManager(gdprSavior, {
bindEventHandlersHook(eventsHub) {
eventsHub.onEnable("myGuard", () => {
connectToWsApi();
});
eventsHub.onDisable("myGuard", () => {
disconnectFromWsApi();
});
},
});
addGuardsBeforeHook
functionA callback that can be used to add guards' definitions to the GdprManagerBuilder
instance before the library parses data from the DOM.
The callback's signature is the following:
type AddGuardsCallback = (managerBuilder: GdprManagerBuilder) => void;
addGuardsAfterHook
functionA callback that can be used to add guards' definitions to the GdprManagerBuilder
instance after the library parsed data from the DOM.
The callback's signature is the following:
type AddGuardsCallback = (managerBuilder: GdprManagerBuilder) => void;
onDeclineAllErrorHook
functionA callback that can be used to hande/capture errors when the user click on a "decline all" button.
The callback's signature is the following:
type StoreErrorHandler = (didStore: boolean, error?: Error) => void;
onSaveErrorHook
functionA callback that can be used to hande/capture errors when the user saves their preferences.
The callback's signature is the following:
type StoreErrorHandler = (didStore: boolean, error?: Error) => void;
onCancelErrorHook
functionA callback that can be used to hande/capture errors when the user click on a "cancel" button.
The callback's signature is the following:
type StoreErrorHandler = (didStore: boolean, error?: Error) => void;
onBannerClose
functionA callback that can be used to execute code when the GDPR banner is closed.
The callback's signature is the following:
type BannerCallback = () => void;
For instance:
await restoreHtmlGdprManager(gdprSavior, {
onBannerClose() {
hideMyGdprBanner();
},
});
onBannerOpen
functionA callback that can be used to execute code when the GDPR banner is closed.
The callback's signature is the following:
type BannerCallback = () => void;
For instance:
await restoreHtmlGdprManager(gdprSavior, {
onBannerOpen() {
showMyGdprBanner();
},
});