Skip to content

Commit

Permalink
Use session storage to store generated seed
Browse files Browse the repository at this point in the history
  • Loading branch information
Jcparkyn committed Dec 10, 2023
1 parent 0ff9ee3 commit 82fd8de
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,36 @@
return await fetch('sowpods.txt').then(res => res.text());
}

const getInitialSeed = () => {
// Reuse the same seed if it's already been generated, to discourage reload abuse.
const storedSeed = sessionStorage.getItem("initialSeed");
if (storedSeed) {
return parseInt(storedSeed);
}
// This seed isn't perfect, but it doesn't matter too much
const seed = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
sessionStorage.setItem("initialSeed", seed);
return seed;
}

const start = async () => {
document.getElementById("myapp").innerHTML = "Loading dictionary...";
const appElement = document.getElementById("myapp");
appElement.innerHTML = "Loading dictionary...";
let wordlist;
try {
wordlist = await fetchWordlist();
document.getElementById("myapp").innerHTML = "Loading app...";
appElement.innerHTML = "Loading app...";
} catch (e) {
document.getElementById("myapp").innerHTML = "Failed to load dictionary";
appElement.innerHTML = "Failed to load dictionary";
return;
}

// This seed isn't perfect, but it doesn't matter too much
const initialSeed = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
const initialSeed = getInitialSeed();
const shareUrlSupported = !!(navigator.canShare && navigator.canShare({ url: window.location.href }));
const clipboardWriteSupported = !!(navigator.clipboard && navigator.clipboard.writeText);

const app = Elm.Main.init({
node: document.getElementById('myapp'),
node: appElement,
flags: {
wordlist,
initialSeed,
Expand All @@ -116,6 +128,7 @@
app.ports.shareUrl.subscribe(({queryState, useClipboard}) => {
const url = new URL(window.location.href);
url.searchParams.set("state", queryState);
sessionStorage.removeItem("initialSeed");
if (useClipboard) {
navigator.clipboard.writeText(url.href);
}
Expand Down

0 comments on commit 82fd8de

Please sign in to comment.