const EMAIL_ELEMENT = 'input[name="email"]'; const EMAIL_HASH_ELEMENT = 'input[name="email_hash"]'; const GA_ID_ELEMENT = 'input[name="ga_id"]'; function getCookie(name, encodingType = 'raw') { const cookie = document.cookie .split('; ') .find((row) => row.startsWith(`${name}=`)) ?.split('=')[1]; if (!cookie) return ''; if (encodingType === 'base64') { const cookieValue = decodeURIComponent(cookie.toString('utf8')); const decodedBytes = Uint8Array.from(atob(cookieValue), (char) => char.charCodeAt(0)); const decoder = new TextDecoder('utf-8'); return decoder.decode(decodedBytes); } return cookie; } async function sha256(message) { const msgBuffer = new TextEncoder().encode(message); const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer); const hashArray = Array.from(new Uint8Array(hashBuffer)); const hashHex = hashArray .map((b) => b.toString(16).padStart(2, '0')) .join(''); return hashHex; } function addEmailHashListener(emailElement, emailHashElement) { emailElement.addEventListener('blur', () => { if (!emailElement.value) return; sha256(emailElement.value).then((hashValue) => { emailHashElement.value = hashValue; emailHashElement.dispatchEvent(new Event('input', { bubbles: true })); }); }); } function setGaIdValue(gidElement) { gidElement.value = getCookie('_gid'); gidElement.dispatchEvent(new Event('input', { bubbles: true })); } function handlePrefill() { const form = document.querySelector('form'); console.log(form); if (!form) return; const emailElement = form.querySelector(EMAIL_ELEMENT); const emailHashElement = form.querySelector(EMAIL_HASH_ELEMENT); const gidElement = form.querySelector(GA_ID_ELEMENT); console.log(emailElement, emailHashElement, gidElement); if (gidElement) setGaIdValue(gidElement); if (emailElement && emailHashElement) addEmailHashListener(emailElement, emailHashElement); } document.addEventListener('readystatechange', event => { console.log(event.target.readyState); if (event.target.readyState === "complete") handlePrefill(); });