Update script.js
Browse files
script.js
CHANGED
@@ -13,8 +13,8 @@ function updateHistory(userQuestion, botAnswer) {
|
|
13 |
|
14 |
// Handle Generate button click
|
15 |
document.getElementById("generateBtn").addEventListener("click", async () => {
|
16 |
-
const questionInput = document.getElementById("question");
|
17 |
-
const answerOutput = document.getElementById("answer");
|
18 |
const question = questionInput.value.trim();
|
19 |
|
20 |
if (!question) {
|
@@ -56,6 +56,14 @@ function resetApp() {
|
|
56 |
document.getElementById("answer").value = "";
|
57 |
document.getElementById("topLabel").innerText = "Dictate your legal question!";
|
58 |
document.getElementById("history").value = ""; // Clear history on reset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
}
|
60 |
|
61 |
// Handle Read Aloud (using Web Speech API)
|
@@ -72,7 +80,7 @@ function readAloud() {
|
|
72 |
synth.speak(utterance);
|
73 |
}
|
74 |
|
75 |
-
// Handle Save/Print
|
76 |
function saveQA() {
|
77 |
const question = document.getElementById("question").value.trim();
|
78 |
const answer = document.getElementById("answer").value.trim();
|
@@ -82,14 +90,22 @@ function saveQA() {
|
|
82 |
return;
|
83 |
}
|
84 |
|
85 |
-
|
86 |
-
const
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
}
|
94 |
|
95 |
// Handle Upload MP3
|
@@ -128,7 +144,7 @@ async function uploadMP3() {
|
|
128 |
document.getElementById("question").value = transcription; // Put transcription in question box
|
129 |
document.getElementById("answer").value = "Transcription complete. You can now summarize or generate an answer.";
|
130 |
|
131 |
-
//
|
132 |
const summarizeConfirm = confirm("MP3 transcribed. Do you want to summarize it?");
|
133 |
if (summarizeConfirm) {
|
134 |
const summaryRes = await fetch("/summarize", {
|
@@ -173,8 +189,6 @@ function handleDictate() {
|
|
173 |
if (recognition && (recognition.recognizing || recognition.continuous)) {
|
174 |
recognition.stop();
|
175 |
document.getElementById("topLabel").innerText = "Dictate your legal question!";
|
176 |
-
// If you want to automatically trigger answer generation after stopping dictation:
|
177 |
-
// document.getElementById("generateBtn").click();
|
178 |
return;
|
179 |
}
|
180 |
|
@@ -216,18 +230,24 @@ function handleDictate() {
|
|
216 |
document.addEventListener("DOMContentLoaded", () => {
|
217 |
// The existing event listener for "generateBtn" is already set up.
|
218 |
// Ensure the button IDs in HTML match what's used in JS.
|
219 |
-
// current IDs:
|
220 |
-
// Dictate: handleDictate()
|
221 |
-
// Generate Response: generateAnswer() -> now mapped to generateBtn click event
|
222 |
-
// Read Aloud: readAloud()
|
223 |
-
// Upload MP3: uploadMP3()
|
224 |
-
// Save/Print: saveQA()
|
225 |
-
// Reset: resetApp()
|
226 |
-
|
227 |
-
// Add event listeners for other buttons if they don't have inline onclick
|
228 |
-
// For example, if you replace onclick="generateAnswer()" with id="generateBtn"
|
229 |
-
// then the document.getElementById("generateBtn").addEventListener("click", ...) handles it.
|
230 |
|
231 |
// If your HTML buttons use onclick, make sure the function names match these JS functions.
|
232 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
});
|
|
|
13 |
|
14 |
// Handle Generate button click
|
15 |
document.getElementById("generateBtn").addEventListener("click", async () => {
|
16 |
+
const questionInput = document.getElementById("question");
|
17 |
+
const answerOutput = document.getElementById("answer");
|
18 |
const question = questionInput.value.trim();
|
19 |
|
20 |
if (!question) {
|
|
|
56 |
document.getElementById("answer").value = "";
|
57 |
document.getElementById("topLabel").innerText = "Dictate your legal question!";
|
58 |
document.getElementById("history").value = ""; // Clear history on reset
|
59 |
+
|
60 |
+
// Stop any ongoing speech or recognition
|
61 |
+
if (window.speechSynthesis && window.speechSynthesis.speaking) {
|
62 |
+
window.speechSynthesis.cancel();
|
63 |
+
}
|
64 |
+
if (window.recognition && (window.recognition.recognizing || window.recognition.continuous)) {
|
65 |
+
window.recognition.stop();
|
66 |
+
}
|
67 |
}
|
68 |
|
69 |
// Handle Read Aloud (using Web Speech API)
|
|
|
80 |
synth.speak(utterance);
|
81 |
}
|
82 |
|
83 |
+
// Handle Save/Print with confirmation dialog
|
84 |
function saveQA() {
|
85 |
const question = document.getElementById("question").value.trim();
|
86 |
const answer = document.getElementById("answer").value.trim();
|
|
|
90 |
return;
|
91 |
}
|
92 |
|
93 |
+
// Ask for confirmation
|
94 |
+
const confirmSave = confirm("Are you sure you want to save this Q&A?");
|
95 |
+
|
96 |
+
if (confirmSave) {
|
97 |
+
const blob = new Blob([`Question:\n${question}\n\nAnswer:\n${answer}`], { type: "text/plain" });
|
98 |
+
const link = document.createElement("a");
|
99 |
+
link.href = URL.createObjectURL(blob);
|
100 |
+
link.download = "Legal_Assistant_QnA.txt";
|
101 |
+
document.body.appendChild(link);
|
102 |
+
link.click();
|
103 |
+
document.body.removeChild(link);
|
104 |
+
URL.revokeObjectURL(link.href); // Clean up the URL object
|
105 |
+
alert("Q&A saved successfully!");
|
106 |
+
} else {
|
107 |
+
alert("Save operation cancelled.");
|
108 |
+
}
|
109 |
}
|
110 |
|
111 |
// Handle Upload MP3
|
|
|
144 |
document.getElementById("question").value = transcription; // Put transcription in question box
|
145 |
document.getElementById("answer").value = "Transcription complete. You can now summarize or generate an answer.";
|
146 |
|
147 |
+
// Prompt user to summarize or generate an answer
|
148 |
const summarizeConfirm = confirm("MP3 transcribed. Do you want to summarize it?");
|
149 |
if (summarizeConfirm) {
|
150 |
const summaryRes = await fetch("/summarize", {
|
|
|
189 |
if (recognition && (recognition.recognizing || recognition.continuous)) {
|
190 |
recognition.stop();
|
191 |
document.getElementById("topLabel").innerText = "Dictate your legal question!";
|
|
|
|
|
192 |
return;
|
193 |
}
|
194 |
|
|
|
230 |
document.addEventListener("DOMContentLoaded", () => {
|
231 |
// The existing event listener for "generateBtn" is already set up.
|
232 |
// Ensure the button IDs in HTML match what's used in JS.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
|
234 |
// If your HTML buttons use onclick, make sure the function names match these JS functions.
|
235 |
+
// The current index.html uses onclick for all buttons:
|
236 |
+
// <button onclick="handleDictate()">🎙 Dictate</button>
|
237 |
+
// <button onclick="generateAnswer()">🧾 Generate Response</button>
|
238 |
+
// <button onclick="readAloud()">🔊 Read Aloud</button>
|
239 |
+
// <button onclick="uploadMP3()">🎵 Upload MP3</button>
|
240 |
+
// <button onclick="saveQA()">🖨 Save/Print</button>
|
241 |
+
// <button onclick="resetApp()">🧹 Reset</button>
|
242 |
+
|
243 |
+
// Important: Your index.html currently has `onclick="generateAnswer()"`.
|
244 |
+
// For the `document.getElementById("generateBtn").addEventListener("click", ...)`
|
245 |
+
// in script.js to work, you need to change the HTML button to have an `id="generateBtn"`
|
246 |
+
// instead of `onclick="generateAnswer()"`.
|
247 |
+
// So, in index.html, change:
|
248 |
+
// <button onclick="generateAnswer()">🧾 Generate Response</button>
|
249 |
+
// TO:
|
250 |
+
// <button id="generateBtn">🧾 Generate Response</button>
|
251 |
+
// The current script.js is already set up to listen to an ID.
|
252 |
+
|
253 |
});
|