231 lines
8.1 KiB
JavaScript
231 lines
8.1 KiB
JavaScript
/**
|
|
* GDPR Cookie Consent Module for PrestaShop
|
|
*
|
|
* @author Walzen665
|
|
* @copyright Copyright (c) 2025
|
|
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
|
|
*/
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Cookie functions
|
|
function setCookie(name, value, days) {
|
|
var expires = '';
|
|
if (days) {
|
|
var date = new Date();
|
|
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
|
|
expires = '; expires=' + date.toUTCString();
|
|
}
|
|
document.cookie = name + '=' + (value || '') + expires + '; path=/; SameSite=Lax';
|
|
}
|
|
|
|
function getCookie(name) {
|
|
var nameEQ = name + '=';
|
|
var ca = document.cookie.split(';');
|
|
for (var i = 0; i < ca.length; i++) {
|
|
var c = ca[i];
|
|
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
|
|
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Show manage preferences button when cookies are already set
|
|
function showManageButton() {
|
|
document.getElementById('gdpr-cookie-manage-button').style.display = 'block';
|
|
}
|
|
|
|
// After user consents, hide banner and modal and show the manage button
|
|
function afterConsent() {
|
|
// Hide banner and modal
|
|
hideModal();
|
|
document.getElementById('gdpr-cookie-banner').style.display = 'none';
|
|
|
|
// Show manage button
|
|
showManageButton();
|
|
}
|
|
|
|
// Check if cookie consent is already set
|
|
var cookieConsent = getCookie('gdpr_cookie_consent');
|
|
|
|
if (!cookieConsent) {
|
|
// Show the cookie banner if consent is not set
|
|
document.getElementById('gdpr-cookie-banner').style.display = 'block';
|
|
} else {
|
|
// Apply cookie preferences
|
|
var consentPreferences = JSON.parse(cookieConsent);
|
|
applyConsentPreferences(consentPreferences);
|
|
|
|
// Show the manage button since consent was already given
|
|
showManageButton();
|
|
}
|
|
|
|
// Banner buttons
|
|
document.getElementById('gdpr-cookie-banner-accept').addEventListener('click', function() {
|
|
acceptAllCookies();
|
|
});
|
|
|
|
document.getElementById('gdpr-cookie-banner-decline').addEventListener('click', function() {
|
|
declineAllCookies();
|
|
});
|
|
|
|
document.getElementById('gdpr-cookie-banner-settings').addEventListener('click', function() {
|
|
showModal();
|
|
});
|
|
|
|
// Modal buttons
|
|
document.getElementById('gdpr-cookie-accept-all').addEventListener('click', function() {
|
|
acceptAllCookies();
|
|
});
|
|
|
|
document.getElementById('gdpr-cookie-decline').addEventListener('click', function() {
|
|
declineAllCookies();
|
|
});
|
|
|
|
document.getElementById('gdpr-cookie-accept-selected').addEventListener('click', function() {
|
|
acceptSelectedCookies();
|
|
});
|
|
|
|
// Manage button (for after consent is given)
|
|
document.getElementById('gdpr-cookie-manage').addEventListener('click', function() {
|
|
showModal();
|
|
});
|
|
|
|
// Functions
|
|
function showModal() {
|
|
document.getElementById('gdpr-cookie-banner').style.display = 'none';
|
|
document.getElementById('gdpr-cookie-modal').style.display = 'block';
|
|
|
|
// Load saved preferences if they exist
|
|
var cookieConsent = getCookie('gdpr_cookie_consent');
|
|
if (cookieConsent) {
|
|
var preferences = JSON.parse(cookieConsent);
|
|
|
|
// Set checkboxes based on saved preferences
|
|
document.querySelectorAll('.gdpr-cookie-checkbox').forEach(function(checkbox) {
|
|
var category = checkbox.getAttribute('data-cookie-category');
|
|
if (preferences[category]) {
|
|
checkbox.checked = true;
|
|
} else {
|
|
checkbox.checked = false;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function hideModal() {
|
|
document.getElementById('gdpr-cookie-modal').style.display = 'none';
|
|
}
|
|
|
|
function acceptAllCookies() {
|
|
var preferences = {
|
|
necessary: true,
|
|
functional: true,
|
|
analytics: true,
|
|
marketing: true
|
|
};
|
|
|
|
setCookie('gdpr_cookie_consent', JSON.stringify(preferences), 365);
|
|
applyConsentPreferences(preferences);
|
|
afterConsent(); // Use the new function for consistent post-consent behavior
|
|
}
|
|
|
|
function declineAllCookies() {
|
|
var preferences = {
|
|
necessary: true, // Necessary cookies are always accepted
|
|
functional: false,
|
|
analytics: false,
|
|
marketing: false
|
|
};
|
|
|
|
setCookie('gdpr_cookie_consent', JSON.stringify(preferences), 365);
|
|
applyConsentPreferences(preferences);
|
|
afterConsent(); // Use the new function for consistent post-consent behavior
|
|
}
|
|
|
|
function acceptSelectedCookies() {
|
|
var preferences = {
|
|
necessary: true // Necessary cookies are always accepted
|
|
};
|
|
|
|
// Get selected preferences
|
|
document.querySelectorAll('.gdpr-cookie-checkbox').forEach(function(checkbox) {
|
|
var category = checkbox.getAttribute('data-cookie-category');
|
|
preferences[category] = checkbox.checked;
|
|
});
|
|
|
|
setCookie('gdpr_cookie_consent', JSON.stringify(preferences), 365);
|
|
applyConsentPreferences(preferences);
|
|
afterConsent(); // Use the new function for consistent post-consent behavior
|
|
}
|
|
|
|
function applyConsentPreferences(preferences) {
|
|
// Example implementation
|
|
// You would need to adapt this based on your specific cookie usage
|
|
|
|
// Functional cookies
|
|
if (!preferences.functional) {
|
|
// Disable functional cookies
|
|
removeFunctionalCookies();
|
|
}
|
|
|
|
// Analytics cookies
|
|
if (!preferences.analytics) {
|
|
// Disable analytics cookies (like Google Analytics)
|
|
disableAnalytics();
|
|
} else {
|
|
// Enable analytics
|
|
enableAnalytics();
|
|
}
|
|
|
|
// Marketing cookies
|
|
if (!preferences.marketing) {
|
|
// Disable marketing cookies
|
|
disableMarketing();
|
|
} else {
|
|
// Enable marketing
|
|
enableMarketing();
|
|
}
|
|
}
|
|
|
|
// Helper functions to implement consent preferences
|
|
function removeFunctionalCookies() {
|
|
// This is just an example - implement based on your specific needs
|
|
var functionalCookies = ['prefs', 'language', 'theme'];
|
|
functionalCookies.forEach(function(cookie) {
|
|
document.cookie = cookie + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
|
|
});
|
|
}
|
|
|
|
function disableAnalytics() {
|
|
// Example: Disable Google Analytics
|
|
window['ga-disable-UA-XXXXXXXX-X'] = true;
|
|
|
|
// Remove existing GA cookies
|
|
document.cookie = '_ga=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Domain=.' + window.location.hostname;
|
|
document.cookie = '_gid=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Domain=.' + window.location.hostname;
|
|
document.cookie = '_gat=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Domain=.' + window.location.hostname;
|
|
}
|
|
|
|
function enableAnalytics() {
|
|
// Example: Enable Google Analytics
|
|
window['ga-disable-UA-XXXXXXXX-X'] = false;
|
|
}
|
|
|
|
function disableMarketing() {
|
|
// Example implementation for common marketing cookies
|
|
var marketingCookies = ['_fbp', 'fr', 'IDE', 'MUID', 'personalization_id'];
|
|
var domains = [window.location.hostname, '.' + window.location.hostname];
|
|
|
|
marketingCookies.forEach(function(cookie) {
|
|
domains.forEach(function(domain) {
|
|
document.cookie = cookie + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Domain=' + domain;
|
|
});
|
|
});
|
|
}
|
|
|
|
function enableMarketing() {
|
|
// For enabling marketing, we typically don't need to do anything special
|
|
// The marketing scripts will set their cookies when they load
|
|
// Just ensure the marketing scripts are loaded after checking consent
|
|
}
|
|
}); |