IMDB "My Movies" enhancer

By ric Last update Sep 25, 2009 — Installed 1,408 times. Daily Installs: 3, 2, 2, 5, 1, 1, 3, 1, 4, 2, 3, 5, 2, 2, 6, 8, 5, 0, 3, 2, 1, 3, 1, 2, 4, 0, 1, 2, 3, 1, 2, 3

There are 6 previous versions of this script.

// This is a script for the IMDB site. It emphasize links to movies in your
// "My Movies" and "Vote History" lists. For instance, on an actor's page,
// you'll easily notice which of his/her movies you've already seen/voted.
//
// Copyright (c) 2008-2009, Ricardo MendonГ§a Ferreira (ric@mpcnet.com.br)
// Released under the GPL license - http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          IMDB "My Movies" enhancer
// @description   Emphasize the links for movies on your "My Movies" & "Vote History" list
// @namespace     http://www.flickr.com/photos/ricardo_ferreira/2502798105/
// @include       http://*imdb.com/*
// @exclude       http://i.imdb.com/*
// @exclude       http://*imdb.com/images/*
// @version       2009.09.23
// ==/UserScript==
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://www.greasespot.net/
// Install Greasemonkey, then restart Firefox and revisit this script.
//
// To uninstall, go to Tools/Greasemonkey/Manage User Scripts,
// select this script and click Uninstall.
//
// --------------------------------------------------------------------
//
// History:
// --------
// 2009.09.23  Fix for another site redesign
// 2009.08.12  Restored code to deal with links like those on http://www.imdb.com/Sections/Genres/Sci-Fi/average-vote
// 2009.07.28  Fix for IMDB site change, added debug information, exclude running on image URLs
// 2008.08.27  Explicitly send cookies (FF3 compatibility fix)
// 2008.07.27  Fixed bug where removed movies where not actually removed;
//             now also highlight the title of the movies
// 2008.06.11  Fixed bug that ketp growing the movie data in Firefox;
//             now also get the vote history
// 2008.05.18  First public release
// 2008.05.12  First test version, private use only
//


(function() {

   var myname = 'IMDB "My Movies" Enhancer';

   // Find current logged user, or quit script
   var user    = '';
   var account = document.getElementById('nb15personal');
   if (!account) {
      account = document.getElementById('nb_personal');
   }
   if (account) {
      var result = account.innerHTML.match(/\s*([^>]+)'s account/i);
      if (result && result[1]) {
         user = result[1];
      }
   }
   if (!user) {
      GM_log(document.URL + "\nUser not logged in (or couldn't get user info)"); // responseDetails.responseText
      return;
   }


   // Movie data is loaded as string, and split into an array
   //GM_setValue("mymovies:"+user, '');
   var mymovies = new Array;
   if (GM_getValue("mymovies:"+user))
      { mymovies=GM_getValue("mymovies:"+user).split('|'); }

   function addMovies(responseDetails) {
      links = responseDetails.responseText.match(/<a href=".title.tt[0]*(\d+)\//g);
      if (!links) {
         GM_log('No links found in movies list!'); // responseDetails.responseText
         return;
      }
      for (var i=0; i < links.length; i++) {
         var m = links[i].match(/tt[0]*(\d+)\/$/);
         if (!m) continue;
         // I "encode" the movie number with "base 36" to save memory
         num = parseInt(m[1]).toString(36);
         if (mymovies.indexOf(num) == -1)
            { mymovies.push(num); }
      }

      GM_setValue("mymovies:"+user, mymovies.join('|'));
      responseDetails.responseText = '';
      links = '';
   }

   var firstRun = 0;
   
   // this callback function receives the MyMovies HTML page
   function gotList1(responseDetails, firstRun) {
      mymovies = new Array;
      addMovies(responseDetails);
      // Get VoteHistory full page
      GM_xmlhttpRequest({
         method : 'GET',
         url    : 'http://www.imdb.com/mymovies/list?votehistory&a=1',
         headers: { 'Cookie': document.cookie },
         onload : function(responseDetails) { gotList2(responseDetails, firstRun) },
         onerror: function(responseDetails) { gotListError(responseDetails) }
      });
   }

   // this callback function receives the VoteHistory HTML page
   function gotList2(responseDetails, firstRun) {
      if (firstRun) {
         alert("Done!\n\n" +
               "Next time you load an IMDB page the '" + myname +
               "' will work.\n\nEnjoy!");
      }
      addMovies(responseDetails);
   }


   function gotListError(responseDetails) {
      alert('Error loading your list of movies: '+
         responseDetails.status + ' ' + responseDetails.statusText);
   }
   

   function updateMovieList() {
      if (!GM_xmlhttpRequest) {
         alert('Please upgrade to the latest version of Greasemonkey.');
         return;
      }
      // First run?
      firstRun = 0;
      if (!mymovies.length) {
         firstRun++;
         alert("We have to update the data for the '"+myname+"'\nPress OK and wait a moment, please...");
      }
      // Get MyMovies full page
      GM_xmlhttpRequest({
         method : 'GET',
         url    : 'http://www.imdb.com/mymovies/list?a=1',
         headers: { 'Cookie': document.cookie },
         onload : function(responseDetails) { gotList1(responseDetails, firstRun) },
         onerror: function(responseDetails) { gotListError(responseDetails) }
      });
   }


   function scanLinks() {
      if (!mymovies.length)
         updateMovieList();

      // Verify if the current page has a title to be highlighted
      var m = document.location.href.match(/tt[0]*(\d+)\/$/);
      if (m) {
         var num = parseInt(m[1]).toString(36);
         if (mymovies.indexOf(num) != -1) {
            var title = document.getElementsByTagName('h1');
            if (title) { title[0].style.color='green'; }
         }
      }

      // Check all links in the page
      var anchors = document.getElementsByTagName('a');
      for (var i=0; i < anchors.length; i++) {
         var a = anchors[i];
         m = a.href.match(/tt[0]*(\d+)\/$/);
         if (!m) {
            m = a.href.match(/imdb.com\/Title\?[0]*(\d+)$/);
            // http://www.imdb.com/Title?0266543 
            if (!m) continue;
         }
         // I "encode" the movie number with "base 36" to save memory
         num = parseInt(m[1]).toString(36);
         if (mymovies.indexOf(num) != -1) {
            a.style.fontWeight='bold';
            //a.style.fontStyle='italic';
            a.style.color='green';
         }
      }
   }
   
   //-------- "main" --------
   
   if (document.location.href.indexOf('/mymovies/list') != -1)
        { updateMovieList(); }
   else { scanLinks(); }

})()

// Test URLs:
//    http://www.imdb.com/chart/top
//    http://www.imdb.com/Sections/Genres/Sci-Fi/average-vote
//    http://www.imdb.com/Sections/Awards/Academy_Awards_USA/2009
//    http://www.imdb.com/Sections/Years/2004/top-grossing
//       Funny... Shark Tale on the page above points to http://www.imdb.com/title/tt0384531/,
//       but when opened it redirects to ............... http://www.imdb.com/title/tt0307453/