-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
74 lines (60 loc) · 2.37 KB
/
app.js
File metadata and controls
74 lines (60 loc) · 2.37 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
62
63
64
65
66
67
68
69
70
71
72
73
74
document.getElementById("checkButton").addEventListener("click", function () {
const textInput = document.getElementById("textInput").value.trim();
if (!textInput) {
document.getElementById("result").innerHTML = "<p>Please enter some text to check.</p>";
return;
}
// Add the text to the URL as a query parameter
const currentUrl = new URL(window.location);
currentUrl.searchParams.set('text', encodeURIComponent(textInput));
window.history.pushState({}, '', currentUrl);
// Call function to check grammar
checkGrammar(textInput);
});
// On page load, check if there's text in the URL parameter and run the grammar check automatically
window.onload = function () {
const urlParams = new URLSearchParams(window.location.search);
const savedText = urlParams.get('text');
if (savedText) {
const decodedText = decodeURIComponent(savedText);
document.getElementById("textInput").value = decodedText;
checkGrammar(decodedText);
}
};
// Function to check grammar using the LanguageTool API
async function checkGrammar(text) {
const resultDiv = document.getElementById("result");
resultDiv.innerHTML = "<p>Checking for grammar mistakes...</p>";
// LanguageTool API endpoint
const apiUrl = "https://api.languagetool.org/v2/check";
try {
// Send the text to LanguageTool API for grammar checking
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
text: text,
language: "en-US",
}),
});
const data = await response.json();
if (data.matches.length === 0) {
resultDiv.innerHTML = "<p>No grammar mistakes found!</p>";
return;
}
// Display the grammar issues without bullet points
let output = "<h3>Suggestions:</h3>";
data.matches.forEach((match) => {
const from = match.offset;
const to = from + match.length;
const issue = text.substring(from, to);
output += `<div><span class="suggestion">${issue}</span>: ${match.message}<br>Suggested replacement: <span class="replacement">${match.replacements.map(r => r.value).join(', ')}</span></div>`;
});
resultDiv.innerHTML = output;
} catch (error) {
resultDiv.innerHTML = "<p>There was an error checking the text. Please try again later.</p>";
console.error("Error:", error);
}
}