diff --git a/scripts/lib/constants.mjs b/scripts/lib/constants.mjs index 0c51a5c..cd352f3 100644 --- a/scripts/lib/constants.mjs +++ b/scripts/lib/constants.mjs @@ -6,6 +6,8 @@ export const PUBLIC = path.join(ROOT, "public"); export const UNLISTED_PATH = path.join(ROOT, "unlisted.txt"); export const ARTICLES_GLOB = "articles/**/index.md"; export const SITE_URL = (process.env.SITE_URL || "https://apophenia.news").replace(/\/+$/, ""); +// Public site key of the "newsletter" Turnstile widget (secret lives on newsletter.planetrenox.com) +export const TURNSTILE_SITE_KEY = "0x4AAAAAAFG4HWETOcDDQ5vg"; export const AUTHOR_PAGES = [ { diff --git a/scripts/lib/renderers.mjs b/scripts/lib/renderers.mjs index 01a2232..745ec0f 100644 --- a/scripts/lib/renderers.mjs +++ b/scripts/lib/renderers.mjs @@ -1,4 +1,4 @@ -import { SITE_URL } from "./constants.mjs"; +import { SITE_URL, TURNSTILE_SITE_KEY } from "./constants.mjs"; import { footer, merch, nav, shellHead } from "./templates.mjs"; import { escapeHtml, escapeXml, fmtDate, renderAuthorInline, toISODate } from "./utils.mjs"; @@ -123,22 +123,27 @@ ${nav} this.error = ''; this.ok = false; this.loading = true; + const token = () => this.$el.querySelector('[name=cf-turnstile-response]')?.value; + for (let i = 0; i < 50 && !token(); i++) await new Promise((r) => setTimeout(r, 100)); try { const res = await fetch('https://newsletter.planetrenox.com/api/sub', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ pool: 'apophenia', email: this.email }) + body: JSON.stringify({ pool: 'apophenia', email: this.email, token: token() || '' }) }); const data = await res.json(); if (res.ok && data.ok) { this.ok = true; this.email = ''; } else { - this.error = data.error === 'invalid email' ? 'That email doesn\\'t look right.' : 'Something went wrong. Please try again.'; + this.error = data.error === 'invalid email' ? 'That email doesn\\'t look right.' + : data.error === 'captcha failed' ? 'Couldn\\'t verify you\\'re human. Please try again.' + : 'Something went wrong. Please try again.'; } } catch (e) { this.error = 'Network error. Please try again.'; } + window.turnstile?.reset(); this.loading = false; } }" @@ -164,6 +169,7 @@ ${nav} +
@@ -176,6 +182,7 @@ ${nav}
Unsubscribe anytime. We never share your email.
+ ${merch}