IMDB Bookmarklet

In last week's post, I ended up linking to a whole bunch of movies on the IMDB. The process was somewhat tedious, and I lamented the lack of movable type plugins that would help. There are a few plugins that could potentially help, but not in the exact context I'm looking for (MT-Textile does have some IMDB shortcuts, but they're for IMDB searches).

So after a looking around, I decided that the best way to go would be to write a bookmarklet that would generate the code to insert a link to IMDB. I'm no expert on this stuff and I'm sure there's something wrong with the below code, but it appears to work passably well (maybe I should just call it IMDB Bookmarklet - Beta). Basically, all you need to do is go to the movie you want to link to on IMDB, click the bookmarklet in your browser, then copy and paste the text into your post (IE actually has a function that will copy a string directly to your clipboard, but no other browser will do so because of obvious security reasons. Therefore, I simply used a prompt() function to display the generated text which you have to then copy manually.)

This turned out to be something of a pain, mainly because I primarily use the Opera web browser, which is apparently more strict about javascript than any other browser. My first attempt at the bookmarklet appeared to work fine when I just pasted it into the location bar, but when I actually set up the bookmark, it choked. This apparently had something to do with single and double quotes (I thought you were supposed to be able to use both in javascript, but for whatever reason, Opera kept throwing syntax errors.)

Anyway, here's the code:
javascript:mname=document.title;murl=document.location;mdatepos=mname.lastIndexOf(' (');if(mdatepos!=-1){mname2=mname.slice(0,mdatepos);}else{mname2=mname;} temp=prompt('Copy text for a link to IMDB movie:','<a href=\''+murl+'\' title=\'IMDB: '+ mname2 +'\'>'+mname2+'</a>');focus();
Or just use this link: Generate IMDB Link

Again, all you need to do is go to the movie you want to link to on IMDB, click the bookmarklet in your browser, then copy and paste the text into your post. This is the output of the bookmarklet when you use it on IMDB's Miami Vice page:
<a href='http://imdb.com/title/tt0430357/' title='IMDB: Miami Vice'>Miami Vice</a>
A few nerdy coding things to note here:
  • The link that is generated uses single quotes (') instead of the usual double quotes ("). Both work in HTML, but I usually use double quotes and would prefer consistency. However, as previously mentioned, using double quotes does not appear to work in Opera (even when escaped with \"). If you use firefox and want to get double quotes in the generated link, try this:
    javascript:mname=document.title;murl=document.location;mdatepos=mname.lastIndexOf(' (');if(mdatepos!=-1){mname2=mname.slice(0,mdatepos);}else{mname2=mname;} temp=prompt('text','<a href=\"'+murl+'\" title=\"IMDB: '+ mname2 +'\">'+mname2+'</a>');focus();
  • The code is generated by reading in the page's URL and title tag. As such, I had to do some manipulation to remove the year from the page's title (otherwise the link would show up saying Miami Vice (2006). The way I did this may cause problems if a title has an open parentheses, but I tried to account for it. I might change it so that the year shows up in the title attribute of the link, but I don't think it's that big of a deal.
  • Foreign movies will still show up with the foreign title. So Sympathy for Mr. Vengeance will show up as Boksuneun naui geot. Personally, I think this still helps, but I don't see an easy way of generating the link with the English title (and sometimes it's nice to use the foreign title).
  • Now that I think about it, this would be helpful for linking to Amazon too. It seems like they make it more difficult to link using your Associates ID these days, so an automated way to do so will probably be helpful.
And that's it. If you're a javascript or bookmarklet expert and see something wrong with the above, please do let me know.

I realize this post has next to no appeal to the grand majority of my readers, but I ended up spending more time on this than I wanted. I'll see if I can make another post during the week this week...