From 58dc9e4206882723ee92740ef3ee6e1ef1428d8a Mon Sep 17 00:00:00 2001 From: Kato Date: Fri, 4 Sep 2026 16:37:11 +0000 Subject: [PATCH] Fix getCookie to handle n8n first cookie without semicolon prefix --- js/main.js | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/js/main.js b/js/main.js index c09c2d6..e841089 100644 --- a/js/main.js +++ b/js/main.js @@ -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 ''; }