Skip to content

Commit

Permalink
add API for consenting #158
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Eberle committed Sep 16, 2020
1 parent d07443c commit b65c800
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 168 deletions.
18 changes: 14 additions & 4 deletions Documentation/Developer/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ cookieman.hide()
Hides the confirmation modal.


cookieman.consent(groupKey)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

:aspect:`Description`
Adds the given group (e.g. 'marketing') to the consented groups, updates the CookieConsent cookie
and injects all items given each corresponding trackingObject's `inject` section.

This is meant as a programmatic way to implement banners before showing content from external sources such as YouTube
videos, Google Maps, facebook posts, ... – clicking the "yes, show the content"-button would call this function and a
`<script>` in trackingObject's `inject` section would take care of actually loading the content.


cookieman.consenteds()
^^^^^^^^^^^^^^^^^^^^^^

Expand All @@ -72,16 +84,14 @@ cookieman.consenteds()
Returns all groups keys the user has consented to.


cookieman.hasConsented(selection)
cookieman.hasConsented(groupKey)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

:aspect:`Data type`
boolean

:aspect:`Description`
Returns `true` if the user has consented to the given selection, else false.

A selection is any name of a checkbox in the popup, e.g. 'marketing'.
Returns `true` if the user has consented to the given group (e.g. 'marketing'), else false.


cookieman.hasConsentedTrackingObject(trackingObjectKey)
Expand Down
10 changes: 10 additions & 0 deletions Resources/Public/Js/cookieman.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,16 @@ var cookieman = (function () {
* @api
*/
consenteds: consentedSelectionsRespectDnt,
/**
* @api
* @param {string} groupKey
*/
consent: function (groupKey) {
var checkbox = form.querySelector('[type=checkbox][name="' + groupKey + '"]')
setChecked(checkbox, true)
saveSelections()
injectNewTrackingObjects()
},
/**
* @api
* @param {string} trackingObjectKey
Expand Down
2 changes: 1 addition & 1 deletion Resources/Public/Js/cookieman.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

163 changes: 0 additions & 163 deletions Resources/Public/Js/js.cookie.min.js
Original file line number Diff line number Diff line change
@@ -1,163 +0,0 @@
/*!
* JavaScript Cookie v2.2.1
* https://github.com/js-cookie/js-cookie
*
* Copyright 2006, 2015 Klaus Hartl & Fagner Brack
* Released under the MIT license
*/
;(function (factory) {
var registeredInModuleLoader;
if (typeof define === 'function' && define.amd) {
define(factory);
registeredInModuleLoader = true;
}
if (typeof exports === 'object') {
module.exports = factory();
registeredInModuleLoader = true;
}
if (!registeredInModuleLoader) {
var OldCookies = window.Cookies;
var api = window.Cookies = factory();
api.noConflict = function () {
window.Cookies = OldCookies;
return api;
};
}
}(function () {
function extend () {
var i = 0;
var result = {};
for (; i < arguments.length; i++) {
var attributes = arguments[ i ];
for (var key in attributes) {
result[key] = attributes[key];
}
}
return result;
}

function decode (s) {
return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
}

function init (converter) {
function api() {}

function set (key, value, attributes) {
if (typeof document === 'undefined') {
return;
}

attributes = extend({
path: '/'
}, api.defaults, attributes);

if (typeof attributes.expires === 'number') {
attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5);
}

// We're using "expires" because "max-age" is not supported by IE
attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';

try {
var result = JSON.stringify(value);
if (/^[\{\[]/.test(result)) {
value = result;
}
} catch (e) {}

value = converter.write ?
converter.write(value, key) :
encodeURIComponent(String(value))
.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);

key = encodeURIComponent(String(key))
.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
.replace(/[\(\)]/g, escape);

var stringifiedAttributes = '';
for (var attributeName in attributes) {
if (!attributes[attributeName]) {
continue;
}
stringifiedAttributes += '; ' + attributeName;
if (attributes[attributeName] === true) {
continue;
}

// Considers RFC 6265 section 5.2:
// ...
// 3. If the remaining unparsed-attributes contains a %x3B (";")
// character:
// Consume the characters of the unparsed-attributes up to,
// not including, the first %x3B (";") character.
// ...
stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
}

return (document.cookie = key + '=' + value + stringifiedAttributes);
}

function get (key, json) {
if (typeof document === 'undefined') {
return;
}

var jar = {};
// To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all.
var cookies = document.cookie ? document.cookie.split('; ') : [];
var i = 0;

for (; i < cookies.length; i++) {
var parts = cookies[i].split('=');
var cookie = parts.slice(1).join('=');

if (!json && cookie.charAt(0) === '"') {
cookie = cookie.slice(1, -1);
}

try {
var name = decode(parts[0]);
cookie = (converter.read || converter)(cookie, name) ||
decode(cookie);

if (json) {
try {
cookie = JSON.parse(cookie);
} catch (e) {}
}

jar[name] = cookie;

if (key === name) {
break;
}
} catch (e) {}
}

return key ? jar[key] : jar;
}

api.set = set;
api.get = function (key) {
return get(key, false /* read as raw */);
};
api.getJSON = function (key) {
return get(key, true /* read as json */);
};
api.remove = function (key, attributes) {
set(key, '', extend(attributes, {
expires: -1
}));
};

api.defaults = {};

api.withConverter = init;

return api;
}

return init(function () {});
}));

0 comments on commit b65c800

Please sign in to comment.