Created
April 2, 2022 18:04
-
-
Save Gro-Tsen/9d613d232abe7b7cac2f97251b3a2a33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JavaScript functions to replace "th" by the letter thorn throughout | |
// a document. Use this with `thornifyDoc(window.document)`. | |
// Written by David A. Madore, 2022-04-02. Public Domain. | |
function thornifyString(s) { | |
return s.replace(/T[Hh]/g, "\u00DE").replace(/th/g, "\u00FE"); | |
} | |
function thornifyDoc(doc) { | |
var body = doc.body; | |
var walker = doc.createTreeWalker(body, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, null, false); | |
var node = walker.nextNode(); | |
while ( node ) { | |
if ( node.nodeType == Node.TEXT_NODE ) { | |
var thisnode = node; | |
node = walker.nextNode(); | |
var str = thornifyString(thisnode.nodeValue); | |
thisnode.parentNode.replaceChild(doc.createTextNode(str), thisnode); | |
} else if ( node.nodeType == Node.ELEMENT_NODE | |
&& ( node.localName == "pre" | |
|| node.localName == "code" | |
|| node.localName == "script" | |
|| node.localName == "style" ) ) { | |
do { | |
node = walker.nextSibling(); | |
if ( node ) | |
break; | |
node = walker.parentNode(); | |
if ( ! node ) | |
break; | |
} while ( true ); | |
} else | |
node = walker.nextNode(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment