forked from diegolamonica/EUCookieLaw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEUCookieLaw.js
146 lines (130 loc) · 4.39 KB
/
EUCookieLaw.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
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
/**
* EUCookieLaw: simple object to accomplish european law requirements about cookie transmission to clients
* @class EUCookieLaw
* @version 1.3.1
* @link https://github.com/diegolamonica/EUCookieLaw/
* @author Diego La Monica (diegolamonica) <[email protected]>
* @copyright 2015 Diego La Monica
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html GNU Lesser General Public License
* @note This program is distributed in the hope that it will be useful - WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
var EUCookieLaw = (function (doc) {
var setProperty = function (object, prop, _set, _get) {
if (typeof(Object.defineProperty) === 'function') {
var propObject = {
configurable: true
};
if (typeof(_set) == 'function') propObject['set'] = _set;
if (typeof(_get) == 'function') propObject['get'] = _get;
Object.defineProperty(object, prop, propObject);
} else {
if (typeof(_set) == 'function') object.__defineSetter__(prop, _set);
if (typeof(_get) == 'function') object.__defineGetter__(prop, _get);
}
},
cookieEnabled = false,
isOriginal = true,
instance = null,
cookieRejected = false,
askForCookie = null,
showBanner = false,
defaultTitle = '',
disagreeLabel = '',
agreeLabel = '',
defaultMessage = '',
reloadAfterAgree = false,
theBannerId = '',
theTitleTag = '', // default is h1
body;
return function EUCookieLaw(options) {
if (/__eucookielaw=true/.test(doc.cookie)) return instance;
if (instance instanceof EUCookieLaw) return instance;
instance = this;
this.enableCookies = function () {
cookieEnabled = true;
cookieRejected = false;
if (!isOriginal) {
delete doc.cookie;
isOriginal = true;
}
doc.cookie = "__eucookielaw=true"
+ ";domain=" + window.location.host + ";path=/";
removeBanner();
if(reloadAfterAgree) window.location.reload();
};
this.reject = function () {
cookieRejected = true;
removeBanner();
};
this.isRejected = function () {
return cookieRejected;
};
this.isCookieEnabled = function () {
return cookieEnabled;
};
var buildBanner = function () {
body = doc.body;
if(theBannerId!='' && doc.getElementById(theBannerId)){
}else {
var theDiv = doc.createElement("div");
theBannerId = 'eucookielaw-' + parseInt(Math.random() * 200);
theDiv.setAttribute('id', theBannerId);
theDiv.className = "eucookielaw-banner";
theDiv.innerHTML = '<div class="well">' +
'<' + theTitleTag + ' class="banner-title">' + defaultTitle + '</' + theTitleTag + '>' +
'<p class="banner-message">' + defaultMessage + '</p>' +
'<p class="banner-agreement-buttons">' +
((disagreeLabel!= '') ? '<a href="#" class="disagree-button btn btn-danger" onclick="(new EUCookieLaw()).reject();">' + disagreeLabel + '</a>' : '') +
'<a href="#" class="agree-button btn btn-primary" onclick="(new EUCookieLaw()).enableCookies();">' + agreeLabel + '</a>'+
'</p>' +
'</div>';
var firstNode = body.childNodes[0];
body.insertBefore(theDiv, firstNode);
}
},
removeBanner = function(){
if(theBannerId!=''){
body.removeChild( doc.getElementById(theBannerId) );
theBannerId = '';
}
};
theTitleTag = options.tag || 'h1';
defaultMessage = options.message || 'La legge europea sulla privacy e la conservazione dei cookie richiede il tuo consenso prima di conservare i cookie. Me lo consenti?';
reloadAfterAgree = options.reload;
askForCookie = typeof(options.showAgreement) == 'function' ? options.showAgreement : function () {
if(showBanner) {
buildBanner();
}else {
if (!instance.isRejected() && confirm(options.message)) {
instance.enableCookies();
} else {
cookieRejected = true;
}
}
return instance.isCookieEnabled();
};
showBanner = options.showBanner;
if(showBanner && options.bannerTitle){
defaultTitle = options.bannerTitle;
disagreeLabel = options.disagreeLabel || "";
agreeLabel = options.agreeLabel || "Agree";
window.addEventListener('load', buildBanner);
}
isOriginal = false;
setProperty(doc, 'cookie', function (cookie) {
body = doc.body;
if (!cookieEnabled) {
if (askForCookie()) {
instance.enableCookies();
doc.cookie = cookie;
}
return false;
} else {
delete doc.cookie;
doc.cooke = cookie;
}
return cookie;
}, null);
};
})(document);