summarizer_space / script.js
student2222333051's picture
Update script.js
642cccf verified
// script.js
async function summarizeText() {
clearMessages();
const text = document.getElementById("inputText").value;
const btn = document.getElementById("summTextBtn");
btn.disabled = true;
btn.innerText = "Summarizing...";
try {
const response = await fetch("/summarize/text", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({ text })
});
if (!response.ok) {
const err = await response.json();
showError(err.detail || "Error during summarization.");
return;
}
const data = await response.json();
document.getElementById("summaryBox").innerText = data.summary;
} catch (e) {
showError(e.message || "Network error.");
} finally {
btn.disabled = false;
btn.innerText = "Summarize Text";
}
}
async function summarizePDF() {
clearMessages();
const fileInput = document.getElementById("pdfFile");
if (!fileInput.files.length) {
showError("Please select a PDF file first.");
return;
}
const file = fileInput.files[0];
const formData = new FormData();
formData.append("file", file);
const btn = document.getElementById("summPdfBtn");
btn.disabled = true;
btn.innerText = "Summarizing...";
try {
const response = await fetch("/summarize/pdf", {
method: "POST",
body: formData
});
if (!response.ok) {
const err = await response.json();
showError(err.detail || "Error during PDF summarization.");
return;
}
const data = await response.json();
document.getElementById("summaryBox").innerText = data.summary;
} catch (e) {
showError(e.message || "Network error.");
} finally {
btn.disabled = false;
btn.innerText = "Summarize PDF";
}
}
function showError(msg) {
const errBox = document.getElementById("errorBox");
errBox.hidden = false;
errBox.innerText = msg;
}
function clearMessages() {
document.getElementById("summaryBox").innerText = "";
const errBox = document.getElementById("errorBox");
errBox.hidden = true;
errBox.innerText = "";
}
function copySummary() {
const text = document.getElementById("summaryBox").innerText;
if (!text) {
alert("There's no summary to copy.");
return;
}
navigator.clipboard.writeText(text).then(() => {
alert("Summary copied to clipboard.");
}).catch(() => {
alert("Failed to copy summary.");
});
}