First, set up your bookmarklet with a link that you can remove from the bookmarks bar:
<html> <head></head> <body> <a href="javascript:(function(src, cb){var s = document.createElement('script');s.charset = 'UTF-8';document.body.insertBefore(s, document.body.firstChild);s.src = src;if(typeof cb === 'function'){s.onload = cb;s.onreadystatechange = function(){(/loaded|complete/).test(s.readyState)&&cb(s);};}return s;}('http://github.com/pure/pure/raw/master/libs/pure.js', function(e){alert('loaded');}))">load</a> </body> </html>
Replace the url with your script, it will be loaded and launched on the main page.
However, it is now on the hosted page and cannot call your server with XMLHTTPRequest , because the domains do not match.
Here comes the JSONP.
In a loaded script, you can put a function, for example: function srvCallback(json){...}
If you want to call your server, you will enter it as a script, using the same function as in the above bookmarklet:
function jsonp(src){ var s = document.createElement('script'); old = document.getElementById('srvCall'); old && document.body.removeChild(old); s.charset = 'UTF-8'; s.id = 'srvCall'; document.body.insertBefore(s, document.body.firstChild); s.src = src + '?' + new Date().getTime(); }
Make your request, for example:
jsonp('http://domain.com/controller/method/word')
The server should respond something like this:
srvCallback({word:'hello'});
And finally, the srvCallback function srvCallback automatically called, inside the function you get your JSON and show the result to the user.