Fix getCookie to handle n8n first cookie without semicolon prefix
This commit is contained in:
+11
-19
@@ -8,35 +8,27 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
|
||||
function getCookie(name) {
|
||||
console.log('DEBUG getCookie: all cookies =', document.cookie);
|
||||
// Try multiple parsing strategies
|
||||
const raw = document.cookie;
|
||||
|
||||
// Strategy 1: standard split
|
||||
// Strategy 1: semicolon prefix (cookies after first)
|
||||
const parts1 = raw.split(`; ${name}=`);
|
||||
if (parts1.length === 2) {
|
||||
const val = parts1.pop().split(';').shift();
|
||||
console.log('DEBUG getCookie: strategy1 result =', val, '(cleaned: ' + val.replace(/^"|"$/g, '') + ')');
|
||||
return val.replace(/^"|"$/g, '');
|
||||
const cleaned = val.replace(/^"|"$/g, '');
|
||||
console.log('DEBUG getCookie:', name, '=', cleaned);
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
// Strategy 2: with quotes (n8n sets cookies quoted)
|
||||
const parts2 = raw.split(`${name}="`);
|
||||
// Strategy 2: no semicolon prefix (FIRST cookie in document.cookie)
|
||||
const parts2 = raw.split(`${name}=`);
|
||||
if (parts2.length === 2) {
|
||||
const val = parts2.pop().split('"').shift();
|
||||
console.log('DEBUG getCookie: strategy2 (quoted) result =', val);
|
||||
return val;
|
||||
const val = parts2.pop().split(';').shift();
|
||||
const cleaned = val.replace(/^"|"$/g, '');
|
||||
console.log('DEBUG getCookie:', name, '=', cleaned, '(first cookie)');
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
// Strategy 3: regex
|
||||
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');
|
||||
console.log('DEBUG getCookie:', name, 'NOT FOUND');
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user