-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
585 lines (528 loc) · 17.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
<!DOCTYPE html>
<html
class="uk-background-muted"
lang="en"
>
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Consenty Docs</title>
<script
src="consenty.js"
defer
></script>
<link
rel="stylesheet"
href="assets/uikit.min.css"
/>
<script
src="assets/uikit.min.js"
defer
></script>
<style>
h2,
h3 {
margin-top: 0;
}
.border {
border: 2px solid #efefef;
padding: 10px;
}
.uk-card-default {
color: #1b1b1b;
}
iframe {
max-width: 100%;
}
em {
color: black;
background-color: #efefef;
padding: 3px 8px;
}
</style>
</head>
<body class="uk-padding-small">
<div
class="uk-card uk-card-default"
style="max-width: 800px; margin: auto"
>
<div class="uk-card-body">
<h1 class="uk-text-center uk-margin-medium-bottom">Consenty Docs</h1>
<div class="uk-alert">
<label>
<input
class="uk-checkbox"
type="checkbox"
consenty-toggle="highlight"
/>
Allow code highlighting and show docs
</label>
</div>
<div class="uk-text-small uk-margin-bottom">
Note: If you allow code highlighting this website will lode the
shiki.style library from CDN @ https://esm.sh/[email protected]
</div>
<template consenty-if="highlight">
<hr />
<h2>Setup</h2>
<p>
<a href="https://github.com/baumrock/Consenty">
Download Consenty from Github
</a>
and add it to the head of your website:
</p>
<template class="code">
<script
src="consenty.js"
defer
></script>
</template>
<hr />
<h2>Hello World</h2>
<div class="uk-alert">
Note: This demo-page uses the
<a href="https://getuikit.com/">UIkit frontend framework</a>
for styling, but Consenty will work without any external
dependencies!
</div>
<div class="uk-margin code border">
<button
class="uk-button"
consenty-allow="hello-world"
>
ALLOW
</button>
<button
class="uk-button"
consenty-revoke="hello-world"
>
REVOKE
</button>
<div class="uk-margin-small-top">
Current status:
<template consenty-if="hello-world">allowed</template>
<template
consenty-if="!hello-world"
consenty-tag="span"
>revoked</template
>
</div>
</div>
<p>
Consenty will inject a <div> right before your
<template> - you can make it use a <span> instead by
adding <em>consenty-tag="span"</em> to your element.
</p>
<div class="uk-text-small">
Note: Try reloading the page and see that the state persists!
</div>
<hr />
<h2>Script Example</h2>
<p>
The example above has one caveat: If you put a script-tag inside one
of the <span>-tags that script would either immediately get
executed or if using a src-attribute load the script from the given
source. The idea of consenty is to prevent this, so you can simply
use the <template>-tag instead:
</p>
<div class="uk-margin code border">
<button
class="uk-button"
consenty-allow="script-example"
>
ALLOW
</button>
<button
class="uk-button"
consenty-revoke="script-example"
>
REVOKE
</button>
<div class="uk-margin-small-top">
check your console for this example
</div>
<template consenty-if="script-example">
<script>
alert("Script loaded");
console.log("script example status: allowed");
</script>
</template>
<template consenty-if="!script-example">
<script>
console.log("script example status: revoked");
</script>
</template>
</div>
<hr />
<h2>YouTube Inline Example</h2>
<div class="uk-alert">
Note: This example uses UIkit for easier styling.
</div>
<div class="code border keepclasses uk-margin-bottom">
<template consenty-if="!youtube-inline">
<div class="uk-inline">
<img
src="assets/thumbnail.jpg"
width="560"
height="315"
/>
<div class="uk-overlay-default uk-position-cover">
<div class="uk-position-center uk-text-center">
<a
class="uk-button uk-button-primary"
consenty-allow="youtube-inline"
>
Allow Youtube
</a>
<div class="uk-margin-small-top">See [privacy policy]</div>
</div>
</div>
</div>
</template>
<template consenty-if="youtube-inline">
<iframe
width="560"
height="315"
src="https://www.youtube.com/embed/ncS36UqaBvc?autoplay=1"
title="YouTube video player"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
referrerpolicy="strict-origin-when-cross-origin"
allowfullscreen
></iframe>
</template>
<div class="uk-margin-small-top">
<div>
Place this on your privacy page so that the user can revoke
consent:
</div>
<label>
<input
type="checkbox"
class="uk-checkbox"
consenty-toggle="youtube-inline"
/>
Allow embedding of YouTube videos
</label>
</div>
</div>
<hr />
<h2>YouTube Modal Example</h2>
<div class="uk-alert">
Note: This example uses UIkit for easier styling and for the
<a
href="https://getuikit.com/docs/modal"
target="_blank"
>Modal Component.</a
>
</div>
<p>The basic concept is as follows:</p>
<template class="code">
<!-- always show the image to open the modal -->
<!-- listen to click events -->
<a
href="#"
consenty-click="youtube-modal"
><img src="..."
/></a>
<!-- modal to ask for consent -->
<div
id="yt-modal-consent"
uk-modal
>
...
</div>
<!-- modal to show the video -->
<!-- !! use <template> to prevent loading of iframe !! -->
<template consenty-if="youtube-modal">
<div
id="yt-modal"
uk-modal
>
<!-- youtube iframe here -->
</div>
</template>
</template>
<p>
And then some custom JS to do different things based on consent:
</p>
<script
class="code"
data-lang="js"
>
// handle click event and open modal according to consent
document.addEventListener("consenty:click", (e) => {
const prop = "youtube-modal";
if (e.detail !== prop) return;
if (consenty.isTrue(prop)) {
consenty
// this loads markup from within <template>
.load(prop)
// wait for the promise
.then(() => {
// open the youtube modal
UIkit.modal("#yt-modal").show();
});
} else {
// open the consent modal
UIkit.modal("#yt-modal-consent").show();
}
});
</script>
<p>
Finally we need to handle clicks on buttons in the consent modal:
</p>
<script
class="code"
data-lang="js"
>
document.addEventListener("click", (e) => {
// clicked on "allow once"
if (e.target.matches("#yt-modal-once")) {
// load iframe into dom and show modal
consenty.load("youtube-modal").then(() => {
UIkit.modal("#yt-modal").show();
});
}
// clicked on "allow always"
else if (e.target.matches("#yt-modal-always")) {
// make consent persistant
consenty.allow("youtube-modal");
// load iframe into dom and show modal
consenty.load("youtube-modal").then(() => {
UIkit.modal("#yt-modal").show();
});
}
});
</script>
<div class="border uk-margin-bottom">
<div class="uk-inline">
<img
src="assets/thumbnail.jpg"
width="560"
height="315"
/>
<div class="uk-overlay-default uk-position-cover">
<div class="uk-position-center uk-text-center">
<!-- This is an anchor toggling the modal -->
<a
href="#"
consenty-click="youtube-modal"
class="uk-button uk-button-primary"
>
Play Video
</a>
</div>
</div>
</div>
<div class="uk-margin-small-top">
<div>
Place this on your privacy page so that the user can revoke
consent:
</div>
<label>
<input
type="checkbox"
class="uk-checkbox"
consenty-toggle="youtube-modal"
/>
Allow loading of YouTube videos in modals
</label>
</div>
</div>
<div>
<!-- this is the consent modal asking the user for permission -->
<div
id="yt-modal-consent"
uk-modal
>
<div class="uk-modal-dialog uk-modal-body">
<button
class="uk-modal-close-default"
type="button"
uk-close
></button>
<h2 class="uk-modal-title">YouTube Consent</h2>
<p class="uk-background-muted uk-padding-small">Lorem Ipsum</p>
<p class="uk-flex uk-flex-between">
<button
id="yt-modal-once"
class="uk-button uk-button-default"
type="button"
>
Allow Once
</button>
<button
id="yt-modal-always"
class="uk-button uk-button-primary uk-modal-close"
type="button"
>
Allow Always
</button>
</p>
</div>
</div>
<!-- this shows the video in the iframe -->
<template consenty-if="youtube-modal">
<div
id="yt-modal"
class="uk-flex-top"
uk-modal
>
<div
class="uk-modal-dialog uk-width-auto uk-margin-auto-vertical"
>
<button
class="uk-modal-close-outside"
type="button"
uk-close
></button>
<iframe
src="https://www.youtube.com/embed/ncS36UqaBvc?autoplay=1"
width="1920"
height="1080"
uk-video
uk-responsive
></iframe>
</div>
</div>
</template>
</div>
<hr />
<h2>IF on multiple elements</h2>
<div class="code border uk-margin-small-bottom">
<label>
<input
type="checkbox"
class="uk-checkbox"
consenty-toggle="multiple"
/>
Toggle multiple elements at once
</label>
<template consenty-if="multiple">one</template>
<template consenty-if="multiple">two</template>
<template consenty-if="multiple">three</template>
</div>
<hr />
<h2>Events</h2>
<p>Please check the console of your browser dev tools!</p>
<div class="uk-alert">
All events also have an "before..." event that you can listen to, eg
<em>beforeinit</em>!
</div>
<h3>init</h3>
<script
class="code"
data-lang="js"
>
document.addEventListener("consenty:init", () => {
console.log("consenty initialised");
});
</script>
<h3>allow + revoke</h3>
<script
class="code"
data-lang="js"
>
document.addEventListener("consenty:allow", (e) => {
console.log("user gave consent for " + e.detail);
});
document.addEventListener("consenty:revoke", (e) => {
console.log("user revoked consent for " + e.detail);
});
</script>
<h3>change</h3>
<script
class="code"
data-lang="js"
>
document.addEventListener("consenty:change", (e) => {
console.log(
"user changed consent for " +
e.detail.name +
" from " +
e.detail.from +
" to " +
e.detail.to
);
});
</script>
<hr />
<h2>API Usage</h2>
<p>
When using page transition libraries like barba.js you need to send
pageviews manually to your tracking software. If the user revoke
consent you are not allowed to do so!
</p>
<script
class="code"
data-lang="js"
>
if (consenty.isTrue("matomo-optout")) {
// user chose to opt-out from tracking
// do nothing
} else {
// user allowed to collect statistics
// send pageview event to matomo
_paq.push(["trackPageView"]);
}
</script>
<script type="module">
import { codeToHtml } from "https://esm.sh/[email protected]";
document.querySelectorAll(".code").forEach((element) => {
const div = document.createElement("div");
element.after(div);
// sanitize html
let code = element.innerHTML;
const codeId = element.getAttribute("data-code-id");
if (codeId) {
code = codes[codeId] || code;
}
let lines = code.split("\n").slice(1, -1);
const spaces = lines[0].search(/\S/);
code = lines.map((line) => line.substring(spaces)).join("\n");
// remove all data-consenty-id attributes
code = code.replace(/ data-consenty-id="\d+"/g, "");
// remove all class attributes (they are only for styling)
// only do this if the code element does not have .keepclasses
if (!element.classList.contains("keepclasses")) {
code = code.replace(/ class="[^"]*"/g, "");
}
codeToHtml(code, {
lang: element.getAttribute("data-lang") || "html",
theme: "nord",
})
.then((highlightedCode) => {
div.innerHTML = highlightedCode;
})
.catch((error) => {
console.error("Error highlighting code with Shiki:", error);
});
});
</script>
</template>
</div>
</div>
<div class="uk-text-center uk-text-muted uk-text-small uk-section-small">
built in vienna by <a href="https://www.baumrock.com">baumrock.com</a>
</div>
<script>
// before consenty shows new elements we store the initial code for later
// this is to make sure that the docs' code sections don't show markup
// that was injected by consently and is not the initial markup
var codes = {};
document.addEventListener("consenty:beforeload", (e) => {
let id = e.detail;
let el = consenty.elements[id].old;
let code = el.closest(".code");
if (!code) return;
if (code.hasAttribute("data-code-id")) return;
codes[id] = code.innerHTML;
code.setAttribute("data-code-id", id);
});
</script>
</body>
</html>