Check for localStorage availability upon load

This commit is contained in:
Gergely Polonkai 2025-06-05 19:15:43 +02:00
parent 5e0bddf0d3
commit 18604339a7
No known key found for this signature in database
GPG Key ID: 38F402C8471DDE93

View File

@ -181,6 +181,11 @@
text-align: center; text-align: center;
} }
#no-local-storage {
display: none;
color: red;
}
.box { .box {
width: 80%; width: 80%;
border-width: 2px; border-width: 2px;
@ -205,10 +210,44 @@
<span class="light-mode-hide"></span> <span class="light-mode-hide"></span>
</label> </label>
</div> </div>
<h1>Cypher Player Assistant</h1> <h1>
Cypher Player Assistant
<span id="no-local-storage" title="Local Storage is not available. You can import/export data, but the app wont save it between sessions!">!</span>
</h1>
<div id="no-loaded-container" class="box"> <div id="no-loaded-container" class="box">
No character is loaded. No character is loaded.
</div> </div>
</div> </div>
<script>
const checkLocalStorage = () => {
let storage;
try {
storage = window.localStorage;
const x = "__storage_test__";
storage.setItem(x, x);
storage.removeItem(x);
return true;
} catch (e) {
return (
e instanceof DOMException &&
e.name === "QuotaExceededError" &&
// acknowledge QuotaExceededError only if there's something already stored
storage &&
storage.length !== 0
);
}
};
document.addEventListener(
"DOMContentLoaded",
() => {
if (!checkLocalStorage()) {
document.getElementById("no-local-storage").style.display = "initial";
alert("Local Storage is not available, saving and loading data will be unavailable.");
}
},
);
</script>
</body> </body>
</html> </html>