This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
79 lines (69 loc) · 2.52 KB
/
script.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
const CORS_BASEURL = "https://locness-cors.duckdns.org/"
const domParser = new DOMParser();
const app = document.querySelector(".app");
const appTitle = document.querySelector(".js-app-title");
const appProgressBar = document.querySelector(".app__progress-bar");
const appProgressBarLoaded = document.querySelector(".app__progress-bar__loaded-fraction");
const appBackButton = document.querySelector(".js-back-button");
const appForwardButton = document.querySelector(".js-forward-button");
const appMainSearch = document.querySelector(".js-app-main-search");
const appSearchButton = document.querySelector(".js-search-button");
const dictContentHolder = document.querySelector(".js-dict-content");
const dictError = document.querySelector(".js-dict-error")
function setLoadProgress (percent) {
percent === 100 ? appProgressBar.classList.remove("visible") : appProgressBar.classList.add("visible");
appProgressBarLoaded.style.width = percent + "%";
};
function loadArticle (article) {
appTitle.textContent = article;
dictContentHolder.innerHTML = "";
dictError.style.display = "none";
setLoadProgress(50);
fetch(`${CORS_BASEURL}www.cnrtl.fr/definition/${article}`)
.then(response => response.text())
.then(responseText => {
const responseDOM = domParser.parseFromString(responseText, "text/html")
setLoadProgress(90);
const responseDictContent = responseDOM.getElementById("lexicontent").outerHTML;
dictContentHolder.innerHTML = responseDictContent;
setLoadProgress(100);
})
.catch(() => {
setLoadProgress(100);
dictError.style.display = "flex";
});
};
appSearchButton.addEventListener("click", (e) => {
app.classList.add("searching");
appMainSearch.focus();
})
appMainSearch.addEventListener("keyup", (e) => {
if (e.key === "Enter") {
if (appMainSearch.value !== "") {
const searchTerm = appMainSearch.value.trim();
loadArticle(searchTerm);
history.pushState({ article: appMainSearch.value }, appMainSearch.value);
appMainSearch.blur();
}
}
});
appMainSearch.addEventListener("blur", () => {
app.classList.remove("searching");
})
appBackButton.addEventListener("click", () => history.back());
appForwardButton.addEventListener("click", () => history.forward());
window.onpopstate = () => {
if (history.state) {
loadArticle(history.state.article);
}
else {
appTitle.textContent = "";
dictContentHolder.innerHTML = "";
dictError.style.display = "none";
}
};
window.onload = () => {
if (history.state) {
loadArticle(history.state.article);
}
}