@jarijokinen/consent 0.0.2 → 0.0.3
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/README.md +15 -8
- package/package.json +1 -1
- package/src/consent.js +12 -26
package/README.md
CHANGED
|
@@ -15,14 +15,23 @@ Yarn:
|
|
|
15
15
|
|
|
16
16
|
## Usage
|
|
17
17
|
|
|
18
|
+
Initialization:
|
|
19
|
+
|
|
18
20
|
import { consent } from '@jarijokinen/consent';
|
|
19
21
|
|
|
20
22
|
document.addEventListener('DOMContentLoaded', () => {
|
|
21
23
|
consent();
|
|
22
24
|
});
|
|
23
25
|
|
|
26
|
+
Add link that opens the dialog:
|
|
27
|
+
|
|
28
|
+
<a href="#" class="consent-settings-link">Cookie Settings</a>
|
|
29
|
+
|
|
24
30
|
## Configuration
|
|
25
31
|
|
|
32
|
+
Customize configuration options by passing some or all of them as a first
|
|
33
|
+
argument to the consent() function.
|
|
34
|
+
|
|
26
35
|
const options = {
|
|
27
36
|
storages: {
|
|
28
37
|
analytics_storage: 'Analytics',
|
|
@@ -34,15 +43,13 @@ Yarn:
|
|
|
34
43
|
allowAll: 'Allow All'
|
|
35
44
|
},
|
|
36
45
|
dialogMarkup: `
|
|
37
|
-
<h2
|
|
38
|
-
<
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
<form>
|
|
42
|
-
<div class="consent-fields"></div>
|
|
43
|
-
<div class="consent-buttons"></div>
|
|
44
|
-
</form>
|
|
46
|
+
<h2 class="consent-title"></h2>
|
|
47
|
+
<div class="consent-message"></div>
|
|
48
|
+
<div class="consent-fields"></div>
|
|
49
|
+
<div class="consent-buttons"></div>
|
|
45
50
|
`,
|
|
51
|
+
dialogTitle: 'Cookies',
|
|
52
|
+
dialogMessage: 'We would like to get your permission to use cookies for:',
|
|
46
53
|
dialogClass: 'consent',
|
|
47
54
|
settingsLinkSelector: '.consent-settings-link'
|
|
48
55
|
};
|
package/package.json
CHANGED
package/src/consent.js
CHANGED
|
@@ -10,15 +10,13 @@ export const consent = (options) => {
|
|
|
10
10
|
allowAll: 'Allow All'
|
|
11
11
|
},
|
|
12
12
|
dialogMarkup: `
|
|
13
|
-
<h2
|
|
14
|
-
<
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
<form>
|
|
18
|
-
<div class="consent-fields"></div>
|
|
19
|
-
<div class="consent-buttons"></div>
|
|
20
|
-
</form>
|
|
13
|
+
<h2 class="consent-title"></h2>
|
|
14
|
+
<div class="consent-message"></div>
|
|
15
|
+
<div class="consent-fields"></div>
|
|
16
|
+
<div class="consent-buttons"></div>
|
|
21
17
|
`,
|
|
18
|
+
dialogTitle: 'Cookies',
|
|
19
|
+
dialogMessage: 'We would like to get your permission to use cookies for:',
|
|
22
20
|
dialogClass: 'consent',
|
|
23
21
|
settingsLinkSelector: '.consent-settings-link',
|
|
24
22
|
...options
|
|
@@ -35,26 +33,13 @@ export const consent = (options) => {
|
|
|
35
33
|
let dialog = document.createElement('div');
|
|
36
34
|
dialog.classList.add(opts.dialogClass);
|
|
37
35
|
|
|
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
36
|
const getConsent = (storage) => {
|
|
51
|
-
return
|
|
37
|
+
return localStorage.getItem('consent_' + storage);
|
|
52
38
|
};
|
|
53
39
|
|
|
54
40
|
const updateConsent = () => {
|
|
55
41
|
Object.keys(opts.storages).forEach((storage) => {
|
|
56
|
-
|
|
57
|
-
setCookie(cookieName, consentState[storage]);
|
|
42
|
+
localStorage.setItem('consent_' + storage, consentState[storage]);
|
|
58
43
|
});
|
|
59
44
|
|
|
60
45
|
if (typeof gtag === 'function') {
|
|
@@ -71,9 +56,8 @@ export const consent = (options) => {
|
|
|
71
56
|
|
|
72
57
|
const loadConsentState = () => {
|
|
73
58
|
Object.keys(opts.storages).forEach((storage) => {
|
|
74
|
-
|
|
75
|
-
consentState[storage]
|
|
76
|
-
if (consentState[storage] === undefined) {
|
|
59
|
+
consentState[storage] = getConsent(storage);
|
|
60
|
+
if (consentState[storage] === null) {
|
|
77
61
|
showDialog();
|
|
78
62
|
}
|
|
79
63
|
});
|
|
@@ -85,6 +69,8 @@ export const consent = (options) => {
|
|
|
85
69
|
|
|
86
70
|
const createDialog = () => {
|
|
87
71
|
dialog.innerHTML = opts.dialogMarkup;
|
|
72
|
+
dialog.querySelector('.consent-title').innerText = opts.dialogTitle;
|
|
73
|
+
dialog.querySelector('.consent-message').innerText = opts.dialogMessage;
|
|
88
74
|
|
|
89
75
|
Object.keys(opts.storages).forEach((storage) => {
|
|
90
76
|
const fieldset = document.createElement('fieldset');
|