cogito ergo mac

How to change a Mozilla bookmarklet so it will work in Safari

A sample Google search bookmarklet's javascript code broken into three lines so we can see the differences. In usage the code must all be a single line.

Mozilla Google Search

javascript:Qr=document.getSelection();
if(!Qr){void(Qr=prompt('Keywords ...',''))};
if(Qr)location.href='http://www.google.com/search?q='+escape(Qr)

Safari Google Search
javascript:Qr=escape(getSelection());
if(!Qr){void(Qr=prompt('Keywords ...',''))};
window.location='http://www.google.com/search?q='+Qr

If I knew JavaScript this might have been a no-brainer, but I don't. I would have thought a google search on the subject would have revealed a clear and easy answer right away. But no, so here it is.

[UPDATE] Not quite there yet. This solution doesn't work if the selection is more than one word. I believe the proper terminology is that it doesn't escape characters properly. A becomes <%20>, for example, not an escaped %20 but a real one, like "two%20words". Shit. This has become my white whale.
[UPDATE v2] It appears to depend on where it's escaping from, on something I can't pin down. Sometimes it works and sometimes it doesn't. Maybe on the text-encoding of the web page?

Labels: ,

0 Comments:

Post a Comment

<< Home