53f5d51821
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
66 lines
5.5 KiB
JavaScript
66 lines
5.5 KiB
JavaScript
// 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, '>').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 <form> block your provider gives you.';
|
|
if (/<script|<iframe|<embed|<object/i.test(src)) return 'Form code contains a script, iframe or embed tag. Paste your provider\'s plain HTML form (the version without JavaScript).';
|
|
if (/recaptcha|g-recaptcha|h-captcha|hcaptcha|cf-turnstile/i.test(src)) return 'Form code includes a captcha, which cannot run on a published page. Turn the captcha off in your provider\'s form settings.';
|
|
const forms = src.match(/<form\b[\s\S]*?<\/form>/gi);
|
|
if (!forms || !forms.length) return 'No <form> 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(/<form\b[^>]*>/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 (/<select\b|<textarea\b/i.test(forms[0])) return 'The form uses a drop-down or text area, which cannot be restyled. Use a form with text and email fields only.';
|
|
const inputs = forms[0].match(/<input\b[^>]*>/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(/<form\b[\s\S]*?<\/form>/gi)[0];
|
|
const formTag = formHtml.match(/<form\b[^>]*>/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(/<input\b[^>]*>/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(/<button\b[^>]*>([\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 = '<form class="lsform" action="' + escAttr(parsed.action) + '" method="' + parsed.method + '"' + (parsed.charset ? ' accept-charset="' + escAttr(parsed.charset) + '"' : '') + (parsed.target ? ' target="' + escAttr(parsed.target) + '"' : '') + '>';
|
|
const hidden = parsed.hidden.map(h => '<input type="hidden" name="' + escAttr(h.name) + '" value="' + escAttr(h.value) + '">').join('\n');
|
|
const inputs = parsed.fields.map(f => '<input class="lsin" type="' + f.type + '" name="' + escAttr(f.name) + '" placeholder="' + escAttr(f.placeholder) + '"' + (f.value ? ' value="' + escAttr(f.value) + '"' : '') + (f.required || f.type === 'email' ? ' required' : '') + '>').join('\n');
|
|
return [open, hidden, inputs, '<button type="submit" class="cta">' + escAttr((parsed.buttonText || '').trim() || defaultButton) + '</button>', '</form>'].join('\n');
|
|
}
|
|
module.exports = { EMBED_MAX_CHARS, problem, parse, html };
|