I need to create a comprehensive list of cookies along with their values, retrieve the values, and then store them in a different location
var cookieNames = ["Cookie1", "Cookie2", "Cookie3"];
function getCookie(cookieName) {
let name = cookieName + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let cookieArray = decodedCookie.split(';');
let cookieValue = [];
for(let i = 0; i < cookieArray.length; i++) {
let cookie = cookieArray[i];
while (cookie.charAt(0) == ' ') {
cookie = cookie.substring(1);
}
if (cookie.indexOf(name) == 0) {
cookieValue = [cookie.substring(name.length, cookie.length)];
// Attempting here to generate a key-value pair list
cookiesList +=[cookieName:cookie.value];
}
}
}
// Retrieve all cookie names from the list
for (var key in cookieNames) {
getCookie(cookieName);
}
// exportsTest~ Exporting the variable for testing purposes on Testim platform
exportsTest.cookiesList;
Once I have collected the cookie names and their respective values, I wish to set them at a separate location
function setCookie(cookieName, cookieValue, expiryDays) {
const currentDate = new Date();
currentDate.setTime(currentDate.getTime() + (expiryDays*24*60*60*1000));
let expires = "expires="+ currentDate.toUTCString();
document.cookie = cookieName + "=" + cookieValue + ";" + expires + ";path=/";
}
// Setting all the cookies from the list
for (var key in cookiesList) {
setCookie(key, cookiesList[key], 120);
}
Is there something missing that could prevent this from functioning properly?