-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.js
69 lines (51 loc) · 2.32 KB
/
widget.js
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
const widgetDomain = 'https://chezzyderp.github.io/cookie-widget/'
// Get cookie value by name
const getCookieByName = (name) =>{
let matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : undefined;
}
const setCookieSkipped = () => {
const futureDate = new Date()
const time = futureDate.getTime()
const expireTime = time + 1000 * 36000
futureDate.setTime(expireTime)
const expires = futureDate.toUTCString()
let updatedCookie = `cookiesWidgetSkipped=true;expires=${expires}`
// Set cookie and remove notification
document.cookie = updatedCookie;
document.getElementsByClassName('cookie_widget')[0].remove()
}
cookiesWidgetSkipped = getCookieByName('cookiesWidgetSkipped')
// If script has a every_active prop
const every_active = document.currentScript.getAttribute('every_active')
// Script will ignore cookies and every show notification
every_active != null ? cookiesWidgetSkipped = false : null
if(!cookiesWidgetSkipped){
const text = document.currentScript.getAttribute('text')
const every_active = document.currentScript.getAttribute('text')
// Create accept button
_acceptButton = document.createElement('button')
_acceptButton.innerText = 'Accept'
_acceptButton.onclick = () => setCookieSkipped()
// Write widget layout
document.write(`
<div class="cookie_widget">
<div class="cookie_widget_wrap">
<img src="${widgetDomain}/cookie.svg" alt="">
<div class="cookie_widget_texts">
<p class="cookie_widget_title">
Cookie!
</p>
<p class="cookie_widget_text">
${text || `We use cookies and other tracking technologies to improve your browsing experience on our website,
to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from. `}
</p>
</div>
</div>
</div>
`)
// Append button to widget layout
document.getElementsByClassName('cookie_widget')[0].appendChild(_acceptButton)
}