Sha256: b24f5843c2a05f052fde3dbaf2cca6f45d1a56529d52a2ac77e1f4d848d4b37f
Contents?: true
Size: 1.38 KB
Versions: 24
Compression:
Stored size: 1.38 KB
Contents
const visitedPages = JSON.parse(sessionStorage.getItem("visitedPages")) || [] const DELAYED_VISITS = 2 let deferredPrompt = null const shouldCountVisitedPages = () => sessionStorage.getItem("userChoice") !== "dismissed" && visitedPages.length < DELAYED_VISITS && !visitedPages.includes(location.pathname) const shouldPrompt = () => { // Disable the application install prompt showing constantly. if (localStorage.getItem("pwaInstallPromptSeen")) { return false } return deferredPrompt && sessionStorage.getItem("userChoice") !== "dismissed" && visitedPages.length >= DELAYED_VISITS } window.addEventListener("beforeinstallprompt", (event) => { event.preventDefault() deferredPrompt = event // allow the user browse through different locations before prompt them anything if (shouldCountVisitedPages()) { sessionStorage.setItem("visitedPages", JSON.stringify([...visitedPages, location.pathname])) } }); // to trigger the prompt, it must be a "user gesture" window.addEventListener("click", async (event) => { if (shouldPrompt()) { event.preventDefault() deferredPrompt.prompt() const { outcome } = await deferredPrompt.userChoice; // store the user choice to avoid asking again in the current session sessionStorage.setItem("userChoice", outcome) sessionStorage.removeItem("visitedPages") localStorage.setItem("pwaInstallPromptSeen", true) } });
Version data entries
24 entries across 24 versions & 1 rubygems