Tuesday 12 April 2011

Wow I made it work

I mentioned in the last post a problem I had using a Javascript button to import a book into LibraryThing from its amazon.co.uk page. I have fixed it! Hurray for me! I don't know any Javascript, I just hacked it around a bit and compared the output with the input until it did the right thing. The button which you get from this LibraryThing page has this javascript:
javascript:var%20rx%20=%20/amazon\.(.*?)\/.*?\/(.{10})\//;%20var%20rs%20=%20new%20Array();%20var%20rs%20=%20document.URL.match(rx);%20if(rs==null)%20{%20alert('Use%20this%20on%20an%20Amazon%20book%20page.');%20}%20else%20{%20pt%20=%20prompt('Tags?%20(or%20leave%20blank)');%20location.href='http://www.librarything.com/addbooks.php?bm=1&isbn='+rs[2]+'&vs='+rs[1]+'&pt='+encodeURI(pt);%20}
I saw that there is a quick URL-based import using ISBN described on this LibraryThing page, so I altered it to this:
javascript:var%20rx%20=%20/amazon\.(.*?)\/.*?\/(.{10})\//;%20var%20rs%20=%20new%20Array();%20var%20rs%20=%20document.URL.match(rx);%20if(rs==null)%20{%20alert('Use%20this%20on%20an%20Amazon%20book%20page.');%20}%20else%20{%20pt%20=%20prompt('Tags?%20(or%20leave%20blank)');%20location.href='http://www.librarything.com/addbook/'+rs[2]}
and it now works for me. I feel so good about this it's ridiculous.

So if you use LibraryThing and drag this link: Add to LibraryThing to your bookmark toolbar then you should now be able to use the bookmarklet. Wow I am so smug right now.

2 comments:

  1. Well done! It's always fun to tweak things into working a little better---that's one reason I like free software.

    Since you've excised the part that submits the tags you might as well get rid of the prompt asking for them as well. That would be this statement:

    pt = prompt('Tags? (or leave blank)');

    You could also, if you wished, remove this entirely redundant statement:

    var rs = new Array();

    which does nothing because the variable rs is immediately re-declared.

    (The %20 symbols in the code are just spaces, because the link has been URI encoded by your browser; to see a rather more intelligible version you can go to the original page, view the source (Ctrl+U in Firefox) and scroll down to the relevant section.)

    ReplyDelete
  2. Thanks! I will try those tweaks. That's exactly the problem with altering things without knowledge, you leave artefacts behind, which might make everything less efficient or even mean that although it works in one place it doesn't in others. I didn't realise that the %20s had been added by the browser, I thought maybe Javascript didn't like spaces, and I must say it's a lot more intelligible with spaces instead. Maybe I should try to learn how these things work. For one thing I have a greasemonkey script which turns amazon.com links into amazon.co.uk ones if possible, but it doesn't always work, and maybe I could make a bookmarklet to do it for me from the actual .com page instead of directly from the link. It would be very satisfying if I could.

    ReplyDelete