55 lines
1.9 KiB
JavaScript
55 lines
1.9 KiB
JavaScript
function hasKannadaChar(str) {
|
|
return /[\u0C80-\u0CFF]/.test(str);
|
|
}
|
|
|
|
(function () {
|
|
const reMatchNonKannadaBlobs = new RegExp(/[^\u0C80-\u0CFF]+/g);
|
|
// In the results (definitions), if there are Kannada words, hyperlink
|
|
// them to search.
|
|
let defs = document.querySelectorAll(".defs li");
|
|
if (!defs || defs.length === 0) {
|
|
return;
|
|
}
|
|
|
|
for (let i = 0; i < defs.length; i++) {
|
|
// Go through each definition. Ignore the ASCII ones.
|
|
if (!hasKannadaChar(defs[i].innerText)) {
|
|
continue;
|
|
}
|
|
|
|
// Split the word and iterate through it, turning non-ASCII words into
|
|
// hyperlinks;
|
|
const parts = defs[i].innerText.split(" ");
|
|
const s = document.createElement("span");
|
|
|
|
parts.forEach((v) => {
|
|
if (!hasKannadaChar(v)) {
|
|
// ASCII word. Append the text as-is.
|
|
s.appendChild(document.createTextNode(v));
|
|
} else {
|
|
// Non-ASCII word. Turn into a link.
|
|
const a = document.createElement("a");
|
|
|
|
// Some Kannada words have non-Kannada characters around them,
|
|
// the hyperlink should be formed only for Kannada words, while retaining all the characters order
|
|
const kannadaWord = v.replace(reMatchNonKannadaBlobs, "");
|
|
const nonKannadaWords = v.split(kannadaWord);
|
|
|
|
nonKannadaWords[0] && s.appendChild(document.createTextNode(nonKannadaWords[0]));
|
|
|
|
a.setAttribute("href", kannadaWord);
|
|
a.appendChild(document.createTextNode(kannadaWord));
|
|
s.appendChild(a);
|
|
|
|
nonKannadaWords[1] && s.appendChild(document.createTextNode(nonKannadaWords[1]));
|
|
}
|
|
|
|
// Append a space.
|
|
s.appendChild(document.createTextNode(" "));
|
|
});
|
|
|
|
defs[i].innerHTML = "";
|
|
defs[i].appendChild(s);
|
|
}
|
|
|
|
})(); |