1import { Controller } from '@hotwired/stimulus'; 2import $ from 'jquery'; 3 4export default class extends Controller { 5 connect() { 6 // Didomi script to remove cookies on consent changing 7 (function () { 8 /** 9 * Ajouter ici le nom des cookies fonctionnels du site 10 */ 11 let itemsToKeep = ["euconsent-v2", "didomi_token"]; 12 /** 13 * Returns cookie value 14 */ 15 let getCookieValue = function (cookieName) { 16 let cookie = document.cookie.split(";").filter(function (cookieValue) { 17 return cookieValue.indexOf(cookieName) !== -1; 18 })[0]; 19 if (cookie) { 20 return cookie.split("=")[1]; 21 } 22 }; 23 let deleteCookie = function (name, domain, path) { 24 path = path || "/"; 25 let cookie = [ 26 name + "=", 27 "expires=Thu, 01 Jan 1970 00:00:01 GMT", 28 "path=" + path, 29 ]; 30 if (domain) { 31 cookie.push("domain=" + domain); 32 } 33 document.cookie = cookie.join(";"); 34 }; 35 /** 36 * Check if all vendor and purposes are disabled 37 */ 38 let areAllVendorsAndPurposesDisabled = function () { 39 let enabledEntities = []; 40 let disabledEntities = []; 41 let data = window.Didomi.getUserStatus(); 42 data.vendors.consent.enabled.forEach(function (entity) { 43 enabledEntities.push(entity); 44 }); 45 data.purposes.consent.enabled.forEach(function (entity) { 46 enabledEntities.push(entity); 47 }); 48 49 data.vendors.consent.disabled.forEach(function (entity) { 50 disabledEntities.push(entity); 51 }); 52 53 data.purposes.consent.disabled.forEach(function (entity) { 54 disabledEntities.push(entity); 55 }); 56 /** 57 * We check that we don't have any enabled entities 58 * and that disabled entities are present 59 */ 60 return enabledEntities.length === 0 && disabledEntities.length > 0; 61 }; 62 63 let consentEventsCount = 0; 64 let existingConsentString = getCookieValue("euconsent-v2"); 65 window.didomiEventListeners = window.didomiEventListeners || []; 66 window.didomiEventListeners.push({ 67 event: "consent.changed", 68 listener: function () { 69 /** 70 * We catch consent update event in 2 cases: 71 * -> 1. When user gives consent and updates it without the page reload (via `consentEventsCount` value) 72 * -> 2. When user gives consent and updates it after the page reload (via `existingConsentString` value) 73 */ 74 let consentUpdate = 75 consentEventsCount > 0 ? true : !!existingConsentString; 76 if (consentUpdate && areAllVendorsAndPurposesDisabled()) { 77 /** 78 * Consent has been given previously and this is a consent update 79 */ 80 let cookiesToDelete = document.cookie 81 .split(";") 82 .map(function (cookie) { 83 return cookie.split("=")[0].trim(); 84 }) 85 .filter(function (cookieName) { 86 return itemsToKeep.indexOf(cookieName) === -1; 87 }); 88 /** 89 * Delete cookies 90 */ 91 cookiesToDelete.forEach(function (cookieToDelete) { 92 let domains = ('.#' + document.location.host.replace('.', '#.#')).split('#'); 93 while (domains.length) { 94 let possibleDomain = domains.join(''); 95 deleteCookie(cookieToDelete, possibleDomain); 96 domains.shift(); 97 } 98 }); 99 let localStorageItemsToDelete = Object.keys(window.localStorage).filter( 100 function (localStorageItemName) { 101 return itemsToKeep.indexOf(localStorageItemName) === -1; 102 } 103 ); 104 /** 105 * Delete local storage items 106 */ 107 localStorageItemsToDelete.forEach(function (localStorageItemName) { 108 window.localStorage.removeItem(localStorageItemName) 109 }); 110 // Reload the page 111 window.location.reload(); 112 } 113 consentEventsCount++; 114 }, 115 }); 116 })(); 117 } 118}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.