@jarijokinen/consent 0.0.2 → 0.0.4

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.
Files changed (3) hide show
  1. package/README.md +23 -11
  2. package/package.json +5 -2
  3. package/src/consent.js +29 -24
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,23 +43,26 @@ Yarn:
34
43
  allowAll: 'Allow All'
35
44
  },
36
45
  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>
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
- settingsLinkSelector: '.consent-settings-link'
54
+ settingsLinkSelector: '.consent-settings-link',
55
+ countryCookie: 'cf_country',
56
+ countries: ['AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR',
57
+ 'DE', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL',
58
+ 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE', 'IS', 'LI', 'NO',
59
+ 'GB', 'CH', 'MC', 'SM', 'VA', 'JE', 'GG', 'IM']
48
60
  };
49
61
 
50
62
  consent(options);
51
63
 
52
64
  ## License
53
65
 
54
- MIT License. Copyright (c) 2022 [Jari Jokinen](https://jarijokinen.com). See
55
- [LICENSE](https://github.com/jarijokinen/consent/blob/main/LICENSE.txt)
66
+ MIT License. Copyright (c) 2022 - 2025 [Jari Jokinen](https://jarijokinen.com).
67
+ See [LICENSE](https://github.com/jarijokinen/consent/blob/main/LICENSE.txt)
56
68
  for further details.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jarijokinen/consent",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "A zero-dependency, vanilla JavaScript consent manager with native Consent Mode support.",
5
5
  "main": "src/consent.js",
6
6
  "repository": {
@@ -12,5 +12,8 @@
12
12
  "bugs": {
13
13
  "url": "https://github.com/jarijokinen/consent/issues"
14
14
  },
15
- "homepage": "https://github.com/jarijokinen/consent#readme"
15
+ "homepage": "https://github.com/jarijokinen/consent#readme",
16
+ "publishConfig": {
17
+ "access": "public"
18
+ }
16
19
  }
package/src/consent.js CHANGED
@@ -10,17 +10,20 @@ export const consent = (options) => {
10
10
  allowAll: 'Allow All'
11
11
  },
12
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>
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',
22
+ countryCookie: 'cf_country',
23
+ countries: ['AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR',
24
+ 'DE', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL',
25
+ 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE', 'IS', 'LI', 'NO',
26
+ 'GB', 'CH', 'MC', 'SM', 'VA', 'JE', 'GG', 'IM'],
24
27
  ...options
25
28
  };
26
29
 
@@ -35,26 +38,26 @@ export const consent = (options) => {
35
38
  let dialog = document.createElement('div');
36
39
  dialog.classList.add(opts.dialogClass);
37
40
 
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=/';
41
+ const detectCountry = () => {
42
+ const country = document.cookie.split('; ').find(r =>
43
+ r.startsWith(opts.countryCookie + '=')
44
+ )?.split('=')[1];
45
+ if (country ? opts.countries.includes(country) : true) {
46
+ return;
47
+ }
48
+ Object.keys(opts.storages).forEach((storage) => {
49
+ consentState[storage] = 'granted';
50
+ });
51
+ updateConsent();
48
52
  };
49
53
 
50
54
  const getConsent = (storage) => {
51
- return getCookie('consent_' + storage);
55
+ return localStorage.getItem('consent_' + storage);
52
56
  };
53
57
 
54
58
  const updateConsent = () => {
55
59
  Object.keys(opts.storages).forEach((storage) => {
56
- const cookieName = 'consent_' + storage;
57
- setCookie(cookieName, consentState[storage]);
60
+ localStorage.setItem('consent_' + storage, consentState[storage]);
58
61
  });
59
62
 
60
63
  if (typeof gtag === 'function') {
@@ -71,9 +74,8 @@ export const consent = (options) => {
71
74
 
72
75
  const loadConsentState = () => {
73
76
  Object.keys(opts.storages).forEach((storage) => {
74
- const cookieName = 'consent_' + storage;
75
- consentState[storage] = getCookie(cookieName) || undefined;
76
- if (consentState[storage] === undefined) {
77
+ consentState[storage] = getConsent(storage);
78
+ if (consentState[storage] === null) {
77
79
  showDialog();
78
80
  }
79
81
  });
@@ -85,6 +87,8 @@ export const consent = (options) => {
85
87
 
86
88
  const createDialog = () => {
87
89
  dialog.innerHTML = opts.dialogMarkup;
90
+ dialog.querySelector('.consent-title').innerText = opts.dialogTitle;
91
+ dialog.querySelector('.consent-message').innerText = opts.dialogMessage;
88
92
 
89
93
  Object.keys(opts.storages).forEach((storage) => {
90
94
  const fieldset = document.createElement('fieldset');
@@ -164,6 +168,7 @@ export const consent = (options) => {
164
168
  showDialog();
165
169
  };
166
170
 
171
+ detectCountry();
167
172
  loadConsentState();
168
173
  createDialog();
169
174
  };