// Parse-or-refuse for pasted autoresponder forms (ported from Branded Voice lib/formrestyle.ts, 2026-09-29). // Keeps the MECHANICS (action, method, hidden fields, visible text/email fields), replaces the SKIN, escapes // everything. Anything that cannot be rebuilt faithfully is REFUSED with a reason; member markup is never // injected into a page as-is. 'use strict'; const EMBED_MAX_CHARS = 20000; const escAttr = s => String(s).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); function attr(tag, name) { const m = tag.match(new RegExp('\\b' + name + '\\s*=\\s*"([^"]*)"', 'i')) || tag.match(new RegExp('\\b' + name + "\\s*=\\s*'([^']*)'", 'i')) || tag.match(new RegExp('\\b' + name + '\\s*=\\s*([^\\s"\'>]+)', 'i')); return m ? m[1] : null; } const hasFlag = (tag, name) => new RegExp('\\b' + name + '\\b', 'i').test(tag); function derivePlaceholder(type, name) { const n = name.toLowerCase(); if (type === 'email' || n.includes('email')) return 'Your best email address'; if (n.includes('first')) return 'Your first name'; if (n.includes('last')) return 'Your last name'; if (n.includes('name')) return 'Your name'; return 'Your answer'; } function problem(embed) { const src = String(embed || '').trim(); if (!src) return null; if (src.length > EMBED_MAX_CHARS) return 'Form code is too long. Paste only the
block your provider gives you.'; if (//gi); if (!forms || !forms.length) return 'No tag was found in the form code.'; if (forms.length > 1) return 'Form code contains more than one form. Paste just the opt-in form.'; const formTag = forms[0].match(/]*>/i)[0]; const action = attr(formTag, 'action'); if (!action || !/^https?:\/\//i.test(action)) return 'The form has no https:// action URL to submit to.'; if (/]*>/gi) || []; let visible = 0; for (const tag of inputs) { const type = (attr(tag, 'type') || 'text').toLowerCase(); if (type === 'hidden' || type === 'submit' || type === 'button' || type === 'image') continue; if (type === 'text' || type === 'email') { if (!attr(tag, 'name')) return 'A visible field in the form has no name, so submissions would not reach your list.'; visible++; continue; } return 'The form uses a "' + type + '" field, which cannot be restyled. Use a form with text and email fields only.'; } if (!visible) return 'The form has no email or text field for the visitor to fill in.'; if (visible > 4) return 'The form has more than four visible fields. Opt-in forms with up to four fields are supported.'; return null; } // returns { action, method, charset, target, hidden:[{name,value}], fields:[{type,name,placeholder,required,value}], buttonText } or null function parse(embed) { if (problem(embed)) return null; const src = String(embed).trim(); const formHtml = src.match(//gi)[0]; const formTag = formHtml.match(/]*>/i)[0]; const action = attr(formTag, 'action'); const method = (attr(formTag, 'method') || 'post').toLowerCase() === 'get' ? 'get' : 'post'; const charset = attr(formTag, 'accept-charset'); const target = attr(formTag, 'target'); const hidden = [], fields = []; let buttonText = null; for (const tag of formHtml.match(/]*>/gi) || []) { const type = (attr(tag, 'type') || 'text').toLowerCase(); const name = attr(tag, 'name') || ''; if (type === 'hidden') { if (name) hidden.push({ name, value: attr(tag, 'value') || '' }); continue; } if (type === 'submit' || type === 'button' || type === 'image') { const v = attr(tag, 'value'); if (v && type === 'submit') buttonText = v; continue; } fields.push({ type, name, placeholder: attr(tag, 'placeholder') || derivePlaceholder(type, name), required: hasFlag(tag, 'required'), value: attr(tag, 'value') || '' }); } const btn = formHtml.match(/]*>([\s\S]*?)<\/button>/i); if (btn && !buttonText) buttonText = btn[1].replace(/<[^>]+>/g, '').trim() || null; return { action, method, charset, target, hidden, fields, buttonText }; } function html(parsed, defaultButton) { const open = ''; const hidden = parsed.hidden.map(h => '').join('\n'); const inputs = parsed.fields.map(f => '').join('\n'); return [open, hidden, inputs, '', ''].join('\n'); } // -- known autoresponder providers (2026-10-10): the allowlist is about CLARITY, not blocking. A recognised action host // gets "Recognised: AWeber"; anything else is kept as a plain post to with a note that we cannot vouch for it. // Hosts match the action host or any subdomain of it; `path` (a regex source) narrows by action path, e.g. Sendy on any host. const PROVIDERS = [ { name: 'AWeber', hosts: ['aweber.com'] }, { name: 'GetResponse', hosts: ['getresponse.com', 'getresponse.net', 'gr8.com'] }, { name: 'Mailchimp', hosts: ['list-manage.com', 'mailchimp.com'] }, { name: 'Kit (ConvertKit)', hosts: ['convertkit.com', 'kit.com', 'ck.page'] }, { name: 'ActiveCampaign', hosts: ['activehosted.com', 'activecampaign.com'] }, { name: 'MailerLite', hosts: ['mailerlite.com', 'mailerlite.io'] }, { name: 'SendFox', hosts: ['sendfox.com'] }, { name: 'systeme.io', hosts: ['systeme.io'] }, { name: 'Drip', hosts: ['getdrip.com'] }, { name: 'Sendy', hosts: [], path: '(^|/)subscribe/?$' }, { name: 'Brevo', hosts: ['sibforms.com', 'brevo.com', 'sendinblue.com'] }, { name: 'Constant Contact', hosts: ['constantcontact.com', 'constantcontactpages.com'] }, { name: 'Klaviyo', hosts: ['klaviyo.com'] }, { name: 'Moosend', hosts: ['moosend.com'] }, { name: 'Benchmark Email', hosts: ['benchmarkemail.com'] }, { name: 'beehiiv', hosts: ['beehiiv.com'] }, { name: 'Substack', hosts: ['substack.com'] } ]; function actionOf(embed) { const forms = String(embed || '').match(//gi); if (!forms || !forms.length) return null; const tag = forms[0].match(/]*>/i); const a = tag ? attr(tag[0], 'action') : null; if (!a || !/^https?:\/\//i.test(a)) return null; try { return new URL(a); } catch (e) { return null; } } // { host, known, name, label, note } for a pasted form, or null when there is no usable action function provider(embed) { const u = actionOf(embed); if (!u) return null; const host = u.hostname.toLowerCase().replace(/^www\./, ''); for (const pr of PROVIDERS) { const hostHit = pr.hosts.some(h => host === h || host.endsWith('.' + h)); const pathHit = pr.path ? new RegExp(pr.path, 'i').test(u.pathname) : false; if ((pr.hosts.length && hostHit) || (!pr.hosts.length && pathHit)) return { host, known: true, name: pr.name, label: 'Recognised: ' + pr.name, note: 'Submissions go to ' + host + '.' }; } return { host, known: false, name: '', label: 'Unknown provider', note: 'The form is kept as a plain post to ' + host + '; we cannot vouch for it.' }; } module.exports = { EMBED_MAX_CHARS, PROVIDERS, problem, parse, html, provider };