Update 2/26/02: DelFick /msg'ed me with his version that works for IE and Mozilla (confirmed). Cross browser capability automatically makes it better than mine and
daglo's. Here's an easy-to-copy-&-paste version:
javascript: if(document.all)sel=document.selection.createRange().text; else{sel=getSelection().toString(); }if(!sel)sel=prompt('Enter the text to ROT13',''); alpha="abcdefghijklmnopqrstuvwxyz"; alpha+=alpha+alpha.toUpperCase()+alpha.toUpperCase(); for(i=0,result=""; i<sel.length; result+=(alpha.indexOf(sel.charAt(i))<0)? sel.charAt(i++): alpha.charAt(alpha.indexOf(sel.charAt(i++))+13)); void(alert(result));
The code looks like this:
javascript:
if(document.all)
sel=document.selection.createRange().text;
else {
sel=getSelection().toString();
}
if(!sel)sel=prompt('Enter the text to ROT13','');
alpha="abcdefghijklmnopqrstuvwxyz";
alpha+=alpha+alpha.toUpperCase()+alpha.toUpperCase();
for(i=0,result=""; i < sel.length;
result+=(alpha.indexOf(sel.charAt(i)) < 0)?
sel.charAt(i++):
alpha.charAt(alpha.indexOf(sel.charAt(i++))+13)
);
void(alert(result));
Changing the last line to
void(prompt('result:', result));
makes the bookmarklet into a rot-13 writer also, where you can easily copy the rot-13 encoded text.
Here is my version of a
Mozilla rot-13 decoder
bookmarklet in one line for easy copy-and-pasting:
javascript:p=document.getSelection();if(!p)p=prompt('Text:','');
q=''; for(i=0;i<p.length;i++) {c=p.charCodeAt(i); q+=String.fromCharCode (c+((c<65)^(c<91)^(c<97)^(c<123)? (c-1)%32<13? 13:-13:0))}alert(q)
Paste this as the location of a bookmark, and you've got a rot13 decoding button.
For the curious, an easier-to-read version of the same thing:
javascript:
p=document.getSelection();
if(!p)p=prompt('Text...','');
q='';
for(i=0; i<p.length; i++){
c=p.charCodeAt(i);
q+=String.fromCharCode(
c+((c<65)^(c<91)^(c<97)^(c<123)? // *1
(c-1)%32<13?13:-13:0) // *2
);
}
alert(q)
// *1 - true iff c represents an alphabet
// *2 - decide btwn move forward and loop around
Added functionality: works on selected text, a la
Netscape Everything Search Button-style. Select a
rot13 encoded text, and press the decoder button. If nothing is selected, a dialog box pops up asking you for text.
Test it here!
Guvf znl abg pbzr va unaql...