IMDB Rating

Display imdb rating when hovering on an imdb link

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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);
});