Javascript bookmarklet

posté le 9 février 2010 à 22h10 dans la catégorie ComputerGeek .

In my article How to integrate Google Reader, Delicious.com and your blog using Django, I showed how I post links to my website (and Delicious) via Google Reader. But I didn’t have a solution for site which I stumbled upon. But there is a an easy solution: javascript bookmarklet.

The solution is indeed very easy: add a bookmarklet in your toolbar.

To do that, do a right click on you bookmarks toolbar (most browser have one) and add a bookmark. Enter whatever you want in the title (for instance “post link to mywebsite”) and add the following javascript as the URL:

javascript:(function(){
    f='http://pmarichal.net/toile/api/add/?url='
        + encodeURIComponent(window.location.href)  
        + '&title=' 
        + encodeURIComponent(document.title);
    a=function(){
        if(!window.open(f))
            location.href=f
    };
    if(/Firefox/.test(navigator.userAgent)) {
        setTimeout(a,0)
     }
    else {
        a()
     }
    })()

And save it.

This little piece of code will open a new window (or redirect the current location if it failed) to your API URL, using the window.location.href and the document.title respectively as the URL and the title parameters.

I cheated a little bit regarding the formatting. You need to give a ‘one-liner’. And for readability, I formatted the javascript. If you want an easy cut & paste, just use the following:

javascript:(function(){f='http://pmarichal.net/toile/api/add/?url='+encodeURIComponent(window.location.href)+'&title='+encodeURIComponent(document.title);a=function(){if(!window.open(f))location.href=f};if(/Firefox/.test(navigator.userAgent)){setTimeout(a,0)}else{a()}})()

It’s as simple as that.

Commentaires (0)

Les commentaires sont maintenant fermés sur cette entrée. Si vous voulez partager une idée sur le sujet avec moi, vous pouvez me contacter directement.