|
| 1 | +// The newsletter forms (includes/subscribe.php, and the docs theme's): Cloudflare Turnstile is loaded once |
| 2 | +// someone starts filling in a form, and newsletter.daniil.it/subscribe sends errors back to /newsletter as |
| 3 | +// ?error=<code>. In the docs, a dismissable box invites readers to subscribe (see showPopup). |
| 4 | +(function () { |
| 5 | + var TURNSTILE = "https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit&onload=psalmNewsletterTurnstile"; |
| 6 | + var STORAGE_KEY = "psalm-newsletter"; |
| 7 | + // After a dismissal, the docs box comes back after this long |
| 8 | + var DISMISSED_FOR = 60 * 24 * 60 * 60 * 1000; |
| 9 | + |
| 10 | + var messages = { |
| 11 | + email: "Please enter a valid email address.", |
| 12 | + consent: "Please accept the privacy policy to subscribe.", |
| 13 | + captcha: "The anti-spam check didn't pass: please try again.", |
| 14 | + form: "The form couldn't be read: please try again.", |
| 15 | + unavailable: "The subscription couldn't be saved right now: please try again later.", |
| 16 | + }; |
| 17 | + |
| 18 | + function storage(value) { |
| 19 | + try { |
| 20 | + if (value === undefined) |
| 21 | + return JSON.parse(localStorage.getItem(STORAGE_KEY) || "null"); |
| 22 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(value)); |
| 23 | + } catch (e) { |
| 24 | + // Private windows and blocked storage: the box just shows again |
| 25 | + } |
| 26 | + return null; |
| 27 | + } |
| 28 | + |
| 29 | + var turnstileState = "idle"; |
| 30 | + var waiting = []; |
| 31 | + |
| 32 | + window.psalmNewsletterTurnstile = function () { |
| 33 | + turnstileState = "ready"; |
| 34 | + waiting.splice(0).forEach(render); |
| 35 | + }; |
| 36 | + |
| 37 | + // Renders the form's widget, loading Turnstile first if needed |
| 38 | + function render(form) { |
| 39 | + if (form.newsletterWidget !== undefined) |
| 40 | + return; |
| 41 | + if (turnstileState !== "ready") { |
| 42 | + if (waiting.indexOf(form) === -1) |
| 43 | + waiting.push(form); |
| 44 | + if (turnstileState === "idle") { |
| 45 | + turnstileState = "loading"; |
| 46 | + var script = document.createElement("script"); |
| 47 | + script.src = TURNSTILE; |
| 48 | + script.async = true; |
| 49 | + script.onerror = function () { |
| 50 | + turnstileState = "idle"; |
| 51 | + }; |
| 52 | + document.head.appendChild(script); |
| 53 | + } |
| 54 | + return; |
| 55 | + } |
| 56 | + var box = form.querySelector(".newsletter__turnstile"); |
| 57 | + form.newsletterWidget = window.turnstile.render(box, { |
| 58 | + sitekey: box.getAttribute("data-sitekey"), |
| 59 | + size: "flexible", |
| 60 | + callback: function () { |
| 61 | + if (form.newsletterPending) { |
| 62 | + form.newsletterPending = false; |
| 63 | + form.submit(); |
| 64 | + } |
| 65 | + }, |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + function setUp(form) { |
| 70 | + if (form.newsletterReady) |
| 71 | + return; |
| 72 | + form.newsletterReady = true; |
| 73 | + form.addEventListener("focusin", function () { |
| 74 | + render(form); |
| 75 | + }); |
| 76 | + // Until Turnstile has a token, wait for it and submit then |
| 77 | + form.addEventListener("submit", function (event) { |
| 78 | + var token = form.querySelector("[name=cf-turnstile-response]"); |
| 79 | + if (!token || !token.value) { |
| 80 | + event.preventDefault(); |
| 81 | + form.newsletterPending = true; |
| 82 | + render(form); |
| 83 | + return; |
| 84 | + } |
| 85 | + storage({ subscribed: Date.now() }); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + function showError() { |
| 90 | + var code = new URLSearchParams(location.search).get("error"); |
| 91 | + var form = document.querySelector("form.newsletter--page"); |
| 92 | + if (!code || !form || !Object.prototype.hasOwnProperty.call(messages, code)) |
| 93 | + return; |
| 94 | + var p = document.createElement("p"); |
| 95 | + p.className = "newsletter__error"; |
| 96 | + p.setAttribute("role", "alert"); |
| 97 | + p.textContent = messages[code]; |
| 98 | + form.parentNode.insertBefore(p, form); |
| 99 | + } |
| 100 | + |
| 101 | + // The docs box: shown once the reader has spent some time on the docs, unless they subscribed or |
| 102 | + // dismissed it recently |
| 103 | + function showPopup() { |
| 104 | + var template = document.getElementById("newsletter-popup"); |
| 105 | + if (!template) |
| 106 | + return; |
| 107 | + var state = storage(); |
| 108 | + if (state && (state.subscribed || (state.dismissed && Date.now() - state.dismissed < DISMISSED_FOR))) |
| 109 | + return; |
| 110 | + |
| 111 | + var shown = false; |
| 112 | + function show() { |
| 113 | + if (shown) |
| 114 | + return; |
| 115 | + shown = true; |
| 116 | + window.removeEventListener("scroll", onScroll); |
| 117 | + var popup = template.content.firstElementChild.cloneNode(true); |
| 118 | + popup.querySelector(".newsletter-popup__close").addEventListener("click", function () { |
| 119 | + storage({ dismissed: Date.now() }); |
| 120 | + popup.remove(); |
| 121 | + }); |
| 122 | + document.body.appendChild(popup); |
| 123 | + setUp(popup.querySelector("form.newsletter")); |
| 124 | + } |
| 125 | + function onScroll() { |
| 126 | + var scrolled = window.scrollY + window.innerHeight; |
| 127 | + if (scrolled >= document.documentElement.scrollHeight * 0.6) |
| 128 | + show(); |
| 129 | + } |
| 130 | + window.addEventListener("scroll", onScroll, { passive: true }); |
| 131 | + setTimeout(show, 45000); |
| 132 | + } |
| 133 | + |
| 134 | + function init() { |
| 135 | + Array.prototype.forEach.call(document.querySelectorAll("form.newsletter"), setUp); |
| 136 | + showError(); |
| 137 | + showPopup(); |
| 138 | + } |
| 139 | + |
| 140 | + if (document.readyState === "loading") |
| 141 | + document.addEventListener("DOMContentLoaded", init); |
| 142 | + else |
| 143 | + init(); |
| 144 | +})(); |
0 commit comments