forked from suvelocity/sentimentor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript for API task 19.9.2021.js
50 lines (46 loc) · 1.74 KB
/
javascript for API task 19.9.2021.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
let button = document.querySelector(".input-field>button");
let cat = document.getElementById('cat');
// event handler for button
function getInput(e){
let input = document.getElementById("text-area-1").value;
let resultDiv = document.getElementById("result-div");
async function fetchFunc(myText){
loadingStatus = "loading..."
resultDiv.innerText = "";
resultDiv.append(loadingStatus);
let output = await fetch("https://sentim-api.herokuapp.com/api/v1/" ,{
method: "POST",
headers :{
Accept: "application/json", "Content-Type": "application/json"
},
body : JSON.stringify({ "text":`${myText}` })
});
cat.src = `https://http.cat/${output.status}`;
if(output.status > 400){
resultDiv.innerText = "";
resultDiv.append("Error: invalid request");
}else{
output = await output.json();
output = {
type: output.result.type,
polarity: output.result.polarity
};
let br = document.createElement('br');
resultDiv.innerText = "";
resultDiv.innerText = "";
for(let property in output){
resultDiv.append(br);
resultDiv.append(property + ":" + output[property] + " ")
if(property === "polarity" && Number(output["polarity"]) < 0){
resultDiv.style.color = 'red'
}else if(property === "polarity" && Number(output["polarity"]) === 0){
resultDiv.style.color = 'grey'
}else{
resultDiv.style.color = 'green'
}
}
}
}
fetchFunc(input);
};
button.addEventListener('click', getInput);