-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (48 loc) · 1.97 KB
/
script.js
File metadata and controls
61 lines (48 loc) · 1.97 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
const textArea = document.getElementById("text_to_summarize");
const submitButton = document.getElementById("submit-button");
const summarizedTextArea = document.getElementById("summary");
submitButton.disabled = true;
textArea.addEventListener("input", verifyTextLength);
submitButton.addEventListener("click", submitData);
function verifyTextLength(e) {
// The e.target property gives us the HTML element that triggered the event, which in this case is the textarea. We save this to a variable called 'textarea'
const textarea = e.target;
// Verify the TextArea value.
if (textarea.value.length > 200 && textarea.value.length < 100000) {
// Enable the button when text area has value.
submitButton.disabled = false;
} else {
// Disable the button when text area is empty.
submitButton.disabled = true;
}
}
function submitData(e) {
// This is used to add animation to the submit button
submitButton.classList.add("submit-button--loading");
const text_to_summarize = textArea.value;
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
text_to_summarize: text_to_summarize,
});
var requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow",
};
// Send the text to the server using fetch API
// Note - here we can omit the “baseUrl” we needed in Postman and just use a relative path to “/summarize” because we will be calling the API from our Replit!
fetch("/summarize", requestOptions)
.then((response) => response.text()) // Response will be summarized text
.then((summary) => {
// Do something with the summary response from the back end API!
// Update the output text area with new summary
summarizedTextArea.value = summary;
// Stop the spinning loading animation
submitButton.classList.remove("submit-button--loading");
})
.catch((error) => {
console.log(error.message);
});
}