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