// Browser-context probes return booleans only. Never return input values, // cookies, page text, URLs or storage from these functions. function inspectLoginState (terminalOnly = false) { const visible = selector => Array.from(document.querySelectorAll(selector)).some(el => !el.hidden && el.getAttribute('aria-hidden') !== 'true' && !el.disabled && el.getClientRects().length > 0) const state = { challenge: visible('[id*="captcha" i], [class*="captcha" i], [id^="cf-chl"], [id^="challenge"], iframe[src*="challenge-platform"]'), twoFactor: visible('input[autocomplete="one-time-code"], input[name="otp"], input[name="verification-code"]'), rejected: visible('[data-testid="login-error"], [data-component="authentication-error"], [data-error="authentication"]'), authenticated: visible('a[href*="/account/signout"], a[href*="/account/logout"], button[data-testid="logout"], button[data-action="signout"]'), loginForm: visible('input[type="password"], #email-form-input, form[action*="signin"] input[type="email"], [data-testid="login-form"] input') } return terminalOnly ? (state.challenge || state.twoFactor || state.rejected || (state.authenticated && !state.loginForm)) : state } function classifyLoginState (state) { if (state?.challenge === true) return 'challenge' if (state?.twoFactor === true) return 'two_factor_required' if (state?.rejected === true) return 'auth_rejected' if (state?.authenticated === true && state?.loginForm === false) return 'authenticated' return 'authentication_unconfirmed' } function trustedLoginOrigin (value) { try { const url = new URL(value) return url.protocol === 'https:' && ['www.bloomberg.com', 'bloomberg.com'].includes(url.hostname) && !url.username && !url.password && !url.port } catch { return false } } function authenticatedDestination (value) { return trustedLoginOrigin(value) && !/^\/account\/(signin|login)(\/|$)/.test(new URL(value).pathname) } function clearLoginInput (selector) { const input = document.querySelector(selector) if (!input || input.disabled || input.getClientRects().length === 0) return false input.value = '' input.dispatchEvent(new Event('input', { bubbles: true })) return true } async function typeVisibleInput (page, selectors, text) { for (const selector of selectors) { try { await page.waitForSelector(selector, { visible: true, timeout: 2000 }) if (await page.evaluate(clearLoginInput, selector) !== true) continue await page.type(selector, text, { delay: 60 }) return selector } catch { /* Try another visible field; never report input or raw errors. */ } } return null } function submitInputForm (selector) { const input = document.querySelector(selector) const form = input?.form if (!form) return false const button = Array.from(form.querySelectorAll('button[type="submit"], button:not([type]), input[type="submit"]')) .find(el => !el.disabled && !el.hidden && el.getClientRects().length > 0) if (button) { button.click(); return true } if (typeof form.requestSubmit === 'function') { form.requestSubmit(); return true } return false } module.exports = { inspectLoginState, classifyLoginState, authenticatedDestination, trustedLoginOrigin, clearLoginInput, typeVisibleInput, submitInputForm }