|
async function getCookieReport() { |
|
// Helper: human-friendly duration |
|
function formatDuration(ms) { |
|
const seconds = Math.floor(ms / 1000); |
|
const minutes = Math.floor(seconds / 60); |
|
const hours = Math.floor(minutes / 60); |
|
const days = Math.floor(hours / 24); |
|
const months = Math.floor(days / 30); |
|
|
|
if (months >= 1) return months === 1 ? "1 month" : `${months} months`; |
|
if (days >= 1) return days === 1 ? "1 day" : `${days} days`; |
|
if (hours >= 1) return hours === 1 ? "1 hour" : `${hours} hours`; |
|
if (minutes >= 1) return minutes === 1 ? "1 minute" : `${minutes} minutes`; |
|
return seconds <= 1 ? "1 second" : `${seconds} seconds`; |
|
} |
|
|
|
let cookies = []; |
|
|
|
if ('cookieStore' in window) { |
|
// Use CookieStore API (Chrome/Edge) |
|
const allCookies = await cookieStore.getAll(); |
|
cookies = allCookies.map(cookie => { |
|
let duration; |
|
|
|
if (cookie.expires) { |
|
const diffMs = new Date(cookie.expires) - new Date(); |
|
duration = diffMs > 0 ? formatDuration(diffMs) : "Expired"; |
|
} else { |
|
duration = "Session"; // session cookie |
|
} |
|
|
|
return { |
|
name: cookie.name, |
|
value: cookie.value, |
|
domain: cookie.domain, |
|
path: cookie.path, |
|
sameSite: cookie.sameSite, |
|
secure: cookie.secure, |
|
httpOnly: cookie.httpOnly, |
|
partitioned: cookie.partitioned, |
|
duration |
|
}; |
|
}); |
|
} else { |
|
// Firefox/Safari fallback: only name & value |
|
console.warn("CookieStore API not supported. Reporting only name and value."); |
|
cookies = document.cookie |
|
.split("; ") |
|
.filter(Boolean) |
|
.map(str => { |
|
const [name, value] = str.split("="); |
|
return { name, value }; |
|
}); |
|
} |
|
|
|
return { cookies }; // return as an object |
|
} |