@jarijokinen/consent 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +20 -0
- package/README.md +56 -0
- package/package.json +16 -0
- package/src/consent.js +172 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright (c) 2022 Jari Jokinen
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
5
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
6
|
+
the Software without restriction, including without limitation the rights to
|
|
7
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
8
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
9
|
+
so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# consent
|
|
2
|
+
|
|
3
|
+
A zero-dependency, vanilla JavaScript consent manager with native Consent Mode
|
|
4
|
+
support.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
NPM:
|
|
9
|
+
|
|
10
|
+
npm install @jarijokinen/consent
|
|
11
|
+
|
|
12
|
+
Yarn:
|
|
13
|
+
|
|
14
|
+
yarn add @jarijokinen/consent
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
import { consent } from '@jarijokinen/consent';
|
|
19
|
+
|
|
20
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
21
|
+
consent();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
## Configuration
|
|
25
|
+
|
|
26
|
+
const options = {
|
|
27
|
+
storages: {
|
|
28
|
+
analytics_storage: 'Analytics',
|
|
29
|
+
ad_storage: 'Marketing'
|
|
30
|
+
},
|
|
31
|
+
actions: {
|
|
32
|
+
denyAll: 'Deny All',
|
|
33
|
+
allowSelected: 'Allow Selected',
|
|
34
|
+
allowAll: 'Allow All'
|
|
35
|
+
},
|
|
36
|
+
dialogMarkup: `
|
|
37
|
+
<h2>Cookies</h2>
|
|
38
|
+
<p>
|
|
39
|
+
We would like to get your permission to use cookies for these purposes:
|
|
40
|
+
</p>
|
|
41
|
+
<form>
|
|
42
|
+
<div class="consent-fields"></div>
|
|
43
|
+
<div class="consent-buttons"></div>
|
|
44
|
+
</form>
|
|
45
|
+
`,
|
|
46
|
+
dialogClass: 'consent',
|
|
47
|
+
settingsLinkSelector: '.consent-settings-link'
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
consent(options);
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
MIT License. Copyright (c) 2022 [Jari Jokinen](https://jarijokinen.com). See
|
|
55
|
+
[LICENSE](https://github.com/jarijokinen/consent/blob/main/LICENSE.txt)
|
|
56
|
+
for further details.
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jarijokinen/consent",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A zero-dependency, vanilla JavaScript consent manager with native Consent Mode support.",
|
|
5
|
+
"main": "src/consent.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/jarijokinen/consent.git"
|
|
9
|
+
},
|
|
10
|
+
"author": "Jari Jokinen",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/jarijokinen/consent/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/jarijokinen/consent#readme"
|
|
16
|
+
}
|
package/src/consent.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
export const consent = (options) => {
|
|
2
|
+
const opts = {
|
|
3
|
+
storages: {
|
|
4
|
+
analytics_storage: 'Analytics',
|
|
5
|
+
ad_storage: 'Marketing'
|
|
6
|
+
},
|
|
7
|
+
actions: {
|
|
8
|
+
denyAll: 'Deny All',
|
|
9
|
+
allowSelected: 'Allow Selected',
|
|
10
|
+
allowAll: 'Allow All'
|
|
11
|
+
},
|
|
12
|
+
dialogMarkup: `
|
|
13
|
+
<h2>Cookies</h2>
|
|
14
|
+
<p>
|
|
15
|
+
We would like to get your permission to use cookies for these purposes:
|
|
16
|
+
</p>
|
|
17
|
+
<form>
|
|
18
|
+
<div class="consent-fields"></div>
|
|
19
|
+
<div class="consent-buttons"></div>
|
|
20
|
+
</form>
|
|
21
|
+
`,
|
|
22
|
+
dialogClass: 'consent',
|
|
23
|
+
settingsLinkSelector: '.consent-settings-link',
|
|
24
|
+
...options
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
let consentState = {
|
|
28
|
+
ad_storage: 'denied',
|
|
29
|
+
analytics_storage: 'denied',
|
|
30
|
+
functionality_storage: 'denied',
|
|
31
|
+
personalization_storage: 'denied',
|
|
32
|
+
security_storage: 'denied'
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
let dialog = document.createElement('div');
|
|
36
|
+
dialog.classList.add(opts.dialogClass);
|
|
37
|
+
|
|
38
|
+
const getCookie = (name) => {
|
|
39
|
+
const parts = ('; ' + document.cookie).split('; ' + name + '=');
|
|
40
|
+
return parts.length == 2 ? parts.pop().split(';').shift() : undefined;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const setCookie = (name, value) => {
|
|
44
|
+
let expires = new Date();
|
|
45
|
+
expires.setTime(expires.getTime() + 356 * 86400000);
|
|
46
|
+
document.cookie =
|
|
47
|
+
name + '=' + value + ';expires=' + expires.toUTCString() + ';path=/';
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const getConsent = (storage) => {
|
|
51
|
+
return getCookie('consent_' + storage);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const updateConsent = () => {
|
|
55
|
+
Object.keys(opts.storages).forEach((storage) => {
|
|
56
|
+
const cookieName = 'consent_' + storage;
|
|
57
|
+
setCookie(cookieName, consentState[storage]);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
if (typeof gtag === 'function') {
|
|
61
|
+
gtag('consent', 'update', consentState);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const updateFields = () => {
|
|
66
|
+
Object.keys(opts.storages).forEach((storage) => {
|
|
67
|
+
dialog.querySelector('#consent-field-' + storage).checked =
|
|
68
|
+
consentState[storage] == 'granted' ? true : false;
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const loadConsentState = () => {
|
|
73
|
+
Object.keys(opts.storages).forEach((storage) => {
|
|
74
|
+
const cookieName = 'consent_' + storage;
|
|
75
|
+
consentState[storage] = getCookie(cookieName) || undefined;
|
|
76
|
+
if (consentState[storage] === undefined) {
|
|
77
|
+
showDialog();
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
if (typeof gtag === 'function') {
|
|
82
|
+
gtag('consent', 'update', consentState);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const createDialog = () => {
|
|
87
|
+
const fields = document.createElement('div');
|
|
88
|
+
Object.keys(opts.storages).forEach((storage) => {
|
|
89
|
+
const fieldset = document.createElement('fieldset');
|
|
90
|
+
const checkbox = document.createElement('input');
|
|
91
|
+
checkbox.setAttribute('type', 'checkbox');
|
|
92
|
+
checkbox.setAttribute('name', 'consent-field-' + storage);
|
|
93
|
+
checkbox.setAttribute('id', 'consent-field-' + storage);
|
|
94
|
+
if (getConsent(storage) === 'granted') {
|
|
95
|
+
checkbox.setAttribute('checked', 'checked');
|
|
96
|
+
}
|
|
97
|
+
const label = document.createElement('label');
|
|
98
|
+
label.setAttribute('for', 'consent-field-' + storage);
|
|
99
|
+
label.innerText = opts.storages[storage];
|
|
100
|
+
fieldset.appendChild(checkbox);
|
|
101
|
+
fieldset.appendChild(label);
|
|
102
|
+
fields.appendChild(fieldset);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const buttons = document.createElement('div');
|
|
106
|
+
Object.keys(opts.actions).forEach((action) => {
|
|
107
|
+
const button = document.createElement('input');
|
|
108
|
+
button.setAttribute('type', 'button');
|
|
109
|
+
button.setAttribute('value', opts.actions[action]);
|
|
110
|
+
|
|
111
|
+
switch (action) {
|
|
112
|
+
case 'denyAll':
|
|
113
|
+
button.onclick = () => {
|
|
114
|
+
Object.keys(opts.storages).forEach((storage) => {
|
|
115
|
+
consentState[storage] = 'denied';
|
|
116
|
+
});
|
|
117
|
+
updateConsent();
|
|
118
|
+
updateFields();
|
|
119
|
+
hideDialog();
|
|
120
|
+
};
|
|
121
|
+
break;
|
|
122
|
+
case 'allowSelected':
|
|
123
|
+
button.onclick = () => {
|
|
124
|
+
Object.keys(opts.storages).forEach((storage) => {
|
|
125
|
+
consentState[storage] = document.querySelector(
|
|
126
|
+
'#consent-field-' + storage
|
|
127
|
+
).checked
|
|
128
|
+
? 'granted'
|
|
129
|
+
: 'denied';
|
|
130
|
+
});
|
|
131
|
+
updateConsent();
|
|
132
|
+
updateFields();
|
|
133
|
+
hideDialog();
|
|
134
|
+
};
|
|
135
|
+
break;
|
|
136
|
+
case 'allowAll':
|
|
137
|
+
button.onclick = () => {
|
|
138
|
+
Object.keys(opts.storages).forEach((storage) => {
|
|
139
|
+
consentState[storage] = 'granted';
|
|
140
|
+
});
|
|
141
|
+
updateConsent();
|
|
142
|
+
updateFields();
|
|
143
|
+
hideDialog();
|
|
144
|
+
};
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
buttons.appendChild(button);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
dialog.innerHTML = opts.dialogMarkup;
|
|
152
|
+
dialog.querySelector('.consent-fields').appendChild(fields);
|
|
153
|
+
dialog.querySelector('.consent-buttons').appendChild(buttons);
|
|
154
|
+
document.querySelector('body').appendChild(dialog);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const showDialog = () => {
|
|
158
|
+
dialog.style.display = 'block';
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const hideDialog = () => {
|
|
162
|
+
dialog.style.display = 'none';
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
document.querySelector(opts.settingsLinkSelector).onclick = (ev) => {
|
|
166
|
+
ev.preventDefault();
|
|
167
|
+
showDialog();
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
loadConsentState();
|
|
171
|
+
createDialog();
|
|
172
|
+
};
|