From 7a6e916a8634bd46db569332643c6a8d4f26e1bd Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Tue, 28 Oct 2025 09:42:05 -0400 Subject: [PATCH] detect donation product on gifts page --- ye-snippet.js | 85 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 18 deletions(-) diff --git a/ye-snippet.js b/ye-snippet.js index 78e20ad..340f78c 100644 --- a/ye-snippet.js +++ b/ye-snippet.js @@ -1,30 +1,25 @@ const Render=()=> { - if(new Date() < new Date(`Jan 1 2026`)) + const NOW = new Date(); + if(NOW < new Date(`Jan 1 2026`)) { const layoutBase =(hexColor, ...messages)=>`
${messages.join("")}
`; const messageSmall = `

The Bible teaching from Truth For Life is funded by your donations!

`; const messageLarge =(message)=> `

${message}

`; - /** @type {(selector:string)=>false|(markup:string)=>void} */ - const BuildInserter =(selector)=>{ - const element = document.querySelector(selector); - return element ? (markup)=>element.insertAdjacentHTML("afterend", markup) : false; - } - const insert = BuildInserter(".nav-outer-wrapper"); + const mainNav = document.querySelector(".nav-outer-wrapper"); - if(insert) // if the main nav exists + if(mainNav) // if the main nav exists { if(location.pathname.startsWith("/store/")) // if we are on a store page { if(location.pathname === `/store/category/gifts/`) // if we are on the store category for the gifts page, add this tweak css and *keep going* { - insert(``) + + +const mentionDonation =(imageURL)=> +{ + const selector = `.card-inner img[alt="${imageURL}"]`; + const matchingProduct = document.querySelector(selector); + if(matchingProduct) + { + matchingProduct.parentElement.insertAdjacentHTML("afterend", `
+ Also available when you donate at checkout. +
`); + } +}; + +const cacheName =()=> +{ + return fetch("/donate/").then(r=>r.text()).then(t=>{ + partStart = `

`; + partStop = `

`; + posStart = t.indexOf(partStart)+partStart.length; + posStop = t.indexOf(partStop, posStart); + const imageURL = t.substring(posStart, posStop); + localStorage.setItem("ye-2025-product", JSON.stringify({name:imageURL, date:NOW.getTime()})) + return imageURL; + }); +} + +let lookup = localStorage.getItem("ye-2025-product"); +if(!lookup) +{ + //console.log("fresh cache of name") + cacheName().then(mentionDonation); +} +else +{ + const {name, date} = JSON.parse(lookup); + const THEN = new Date(parseInt(date)); + const elapsed = Math.abs(THEN - NOW); + if( elapsed > 2 * 60 * 60 * 1000) + { + //console.log("cache stale, re-fetching name") + cacheName().then(mentionDonation); + } + else + { + //console.log("using cached name") + mentionDonation(name); + } +} + } if(!location.pathname.startsWith("/store/checkout/")) // if not on the checkout, show CHECKOUT BANNER and *quit* { - insert( + mainNav.insertAdjacentHTML("afterend", layoutBase( "#945c57", messageSmall, @@ -113,7 +157,7 @@ const Render=()=> // return; // } - insert( + mainNav.insertAdjacentHTML("afterend", layoutBase( "#bb8618", messageSmall, @@ -133,11 +177,16 @@ const Render=()=> { if(location.pathname == "/donate/") // if we are on the donate page, insert a THIN DONATION BANNER { - const insert = BuildInserter(".donation-header"); - if(insert) - { - insert(layoutBase("#bb8618", messageLarge(`Your year-end donation will help sustain the global Gospel outreach of Truth For Life. Thank you.Thank you.` + ) + ) + ) } } }