From 24ef78e615328427f424f7c93703556909825ac4 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Tue, 20 May 2025 14:34:17 +0200 Subject: [PATCH] fixed minor mistake --- public/js/index.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/public/js/index.js b/public/js/index.js index 899685e..5fd59e5 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -110,15 +110,21 @@ document.getElementById("upload-btn").onclick = async () => { [...document.getElementsByClassName("download-results")].forEach((e) => { e.onclick = () => { - const dataStr = - "data:text/json;charset=utf-8," + - encodeURIComponent(JSON.stringify(results)); - const dlAnchorElem = document.createElement("a"); - dlAnchorElem.setAttribute("href", dataStr); - dlAnchorElem.setAttribute("download", "results.json"); - document.body.appendChild(dlAnchorElem); - dlAnchorElem.click(); - document.body.removeChild(dlAnchorElem); + // 1. Convert object to JSON string + const json = JSON.stringify(results, null, 2); + // 2. Create a Blob of type application/json + const blob = new Blob([json], { type: "application/json" }); + // 3. Generate a temporary object URL + const url = URL.createObjectURL(blob); + // 4. Create a temporary element and trigger download + const link = document.createElement("a"); + link.href = url; + link.download = "results.json"; + // document.body.appendChild(link); + link.click(); + // 5. Cleanup: revoke the object URL and remove the link + URL.revokeObjectURL(url); + // document.body.removeChild(link); }; }); @@ -185,7 +191,7 @@ socket.on("queue_position", (data) => { let queue_position_interval = null; document.getElementById("request-status").onclick = async () => { - if (!id.reportValidity()) return; + if (!uuid.reportValidity()) return; const res = await fetch(`/answers/status/${uuid.value}`, { headers: { "X-Socket-Id": socket.id, @@ -209,6 +215,8 @@ document.getElementById("request-status").onclick = async () => { if (json.type == "comment") populateCommentTable(json.results); else if (json.type == "comment") populateRefinementTable(json.results); else console.error(`Unknown type ${data.type}`); + // set global variable, used when user wants to download results + results = json.results; } else if (json.status == "waiting") { statusStatusEl.textContent = `Currently waiting, position in queue ${json.queue_position}`; queue_position_interval = setInterval(() => {