Spaces:
Sleeping
Sleeping
File size: 2,276 Bytes
1f725d8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | // web.js
// Requires: constants.js (loaded by base.html before this script)
function handleKeyDown(event) {
if (event.key === 'Enter') {
event.preventDefault();
summarizeWeb();
}
}
async function summarizeWeb() {
const urlInput = document.getElementById('urlInput');
const summarizeBtn = document.getElementById('summarizeBtn');
const resultSection = document.getElementById('resultSection');
const loadingState = document.getElementById('loadingState');
const resultContent = document.getElementById('resultContent');
const errorState = document.getElementById('errorState');
const summaryText = document.getElementById('summaryText');
const errorText = document.getElementById('errorText');
const url = urlInput.value.trim();
if (!url) { urlInput.focus(); return; }
try {
new URL(url);
} catch (e) {
alert("Please enter a valid URL.");
urlInput.focus();
return;
}
urlInput.disabled = true;
summarizeBtn.disabled = true;
resultSection.style.display = 'block';
loadingState.style.display = 'flex';
resultContent.style.display = 'none';
errorState.style.display = 'none';
try {
const response = await fetch(ROUTES.WEB_SUMMARIZE(url), {
method: 'POST',
headers: AUTH_HEADERS({ 'Accept': 'application/json' }),
});
if (!response.ok) {
throw new Error(`Error: ${response.status} ${response.statusText}`);
}
const data = await response.json();
loadingState.style.display = 'none';
if (data && data.data) {
resultContent.style.display = 'block';
summaryText.innerHTML = marked.parse(data.data);
} else {
throw new Error("No summary returned from server. Check server logs.");
}
} catch (error) {
console.error("Summarization error:", error);
loadingState.style.display = 'none';
errorState.style.display = 'flex';
errorText.textContent = error.message || "Failed to summarize the URL. Please try again.";
} finally {
urlInput.disabled = false;
summarizeBtn.disabled = false;
urlInput.focus();
}
}
|