Created
December 14, 2017 15:40
-
-
Save 5im-0n/b335e073d9d24bfd0f9fb68970fb9e08 to your computer and use it in GitHub Desktop.
rot13 bookmaklet
This file contains hidden or 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
(function() { | |
function convert(inText) { | |
var outText = ''; | |
var t; | |
for (i = 0; i < inText.length; i++) { | |
t = inText.charCodeAt(i); | |
if ((t > 64 && t < 78) || (t > 96 && t < 110)) { | |
t += 13; | |
} else if ((t > 77 && t < 91) || (t > 109 && t < 123)) { | |
t -= 13; | |
} | |
outText += String.fromCharCode(t); | |
} | |
return outText; | |
} | |
var sel = window.getSelection().getRangeAt(0); | |
if (sel == '') { | |
var act = document.activeElement; | |
if (act.type == "textarea") { | |
var start = act.selectionStart, | |
end = act.selectionEnd; | |
if (start != end) { | |
var prefix = act.value.substring(0, start); | |
var middle = convert(act.value.substring(start, end)); | |
var suffix = act.value.substring(end); | |
act.value = prefix; | |
act.value += middle; | |
void(act.value += suffix); | |
} | |
} else { | |
void(inText = prompt('Phrase...', '')); | |
if (inText) alert(convert(inText)); | |
} | |
} else { | |
var S = window.getSelection(); | |
function t(N) { | |
return N.nodeType == N.TEXT_NODE; | |
} | |
function r(N) { | |
if (t(N)) { | |
N.data = convert(N.data); | |
} | |
} | |
for (j = 0; j < S.rangeCount; ++j) { | |
var g = S.getRangeAt(j); | |
var e = g.startContainer; | |
var f = g.endContainer; | |
var E = g.startOffset; | |
var F = g.endOffset; | |
var m = (e == f); | |
if (!m || !t(e)) { /* rot13 each text node between e and f, not including e and f. */ | |
q = document.createTreeWalker(g.commonAncestorContainer, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, null, false); | |
q.currentNode = e; | |
for (N = q.nextNode(); N && N != f; N = q.nextNode()) r(N); | |
} | |
if (t(f)) { | |
f.splitText(F); | |
} | |
if (!m) { | |
r(f); | |
} | |
if (t(e)) { | |
r(k = e.splitText(E)); | |
if (m) f = k; | |
e = k; | |
} | |
if (t(f)) { | |
g.setEnd(f, f.data.length); | |
} | |
} | |
} | |
})(); | |
//minified and ready to be a bookmarklet | |
javascript:(function(){function%20e(e){var%20t,n="";for(i=0;i<e.length;i++)(t=e.charCodeAt(i))>64&&t<78||t>96&&t<110?t+=13:(t>77&&t<91||t>109&&t<123)&&(t-=13),n+=String.fromCharCode(t);return%20n}function%20t(e){return%20e.nodeType==e.TEXT_NODE}function%20n(n){t(n)&&(n.data=e(n.data))}if(""==window.getSelection().getRangeAt(0)){var%20r=document.activeElement;if("textarea"==r.type){var%20a=r.selectionStart,o=r.selectionEnd;if(a!=o){var%20l=r.value.substring(0,a),u=e(r.value.substring(a,o)),d=r.value.substring(o);r.value=l,r.value+=u,r.value+=d}}else%20inText=prompt("Phrase...",""),inText&&alert(e(inText))}else{var%20s=window.getSelection();for(j=0;j<s.rangeCount;++j){var%20c=s.getRangeAt(j),f=c.startContainer,g=c.endContainer,v=c.startOffset,T=c.endOffset,E=f==g;if(!E||!t(f))for(q=document.createTreeWalker(c.commonAncestorContainer,NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_TEXT,null,!1),q.currentNode=f,N=q.nextNode();N&&N!=g;N=q.nextNode())n(N);t(g)&&g.splitText(T),E||n(g),t(f)&&(n(k=f.splitText(v)),E&&(g=k),f=k),t(g)&&c.setEnd(g,g.data.length)}}})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment