antispambot-cf-worker — Edge HTML Email Obfuscation on Cloudflare Workers
antispambot-cf-worker brings WordPress's classic antispambot() email protection to any static or dynamic site via Cloudflare's streaming HTMLRewriter API. It intercepts mailto links and plaintext email addresses at the network edge and encodes them into randomized HTML entities to block scraper bots.
export function antispambot(email, hexEncoding = 0) {
let out = '';
for (let i = 0; i < email.length; i++) {
const code = email.charCodeAt(i);
const mode = (i + hexEncoding) % 3;
if (mode === 0) out += `&#${code};`;
else if (mode === 1) out += `&#x${code.toString(16)};`;
else out += email[i];
}
return out;
}