-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (94 loc) · 4.06 KB
/
script.js
File metadata and controls
116 lines (94 loc) · 4.06 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
document.addEventListener("DOMContentLoaded", function () {
console.log("✅ JCAP_AI_PAPER_SUMMARIZER loaded successfully!");
console.log("📌 Checking button and input elements...");
let uploadButton = document.getElementById("uploadButton");
let fileInput = document.getElementById("fileInput");
console.log("Upload Button:", uploadButton);
console.log("File Input:", fileInput);
if (!fileInput) {
console.error("❌ Error: File input element not found in DOM!");
}
if (uploadButton) {
uploadButton.addEventListener("click", function(event) {
event.preventDefault(); // Prevent default form submission
uploadFile();
});
} else {
console.error("❌ Upload button not found! Check index.html.");
}
});
// Function to handle tab switching
function showTab(tabId) {
document.querySelectorAll('.tab').forEach(tab => tab.style.display = 'none');
let selectedTab = document.getElementById(tabId);
if (selectedTab) {
selectedTab.style.display = 'block';
console.log(`🔄 Switching to tab: ${tabId}`);
} else {
console.error(`❌ Tab not found: ${tabId}`);
}
}
// Function to handle file upload
function uploadFile() {
console.log("📌 Upload button clicked");
let fileInput = document.getElementById("fileInput");
if (!fileInput || fileInput.files.length === 0) {
console.error("❌ No file selected!");
alert("Please select a file before uploading.");
return;
}
let formData = new FormData(); // ✅ Ensuring formData is properly declared
formData.append("file", fileInput.files[0]); // ✅ Appending the selected file manually
console.log("📂 Selected file:", fileInput.files[0]);
document.getElementById("progress-container").style.display = "block";
document.getElementById("progress-bar").style.width = "0%";
fetch("/upload", {
method: "POST",
body: formData
})
.then(response => response.json())
.then(data => {
console.log("✅ Full response:", data);
let summaryTab = document.getElementById("summary-tab");
let figuresTab = document.getElementById("figures-tab");
summaryTab.innerHTML = "<h2>Summaries</h2>";
figuresTab.innerHTML = "<h2>Figures</h2>";
if (!data.summaries || typeof data.summaries !== "object") {
console.error("❌ Error: Summaries are missing in response!");
return;
}
if (!data.figures || typeof data.figures !== "object") {
console.error("❌ Error: Figures are missing in response!");
return;
}
// Populate summaries
Object.keys(data.summaries).forEach(pdfName => {
let summaryContent = document.createElement("div");
summaryContent.innerHTML = `
<h3>${pdfName} Summary</h3>
<p>${data.summaries[pdfName]}</p>
<a href="${data.download_links[pdfName]}" download>
<button>Download Summary</button>
</a>
`;
summaryTab.appendChild(summaryContent);
});
// Populate figures
Object.keys(data.figures).forEach(pdfName => {
let figuresContent = document.createElement("div");
figuresContent.innerHTML = `<h3>${pdfName} Figures</h3>`;
data.figures[pdfName].forEach(fig => {
let img = document.createElement("img");
img.src = fig.startsWith("http") ? fig : "/static/figures/" + fig;
img.alt = "Extracted Figure";
figuresContent.appendChild(img);
});
figuresTab.appendChild(figuresContent);
});
showTab("summary-tab");
})
.catch(error => console.error("❌ Fetch error:", error));
}
// **Expose function to global scope**
window.uploadFile = uploadFile;
window.showTab = showTab; // ✅ Also ensuring tab switching works