Skip to content

Instantly share code, notes, and snippets.

@salif
Created July 13, 2022 10:15
Show Gist options
  • Save salif/021c342eacc3b7588a5175a4dca74a73 to your computer and use it in GitHub Desktop.
Save salif/021c342eacc3b7588a5175a4dca74a73 to your computer and use it in GitHub Desktop.
Transliterate a Web Page

Example

transliterate_page({ "Ĉ": "Cx", "Ĝ": "Gx", "Ĥ": "Hx", "Ĵ": "Jx", "Ŝ": "Sx", "Ŭ": "Ux", "ĉ": "cx", "ĝ": "gx", "ĥ": "hx", "ĵ": "jx", "ŝ": "sx", "ŭ": "ux" })

Before transliteration

Ĉiuliteraĵo

After transliteration

Cxiuliterajxo
function transliterate_page(alf) {
function tr(input) {
var output = input
var alf_entries = Object.entries(alf)
for (var i = 0; i < alf_entries.length; i++) {
var alf_entry = alf_entries[i]
output = output.replaceAll(alf_entry[0], alf_entry[1])
}
return output
}
var nodes = document.createNodeIterator(document.documentElement, NodeFilter.SHOW_TEXT), node
while (node = nodes.nextNode()) {
var tag = node.parentElement ? node.parentElement.tagName.toLowerCase() : ""
if (tag === "style" || tag === "script" || tag === "code") {
continue
}
node.nodeValue = tr(node.nodeValue)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment