IMDB Rating

Display imdb rating when hovering on an imdb link

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        IMDB Rating
// @namespace   com.adityapurwa.jenova.gmonkey.imdbrating
// @description Display imdb rating when hovering on an imdb link
// @include     *
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @version     1
// @grant       GM_xmlhttpRequest
// ==/UserScript==

// MIT LICENSE
// Written by Aditya Purwa ([email protected])

requesting = false;
currentlyHovered = null;
$('[href*=\'imdb\']').on('mouseover', function (event) {
  var href = $(this).attr('href');
  var regex = /http:\/\/www.imdb.com\/title\/(.+)/;
  var m = regex.exec(href);
  currentlyHovered = $(this) [0];
  setTimeout(function () {
    if (!requesting) {
      requesting = true;
      GM_xmlhttpRequest({
        method: 'GET',
        url: m[0].toString(),
        onload: function (r) {
          var dom = $.parseHTML(r.responseText);
          var rating = $(dom).find('[class=\'titlePageSprite star-box-giga-star\']');
          var title = $('<div/>').css({
            'font-weight': 'bold',
            'color': 'white'
          }).html('IMDB Rating');
          var subtitle = $('<div/>').css({
            'font-weight': 'bold',
            'color': 'white',
            'font-size': '10px'
          }).html('click to dismiss');
          var ratingBox = $('<div/>', {
            id: 'imdb-popup'
          }).append(title).append(subtitle).append('<div>' + rating.html() + '</div>').css({
            'position': 'absolute',
            'left': currentlyHovered.offsetLeft + 'px',
            'top': currentlyHovered.offsetTop + 'px',
            'font-size': '18px',
            'padding': '14px',
            'color': 'black',
            'z-index': 100,
            'box-shadow': '0 0 12px rgba(160,120,50,.35)'
          }).appendTo($('body')).click(function () {
            $('#imdb-popup').remove();
          });
          var ratingValue = parseFloat(rating.html());
          if (ratingValue > 8) {
            $(ratingBox).css('background', 'gold');
          } 
          else if (ratingValue > 5) {
            $(ratingBox).css('background', '#5A3');
          } else {
            $(ratingBox).css('background', '#F34');
          }
          requesting = false;
        }
      });
    }
  }, 1000);
});