rc-consent
Version:
Cookie consent a Vanilla JS plugin which meets General Data Protection Regulations (GDPR).
1 lines • 5.54 kB
JavaScript
!function(c){"use strict";if(!c.hasInitialised){var a={escapeRegExp:function(t){return t.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")},hasClass:function(t,e){var n=" ";return 1===t.nodeType&&0<=(n+t.className+n).replace(/[\n\t]/g,n).indexOf(n+e+n)},addClass:function(t,e){t.className+=" "+e},removeClass:function(t,e){var n=new RegExp("\\b"+this.escapeRegExp(e)+"\\b");t.className=t.className.replace(n,"")},interpolateString:function(t,e){return t.replace(/{{([a-z][a-z0-9\-_]*)}}/gi,function(t){return e(arguments[1])||""})},getCookie:function(t){var e="; "+document.cookie,n=e.split("; "+t+"=");e=n.length<2?void 0:n.pop().split(";").shift();try{e=JSON.parse(decodeURIComponent(e))}catch(t){}return e},setCookie:function(t,e,n,i,o,s){var a=new Date;a.setDate(a.getDate()+(n||365)),this.isPlainObject(e)&&(e=JSON.stringify(e));var r=[t+"="+(e=encodeURIComponent(e)),"expires="+a.toUTCString(),"path="+(o||"/")];i&&r.push("domain="+i),s&&r.push("secure"),document.cookie=r.join(";")},deepExtend:function(t,e){for(var n in e)e.hasOwnProperty(n)&&(n in t&&this.isPlainObject(t[n])&&this.isPlainObject(e[n])?this.deepExtend(t[n],e[n]):t[n]=e[n]);return t},throttle:function(t,e){var n=!1;return function(){n||(t.apply(this,arguments),n=!0,setTimeout(function(){n=!1},e))}},hash:function(t){var e,n,i=0;if(0===t.length)return i;for(e=0,n=t.length;e<n;++e)i=(i<<5)-i+t.charCodeAt(e),i|=0;return 268435455&i},normaliseHex:function(t){return"#"===t[0]&&(t=t.substr(1)),3===t.length&&(t=t[0]+t[0]+t[1]+t[1]+t[2]+t[2]),t},getContrast:function(t){return t=this.normaliseHex(t),128<=(299*parseInt(t.substr(0,2),16)+587*parseInt(t.substr(2,2),16)+114*parseInt(t.substr(4,2),16))/1e3?"#000":"#fff"},getLuminance:function(t){var e=parseInt(this.normaliseHex(t),16),n=38+(e>>16),i=38+(e>>8&255),o=38+(255&e);return"#"+(16777216+65536*(n<255?n<1?0:n:255)+256*(i<255?i<1?0:i:255)+(o<255?o<1?0:o:255)).toString(16).slice(1)},isMobile:function(){return/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)},isPlainObject:function(t){return"object"==typeof t&&null!==t&&t.constructor===Object},traverseDOMPath:function(t,e){return t&&t.parentNode?util.hasClass(t,e)?t:this.traverseDOMPath(t.parentNode,e):null}},u=[],h={},n={id:null,trackingId:null,category:"analytics",onInitialise:function(t,e){},onAllow:function(t){},onRevoke:function(t){},onStatusChange:function(t,e,n){}},r={cookie:{name:"rcc_consent",domain:"",path:"/",days:7},categories:["analytics"]},l=function(t,e){e||"submit"!==t.type?c.setConsent(e):c.setConsent(t.target)};c={initialise:function(t){a.deepExtend(this.options={},r),a.isPlainObject(t)&&a.deepExtend(this.options,t),window.document.addEventListener("rccSetConsent",l);var e,n=[];a.isPlainObject(this.options.defaultStatus)&&(n=Object.keys(this.options.defaultStatus));for(var i=0;i<u.length;i++)e=u[i].category||"analytics",-1===n.indexOf(e)&&n.push(e),0<=this.options.categories.indexOf(e)&&this.options.categories.splice(this.options.categories.indexOf(e),1);this.options.categories=n.concat(this.options.categories);for(var o=this.getStatus(),s=0;s<u.length;s++)e=u[s].category,u[s].onInitialise.call(u[s],this,o);h=o},addProvider:function(t){if(!t.id||""===t.id)return!1;var e={};return a.deepExtend(e,n),a.isPlainObject(t)&&a.deepExtend(e,t),u.push(e),!0},getProviders:function(){return u},setConsent:function(t){if(t instanceof Element||t instanceof HTMLDocument){var e=t.querySelectorAll("input[name]");if(t=void 0,e.length){t={};for(var n=0;n<e.length;n++)"checkbox"===e[n].type&&0<=c.options.categories.indexOf(e[n].name)&&(t[e[n].name]=e[n].checked)}}var i=this.hasConsented();this.setStatus(t);var o=this.getStatus(),s=!1;JSON.stringify(o)!==JSON.stringify(h)&&(s=!0);for(var a=0;a<u.length;a++){var r=u[a].category;s&&u[a].onStatusChange.call(u[a],this,o,h),i&&o[r]===h[r]||(o[r]?u[a].onAllow.call(u[a],this):u[a].onRevoke.call(u[a],this))}h=o},getStatus:function(){var t=c.options.cookie,e=a.getCookie(t.name),n=a.getCookie(t.name+"_hash"),i=this.getDefaultStatus();return o(i,e),JSON.stringify(e)!==JSON.stringify(i)&&a.setCookie(t.name,i,0,t.domain,t.path),n!==a.hash(JSON.stringify(i))&&a.setCookie(this.options.cookie.name+"_hash","",-1,t.domain,t.path),i},setStatus:function(t){var e=this.options.cookie,n=this.getDefaultStatus();a.isPlainObject(t)||(t=a.getCookie(e.name)),t=o(n,t);var i=a.hash(JSON.stringify(t));a.setCookie(e.name,t,0,e.domain,e.path),a.setCookie(e.name+"_hash",i,e.days,e.domain,e.path)},clearStatus:function(){var t=this.options.cookie;a.setCookie(t.name,"",-1,t.domain,t.path),a.setCookie(t.name+"_hash","",-1,t.domain,t.path),h=this.getDefaultStatus()},getDefaultStatus:function(){for(var t=a.isPlainObject(this.options.defaultStatus)?this.options.defaultStatus:{},e={},n=0;n<this.options.categories.length;n++){var i=t[this.options.categories[n]];e[this.options.categories[n]]=!0===i}return e},hasConsented:function(t){var e=this.getStatus();return!!a.getCookie(this.options.cookie.name+"_hash")&&(!t||""===t||!!e[t])},setForm:function(t){if(t instanceof Element||t instanceof HTMLDocument){var e=this.getStatus(),n=t.querySelectorAll("input[name]");if(n.length)for(var i=0;i<n.length;i++)"checkbox"===n[i].type&&0<=c.options.categories.indexOf(n[i].name)&&(n[i].checked=!0===e[n[i].name])}},destroy:function(){window.document.removeEventListener("rccSetConsent",l)},hasInitialised:!0},window.rcc=c}function o(t,e){if(a.isPlainObject(e))for(var n in t)t.hasOwnProperty(n)&&(!0!==e[n]&&!1!==e[n]||(t[n]=e[n]));return t}}(window.rcc||{});