-
Notifications
You must be signed in to change notification settings - Fork 311
/
start_writing.js
125 lines (92 loc) · 3.34 KB
/
start_writing.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
async function PostBlog(event) {
event.preventDefault();
const form = document.getElementById("blogForm");
const formData = new FormData(form);
try {
const response = await fetch("http://localhost:3000/post_blog", {
method: "POST",
body: formData,
});
const result = await response.json();
if (result.success) {
let existingPopup = document.getElementById("popupMessage");
if (existingPopup) {
existingPopup.remove();
}
const popup = document.createElement("div");
popup.id = "popupMessage";
popup.innerText = "Blog submitted successfully!";
document.body.appendChild(popup);
popup.style.display = "block";
setTimeout(() => {
popup.style.display = "none";
document.getElementById("blogForm").reset();
}, 3000);
} else {
alert("Error: " + result.message);
}
} catch (error) {
console.error("Error submitting blog:", error);
alert("An error occurred while submitting the blog.");
}
}
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (SpeechRecognition) {
const recognition = new SpeechRecognition();
recognition.continuous = true;
recognition.interimResults = false;
recognition.lang = 'en-US';
const blogContent = document.getElementById('blogContent');
const startVoiceInputButton = document.getElementById('start-voice-input');
let isListening = false;
startVoiceInputButton.addEventListener('click', () => {
if (isListening) {
recognition.stop();
startVoiceInputButton.innerText = '🎤';
isListening = false;
} else {
recognition.start();
startVoiceInputButton.innerText = '🛑';
isListening = true;
}
});
recognition.onresult = (event) => {
const transcript = event.results[event.resultIndex][0].transcript;
blogContent.value += ' ' + transcript;
};
recognition.onerror = (event) => {
console.error("Speech recognition error:", event.error);
alert("Error with voice input: " + event.error);
};
} else {
alert("Speech recognition is not supported by your browser.");
}
async function getWritingSuggestions() {
const content = document.getElementById("blogContent").value;
if (!content) {
alert("Please enter some content to get suggestions.");
return;
}
try {
const response = await fetch('http://127.0.0.1:5000/grammar-correct', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text: content })
});
if (response.ok) {
const result = await response.json();
const suggestions = result.corrected_text || "No suggestions available";
document.getElementById("suggestionsText").innerText = suggestions;
document.getElementById("suggestionsContainer").classList.add('visible');
} else {
console.error("Error fetching suggestions:", response.status);
alert("Failed to fetch suggestions. Please try again later.");
}
} catch (error) {
console.error("Error:", error);
alert("An error occurred while fetching suggestions.");
}
}
document.getElementById("getSuggestions").addEventListener("click", getWritingSuggestions);