Skip to content

Instantly share code, notes, and snippets.

@vukicevic
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vukicevic/10389937 to your computer and use it in GitHub Desktop.
Save vukicevic/10389937 to your computer and use it in GitHub Desktop.
Latin script to Serbian cyrillic converter bookmarklet, preserves DOM elements.
(function () {
var s, r, t;
function replace(parent) {
var n, w = document.createTreeWalker(parent, NodeFilter.SHOW_TEXT, null, false),
c = ['љ','Љ','њ','Њ','џ','Џ','ч','Ч','ћ','Ћ','ж','Ж','ш','Ш','ђ','Ђ','з','З','е','Е','р','Р','т','Т','у','У','и','И','о','О','п','П','а','А','с','С','д','Д','ф','Ф','г','Г','х','Х','ј','Ј','к','К','л','Л','ц','Ц','в','В','б','Б','н','Н','м','М'],
l = ['lj','Lj','nj','Nj','dž','Dž','č','Č','ć','Ć','ž','Ž','š','Š','đ','Đ','z','Z','e','E','r','R','t','T','u','U','i','I','o','O','p','P','a','A','s','S','d','D','f','F','g','G','h','H','j','J','k','K','l','L','c','C','v','V','b','B','n','N','m','M'];
while (n = w.nextNode())
c.forEach(function(v, i) { n.nodeValue = n.nodeValue.split(l[i]).join(v) });
}
s = document.getSelection();
if (s.rangeCount) {
r = s.getRangeAt(0);
t = r.extractContents();
replace(t);
r.insertNode(t);
} else {
replace(document.body);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment