-
Notifications
You must be signed in to change notification settings - Fork 0
/
textSummarise.js
100 lines (88 loc) · 2.96 KB
/
textSummarise.js
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
const form = document.querySelector("form");
const urlInput = document.querySelector('input[name="urlInput"]');
const textInput = document.querySelector('textarea[name="textInput"]');
const sentenceLimit = document.querySelector('input[name="sentences"]');
const buttonLoader = document.getElementById('summBtn');
if (sentenceLimit.value === "" ){
sentenceLimit.value = 5;
}
urlInput.addEventListener("input", () => {
textInput.disabled = urlInput.value !== "";
});
textInput.addEventListener("input", () => {
urlInput.disabled = textInput.value !== "";
});
form.addEventListener("submit", (event) => {
buttonLoader.innerHTML = '<span class="button-loader"></span>';
event.preventDefault(); // Prevent the default form submission behavior
// Access the form inputs
const urlValue = urlInput.value;
const textValue = textInput.value;
if (urlValue) {
console.log("URL Input:", urlValue);
const formdata = new FormData();
formdata.append("key", "3075fa71d1f660032c1e493b74cc0fec");
formdata.append("url", urlValue);
formdata.append("sentences", sentenceLimit.value);
const requestOptions = {
method: "POST",
body: formdata,
redirect: "follow",
};
const response = fetch(
"https://api.meaningcloud.com/summarization-1.0",
requestOptions
)
.then((response) => ({
status: response.status,
body: response.json(),
}))
.then(({ status, body }) => {
return body.then((data) => ({
status: status,
summary: data.summary,
}));
})
.then(({ status, summary }) => {
console.log(status, summary);
document.getElementById("output").innerHTML = summary;
})
.catch((error) => console.log("error", error));
buttonLoader.innerHTML = 'Summarise';
} else if (textValue) {
console.log("Text Input:", textValue);
const formdata = new FormData();
formdata.append("key", "3075fa71d1f660032c1e493b74cc0fec");
formdata.append("txt", textValue);
formdata.append("sentences", sentenceLimit.value);
const requestOptions = {
method: "POST",
body: formdata,
redirect: "follow",
};
const response = fetch(
"https://api.meaningcloud.com/summarization-1.0",
requestOptions
)
.then((response) => ({
status: response.status,
body: response.json(),
}))
.then(({ status, body }) => {
return body.then((data) => ({
status: status,
summary: data.summary,
}));
})
.then(({ status, summary }) => {
console.log(status, summary);
document.getElementById("output").innerHTML = summary;
document.getElementById("output").scrollIntoView({
behavior: 'smooth'
});
})
.catch((error) => console.log("error", error));
}
// Reset the form
form.reset();
});