Fix getCookie to handle n8n first cookie without semicolon prefix

This commit is contained in:
Kato
2026-09-04 16:37:11 +00:00
parent 2adaefd556
commit 58dc9e4206
+11 -19
View File
@@ -8,35 +8,27 @@ document.addEventListener('DOMContentLoaded', () => {
function getCookie(name) { function getCookie(name) {
console.log('DEBUG getCookie: all cookies =', document.cookie); console.log('DEBUG getCookie: all cookies =', document.cookie);
// Try multiple parsing strategies
const raw = document.cookie; const raw = document.cookie;
// Strategy 1: standard split // Strategy 1: semicolon prefix (cookies after first)
const parts1 = raw.split(`; ${name}=`); const parts1 = raw.split(`; ${name}=`);
if (parts1.length === 2) { if (parts1.length === 2) {
const val = parts1.pop().split(';').shift(); const val = parts1.pop().split(';').shift();
console.log('DEBUG getCookie: strategy1 result =', val, '(cleaned: ' + val.replace(/^"|"$/g, '') + ')'); const cleaned = val.replace(/^"|"$/g, '');
return val.replace(/^"|"$/g, ''); console.log('DEBUG getCookie:', name, '=', cleaned);
return cleaned;
} }
// Strategy 2: with quotes (n8n sets cookies quoted) // Strategy 2: no semicolon prefix (FIRST cookie in document.cookie)
const parts2 = raw.split(`${name}="`); const parts2 = raw.split(`${name}=`);
if (parts2.length === 2) { if (parts2.length === 2) {
const val = parts2.pop().split('"').shift(); const val = parts2.pop().split(';').shift();
console.log('DEBUG getCookie: strategy2 (quoted) result =', val); const cleaned = val.replace(/^"|"$/g, '');
return val; console.log('DEBUG getCookie:', name, '=', cleaned, '(first cookie)');
return cleaned;
} }
// Strategy 3: regex console.log('DEBUG getCookie:', name, 'NOT FOUND');
const regex = new RegExp(`(?:^|;)\\s*${name}=([^;]*)`);
const match = raw.match(regex);
if (match && match[1]) {
const val = match[1].trim().replace(/^"|"$/g, '');
console.log('DEBUG getCookie: strategy3 (regex) result =', val);
return val;
}
console.log('DEBUG getCookie: NOT FOUND');
return ''; return '';
} }