-
Notifications
You must be signed in to change notification settings - Fork 1
/
hovertranslate.js
48 lines (46 loc) · 1.8 KB
/
hovertranslate.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
//on page load change all the tlt spans to be formatted for reading
document.addEventListener("DOMContentLoaded", () => {
let tlts = document.querySelectorAll("span.tlt");
for (let i = 0; i < tlts.length; i++) {
let input = tlts[i].innerHTML;
let output = "";
let words = input.split("} {");
for (let i = 0; i < words.length; i++) {
let word = words[i];
word = word.replace("{", "");
word = word.replace("}", "");
let wordt = word.split("|");
output += "<span class='og'>";
output += wordt[0];
output += "<span class='tl'>";
output += wordt[1];
output += "</span></span>";
}
tlts[i].innerHTML = output;
//read the document and find all the spans with class og and their corresponding tl
let translateables = document.querySelectorAll("span.og");
let translations = document.querySelectorAll("span.tl");
for (let i = 0; i < translateables.length; i++) {
//add mouselistener to each og that shows the tl
translateables[i].addEventListener("mouseenter", () => {
translations[i].style.display = "inherit";
translateables[i].style.background = "rgba(0,0,0,0.2)";
});
translateables[i].addEventListener("click", () => {
translations[i].style.display = "inherit";
translateables[i].style.background = "rgba(0,0,0,0.2)";
});
//when the mouse leaves it, hide the tl
window.addEventListener("click", (event) => {
if (!translateables[i].contains(event.target)) {
translations[i].style.display = "none";
translateables[i].style.background = "";
}
});
translateables[i].addEventListener("mouseleave", () => {
translations[i].style.display = "none";
translateables[i].style.background = "";
});
}
}
});