|
| 1 | +/* ============================================================ |
| 2 | + PLOTFLOW · Subscribe helper |
| 3 | + A tiny, reusable email-to-Kit submitter for one-off signup spots |
| 4 | + (e.g. the post-wallpaper-download nudge) that aren't the main |
| 5 | + homepage form. Posts to PLOTFLOW_CONFIG.newsletterEndpoint via a |
| 6 | + shared hidden iframe so it works cross-origin without CORS and |
| 7 | + never navigates the page. |
| 8 | + Depends on: scripts/config.js |
| 9 | + ============================================================ */ |
| 10 | +(function () { |
| 11 | + var CFG = window.PLOTFLOW_CONFIG || {}; |
| 12 | + var sink, form, input; |
| 13 | + |
| 14 | + function ensure() { |
| 15 | + if (form) return; |
| 16 | + sink = document.createElement("iframe"); |
| 17 | + sink.name = "plotflow-sub-sink"; |
| 18 | + sink.setAttribute("aria-hidden", "true"); |
| 19 | + sink.style.display = "none"; |
| 20 | + document.body.appendChild(sink); |
| 21 | + |
| 22 | + form = document.createElement("form"); |
| 23 | + form.method = "post"; |
| 24 | + form.target = "plotflow-sub-sink"; |
| 25 | + form.style.display = "none"; |
| 26 | + input = document.createElement("input"); |
| 27 | + input.type = "hidden"; |
| 28 | + input.name = CFG.newsletterField || "email_address"; |
| 29 | + form.appendChild(input); |
| 30 | + document.body.appendChild(form); |
| 31 | + } |
| 32 | + |
| 33 | + function valid(v) { return /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(String(v || "")); } |
| 34 | + |
| 35 | + window.PlotflowSubscribe = { |
| 36 | + valid: valid, |
| 37 | + configured: function () { return !!CFG.newsletterEndpoint; }, |
| 38 | + // Fire-and-forget POST. Returns true if it was sent, false if invalid |
| 39 | + // email or no provider configured. |
| 40 | + submit: function (emailVal) { |
| 41 | + if (!CFG.newsletterEndpoint || !valid(emailVal)) return false; |
| 42 | + ensure(); |
| 43 | + form.action = CFG.newsletterEndpoint; |
| 44 | + input.value = emailVal; |
| 45 | + form.submit(); |
| 46 | + return true; |
| 47 | + } |
| 48 | + }; |
| 49 | +})(); |
0 commit comments