I am trying to create a javascript bookmarklet for a special URL shortening service that we built on http://esv.to to shorten scripture references (i.e., "Matthew 5" becomes " http://esv.to/Mt5 . Estimated the booklet should execute a GET request for http://api.esv.to/Matthew+5 , which returns a text/plain response http://esv.to/Mt5 .
The code of the bookmarklet itself is as follows (extended for readability):
var body = document.getElementsByTagName('body')[0], script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'http://esv.to/media/js/bookmarklet.js'; body.appendChild(script); void(0);
The code from http://esv.to/media/js/bookmarklet.js as follows:
(function() { function shorten(ref, callback) { var url = "http://esv.to/api/" + escape(ref); var req = new XMLHttpRequest(); req.onreadystatechange = function shortenIt() { if ( this.readyState == 4 && this.status == 200 ) { callback(req.responseText); }; }; req.open( "GET", url ); req.send(); }; function doBookmarklet() { var ref = prompt("Enter a scripture reference or keyword search to link to:", "") shorten(ref, function (short) { prompt("Here is your shortened ESV URL:", short); }); }; doBookmarklet(); })();
When called from http://esv.to , the bookmarks itself works correctly. But when used on another page, this is not so. The strange thing is, when I look at a request from Firebug, the response is 200 OK , the browser loads 17 bytes (the length of the returned string), but the response body is empty! The error does not occur, but the empty responseText object of the XmlHttpRequest object.
Now, according to the Ajax call from Bookmarklet , the GET should not violate the same origin policy. This is mistake? Is there a workaround?
source share