move data storage out of query into url hash

This commit is contained in:
1e99 2024-11-04 22:52:00 +01:00
parent a09962d73e
commit 51c4238584

View file

@ -62,11 +62,13 @@ const notFoundDialog = {
close: document.querySelector("button#not-found-close"),
init() {
this.dialog.addEventListener("close", (ev) => {
window.location.search = "";
window.location.hash = "";
window.location.reload();
});
this.close.addEventListener("click", (ev) => {
window.location.search = "";
window.location.hash = "";
window.location.reload();
});
},
show() {
@ -80,7 +82,8 @@ const viewDialog = {
close: document.querySelector("button#view-close"),
init() {
this.dialog.addEventListener("close", (ev) => {
window.location.search = "";
window.location.hash = "";
window.location.reload();
});
this.close.addEventListener("click", (ev) => {
@ -128,10 +131,22 @@ async function viewPassword() {
try {
loadingDialog.show();
let id, key, iv;
// We need to be backwards compatible with old links that still use the query
if (window.location.search.trim() != "") {
const params = new URLSearchParams(window.location.search);
const id = params.get("id");
const key = params.get("key");
const iv = params.get("iv");
id = params.get("id");
key = params.get("key");
iv = params.get("iv");
} else {
// Need to remove leading "#"
const hash = window.location.hash.substring(1);
const split = hash.split(":");
id = split[0];
key = split[1];
iv = split[2];
}
const exists = await hasPassword(id);
if (!exists) {
@ -142,7 +157,10 @@ async function viewPassword() {
const shouldView = await confirmViewDialog.show();
if (!shouldView) {
// This is needed for the redirect, otherwise the user won't get redirected
setTimeout(() => (window.location.search = ""), 0);
setTimeout(() => {
window.location.hash = "";
window.location.reload();
}, 0);
return;
}
@ -171,13 +189,8 @@ enterPassword.addEventListener("submit", async (ev) => {
parseInt(data.get("expires-in")),
);
const params = new URLSearchParams();
params.set("id", id);
params.set("key", password.key);
params.set("iv", password.iv);
const url = new URL(window.location);
url.search = params.toString();
url.hash = [id, password.key, password.iv].join(":");
urlDialog.show(url.toString());
} catch (error) {
errorDialog.show(error);
@ -193,6 +206,12 @@ notFoundDialog.init();
viewDialog.init();
confirmViewDialog.init();
const hash = window.location.hash;
if (hash.trim() != "") {
viewPassword();
}
// We need to be backwards compatible with the old links
const query = window.location.search;
if (query.trim() != "") {
viewPassword();