Skip to content

Instantly share code, notes, and snippets.

@aquilax
Last active February 8, 2022 05:48
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 aquilax/7e6d0f5f1494aee503a4f1025ac2e0bd to your computer and use it in GitHub Desktop.
Save aquilax/7e6d0f5f1494aee503a4f1025ac2e0bd to your computer and use it in GitHub Desktop.
Transliteration bookmarklet
javascript:(()=>{var t={"क्‍":"क्","ख्‍":"ख्","ग्‍":"ग्","घ्‍":"घ्","च्‍":"च्","छ्‍":"छ्","ज्‍":"ज्","झ्‍":"झ्","ट्‍":"ट्","ठ्‍":"ठ्","ड्‍":"ड्","ढ्‍":"ढ्","ण्‍":"ण्","त्‍":"त्","थ्‍":"थ्","द्‍":"द्","ध्‍":"ध्","न्‍":"न्","प्‍":"प्","फ्‍":"फ्","ब्‍":"ब्","भ्‍":"भ्","म्‍":"म्","य्‍":"य्","र्‍":"ऱ्","ल्‍":"ल्","व्‍":"व्","श्‍":"श्","स्‍":"स्","ह्‍":"ह्","ळ्‍":"ळ्"};(e=>{[e,...e.querySelectorAll("*:not(script):not(noscript):not(style)")].forEach(({childNodes:[...e]})=>e.filter(({nodeType:t})=>t===document.TEXT_NODE).forEach(e=>e.textContent=function(t,e){const n=Object.keys(e).join("|");if(!n)return t;const o=new RegExp(n,"g");return t.replace(o,t=>e[t])}(e.textContent,t)))})(document.body)})();
(() => {
var map = {
"क्‍": "क्",
"ख्‍": "ख्",
"ग्‍": "ग्",
"घ्‍": "घ्",
"च्‍": "च्",
"छ्‍": "छ्",
"ज्‍": "ज्",
"झ्‍": "झ्",
"ट्‍": "ट्",
"ठ्‍": "ठ्",
"ड्‍": "ड्",
"ढ्‍": "ढ्",
"ण्‍": "ण्",
"त्‍": "त्",
"थ्‍": "थ्",
"द्‍": "द्",
"ध्‍": "ध्",
"न्‍": "न्",
"प्‍": "प्",
"फ्‍": "फ्",
"ब्‍": "ब्",
"भ्‍": "भ्",
"म्‍": "म्",
"य्‍": "य्",
"र्‍": "ऱ्",
"ल्‍": "ल्",
"व्‍": "व्",
"श्‍": "श्",
"स्‍": "स्",
"ह्‍": "ह्",
"ळ्‍": "ळ्",
};
function mapReplace(str, map) {
// https://stackoverflow.com/a/56744639/17734
const matchStr = Object.keys(map).join('|');
if (!matchStr) return str;
const regexp = new RegExp(matchStr, 'g');
return str.replace(regexp, match => map[match]);
};
const replaceOnDocument = (target) => {
// https://stackoverflow.com/a/50835546/17734
// Handle `string` — see the last section
[
target,
...target.querySelectorAll("*:not(script):not(noscript):not(style)")
].forEach(({childNodes: [...nodes]}) => nodes
.filter(({nodeType}) => nodeType === document.TEXT_NODE)
.forEach((textNode) => textNode.textContent = mapReplace(textNode.textContent, map)));
};
replaceOnDocument(document.body);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment