diff --git a/lib/form_filler.mjs b/lib/form_filler.mjs index 05ee69c..0fbfd09 100644 --- a/lib/form_filler.mjs +++ b/lib/form_filler.mjs @@ -56,11 +56,18 @@ function extractLabel(node) { raw = raw.replace(/\s+/g, ' ').replace(/\s*\*\s*$/, '').replace(/\s*Required\s*$/i, '').trim(); // Deduplicate repeated label text (LinkedIn renders label text twice) if (raw.length > 8) { - for (let len = Math.ceil(raw.length / 2); len >= 4; len--) { - const candidate = raw.slice(0, len); - if (raw.startsWith(candidate + candidate)) { - raw = candidate.trim(); - break; + const half = Math.ceil(raw.length / 2); + const firstHalf = raw.slice(0, half).trim(); + const secondHalf = raw.slice(half).trim(); + if (firstHalf === secondHalf) { + raw = firstHalf; + } else { + // Also check with space separator: "ABC ABC" -> "ABC" + const spaceIdx = raw.indexOf(' ', Math.floor(raw.length / 2) - 2); + if (spaceIdx > 0) { + const left = raw.slice(0, spaceIdx).trim(); + const right = raw.slice(spaceIdx).trim(); + if (left === right) raw = left; } } } @@ -95,9 +102,18 @@ function checkRequired(node) { function normalizeLegend(el) { let raw = (el.textContent || '').replace(/\s+/g, ' ').replace(/\s*\*\s*$/, '').replace(/\s*Required\s*$/i, '').trim(); if (raw.length > 8) { - for (let len = Math.ceil(raw.length / 2); len >= 4; len--) { - const candidate = raw.slice(0, len); - if (raw.startsWith(candidate + candidate)) { raw = candidate.trim(); break; } + const half = Math.ceil(raw.length / 2); + const firstHalf = raw.slice(0, half).trim(); + const secondHalf = raw.slice(half).trim(); + if (firstHalf === secondHalf) { + raw = firstHalf; + } else { + const spaceIdx = raw.indexOf(' ', Math.floor(raw.length / 2) - 2); + if (spaceIdx > 0) { + const left = raw.slice(0, spaceIdx).trim(); + const right = raw.slice(spaceIdx).trim(); + if (left === right) raw = left; + } } } return raw; @@ -354,11 +370,17 @@ Answer:`; let raw = forLabel || ariaLabel || linked || ancestorLabel || node.placeholder || node.name || ''; raw = raw.replace(/\s+/g, ' ').replace(/\s*\*\s*$/, '').replace(/\s*Required\s*$/i, '').trim(); if (raw.length > 8) { - for (let len = Math.ceil(raw.length / 2); len >= 4; len--) { - const candidate = raw.slice(0, len); - if (raw.startsWith(candidate + candidate)) { - raw = candidate.trim(); - break; + const half = Math.ceil(raw.length / 2); + const firstHalf = raw.slice(0, half).trim(); + const secondHalf = raw.slice(half).trim(); + if (firstHalf === secondHalf) { + raw = firstHalf; + } else { + const spaceIdx = raw.indexOf(' ', Math.floor(raw.length / 2) - 2); + if (spaceIdx > 0) { + const left = raw.slice(0, spaceIdx).trim(); + const right = raw.slice(spaceIdx).trim(); + if (left === right) raw = left; } } } @@ -387,9 +409,18 @@ Answer:`; function _normalizeLegend(el) { let raw = (el.textContent || '').replace(/\s+/g, ' ').replace(/\s*\*\s*$/, '').replace(/\s*Required\s*$/i, '').trim(); if (raw.length > 8) { - for (let len = Math.ceil(raw.length / 2); len >= 4; len--) { - const candidate = raw.slice(0, len); - if (raw.startsWith(candidate + candidate)) { raw = candidate.trim(); break; } + const half = Math.ceil(raw.length / 2); + const firstHalf = raw.slice(0, half).trim(); + const secondHalf = raw.slice(half).trim(); + if (firstHalf === secondHalf) { + raw = firstHalf; + } else { + const spaceIdx = raw.indexOf(' ', Math.floor(raw.length / 2) - 2); + if (spaceIdx > 0) { + const left = raw.slice(0, spaceIdx).trim(); + const right = raw.slice(spaceIdx).trim(); + if (left === right) raw = left; + } } } return raw;