Greasy Fork is available in English.

MoreMovieRatings

豆瓣和IMDb互相显示评分

// ==UserScript==
// @name         MoreMovieRatings
// @namespace    http://www.jayxon.com/
// @version      0.5.4
// @description  Show IMDb ratings on Douban, and vice versa
// @description:zh-CN 豆瓣和IMDb互相显示评分
// @author       JayXon
// @match        *://movie.douban.com/subject/*
// @match        *://www.imdb.com/title/tt*
// @grant        GM.xmlHttpRequest
// @connect      api.douban.com
// @connect      m.imdb.com
// @connect      www.omdbapi.com
// ==/UserScript==

function getURL_GM(url, callback) {
  GM.xmlHttpRequest({
    method: 'GET',
    url: url,
    onload: function(response) {
      if (response.status >= 200 && response.status < 400)
        callback(response.responseText);
      else
        console.log(
            'Error getting ' + url + ' (' + this.status + ' ' + this.statusText +
            '): ' + this.responseText);
    },
    onerror: function(response) {
      console.log('Error during GM.xmlHttpRequest to ' + url + ': ' + response.statusText);
    }
  });
}

function getJSON_GM(url, callback) {
  getURL_GM(url, function(data) {
    callback(JSON.parse(data));
  });
}

function getJSON(response) {
  if (response.status >= 200 && response.status < 400)
    return response.json();
  return Promise.reject(new Error(response.status + response.statusText));
}

function getIMDbInfo(id, callback) {
  let keys = ['40700ff1', '4ee790e0', 'd82cb888', '386234f9', 'd58193b6', '15c0aa3f'];
  let apikey = keys[Math.floor(Math.random() * keys.length)];
  let url = 'https://www.omdbapi.com/?tomatoes=true&apikey=' + apikey + '&i=' + id;
  fetch(url)
      .then(getJSON)
      .catch(error => console.error('Error fetching ', url, ': ', error))
      .then(callback);
}

function isEmpty(s) {
    return !s || s === 'N/A';
}

function insertDoubanRatingDiv(parent, title, rating, link, num_raters) {
    var star = (5 * Math.round(rating)).toString();
    if (star.length == 1)
        star = '0' + star;
    if (typeof rating === 'number')
        rating = rating.toFixed(1);
    parent.insertAdjacentHTML('beforeend',
        '<div class="rating_logo">'+title+'</div>' +
        '<div class="rating_self clearfix">' +
            '<strong class="ll rating_num">' + rating + '</strong>' +
            '<div class="rating_right">' +
                '<div class="ll bigstar' + star + '"></div>' +
                '<div style="clear: both" class="rating_sum"><a href=' + link + ' target=_blank>' + num_raters.toString().replace(/,/g, '') + '人评价</a></div>' +
            '</div>' +
        '</div>');
}

(function () {
    var id = 0;
    var host = location.hostname;
    if (host === 'movie.douban.com') {
        var sectl = document.getElementById('interest_sectl');
        if (!sectl) {
            // No rating, might be censored, try to recover using API
            var douban_id = location.href.match(/douban\.com\/subject\/(\d+)/)[1];
            if (!douban_id)
                return;

            // Insert related div back in
            var subjectwrap = document.querySelector('.subjectwrap');
            var subject = document.querySelector('.subject');
            if (!subjectwrap || !subject)
                return;
            sectl = document.createElement('div');
            sectl.id = 'interest_sectl';
            subjectwrap.insertBefore(sectl, subject.nextSibling);
            var rating_wrap = document.createElement('div');
            rating_wrap.className = 'rating_wrap';
            sectl.appendChild(rating_wrap);

            getJSON_GM('https://api.douban.com/v2/movie/' + douban_id, function (data) {
                if (isEmpty(data.rating) || isEmpty(data.rating.average))
                    return;
                insertDoubanRatingDiv(rating_wrap, '豆瓣评分', data.rating.average, 'https://movie.douban.com/subject/' + douban_id + '/collections', data.rating.numRaters);
                rating_wrap.title = '此条目的豆瓣评分已被和谐,MoreMovieRatings恢复了部分评分';
                // Move it down to leave space for fixed rating.
                if (document.querySelector('#movie-fixed-rating-iframe'))
                    sectl.style = 'margin-top:96px';
            });
        }
        id = document.querySelector('#info a[href*="://www.imdb.com/"]');
        if (!id)
            return;
        id = id.textContent;
        getIMDbInfo(id, function (data) {
            if (isEmpty(data.imdbRating) && isEmpty(data.Metascore))
                return;
            var ratings = document.createElement('div');
            ratings.style.padding = '15px 0';
            ratings.style.borderTop = '1px solid #eaeaea';
            var rating_wrap = document.querySelector('.friends_rating_wrap');
            if (!rating_wrap)
                rating_wrap = document.querySelector('.rating_wrap');
            sectl.insertBefore(ratings, rating_wrap.nextSibling);
            // IMDb
            if (!isEmpty(data.imdbRating)) {
                insertDoubanRatingDiv(ratings, 'IMDb评分', data.imdbRating, 'https://www.imdb.com/title/' + id + '/ratings', data.imdbVotes);
                // Check for IMDb Top 250
                if (data.imdbRating >= 8) {
                    getURL_GM('https://m.imdb.com/chart/top', function (top_html) {
                        var list = top_html.match(/data-tconst="(tt\d{7})"/g);
                        var number = list.indexOf('data-tconst="' + id + '"') + 1;
                        if (number < 1 || number > 250)
                            return;
                        // inject css if needed
                        if (document.getElementsByClassName('top250').length === 0) {
                            var style = document.createElement('style');
                            style.innerHTML = '.top250{background:url(https://s.doubanio.com/f/movie/f8a7b5e23d00edee6b42c6424989ce6683aa2fff/pics/movie/top250_bg.png) no-repeat;width:150px;font:12px Helvetica,Arial,sans-serif;margin:5px 0;color:#744900}.top250 span{display:inline-block;text-align:center;height:18px;line-height:18px}.top250 a,.top250 a:link,.top250 a:hover,.top250 a:active,.top250 a:visited{color:#744900;text-decoration:none;background:none}.top250-no{width:34%}.top250-link{width:66%}';
                            document.head.appendChild(style);
                        }
                        var after = document.getElementById('dale_movie_subject_top_icon');
                        if (!after)
                            after = document.querySelector('h1');
                        after.insertAdjacentHTML('beforebegin', '<div class="top250"><span class="top250-no">No.' + number + '</span><span class="top250-link"><a href="https://www.imdb.com/chart/top">IMDb Top 250</a></span></div>');
                        [].forEach.call(document.getElementsByClassName('top250'), function (e) {
                            e.style.display = 'inline-block';
                        });
                    });
                }
            }

            // Metascore
            if (!isEmpty(data.Metascore)) {
                var metascore = parseInt(data.Metascore);
                var metacolor;
                if (metascore >= 60)
                    metacolor = '#6c3';
                else if (metascore >= 40)
                    metacolor = '#fc3';
                else
                    metacolor = '#f00';
                ratings.insertAdjacentHTML('beforeend',
                    'Metascore: ' +
                    '<span style="background-color: ' + metacolor + '; color: #fff; height: 24px; width: 24px; line-height: 24px; vertical-align: middle; display: inline-block; text-align: center; font-weight: bold">' +
                        data.Metascore +
                    '</span>'
                );
            }

            // Rotten Tomatoes
            var tomato_score = null;
            for (var i in data.Ratings) {
                if (data.Ratings[i].Source == 'Rotten Tomatoes') {
                    tomato_score = data.Ratings[i].Value;
                    break;
                }
            }
            if (tomato_score) {
                if (!isEmpty(data.Metascore))
                    ratings.insertAdjacentHTML('beforeend', '<br>');

                var tomatoimg = {
                    "certified": "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAEyElEQVR4AdWTY4DkSABGXyXpTqYxRk+PT2sLZ9u2bdu2bdvmeG27b8dWWkhSZxO/7sWo7xX53yP4C/yGGtx7M/f+20xWtivNE5mOBAGicp4zZ+4C53Pg3X8l0DVh3H+695GjDuLwxqjklsd8lBbaDN/Y+vZaP89FcaHDqDyn9dQbkxcBz/xtwaAC19CZz3jm1a4X+gfVOhcfH+G9SjejBqeonaMzYpDFgy96OHi3GMUBG78Oi2fw3lG3mwcAsT8VDP4m/FV97pd9GB/WGDS1KRQVSE4+IMxDs3OZEjTJVGwkMHJQikPPz+SW80z6TMGSma7lwNA/FBiaMFY+m9V307uanpUuOfmgKO9U6l+HR4lZKue9HKQs6HDqZm3oikN/WLAipHHZPX5KAjbnHh7htefFPcCZfI/Gzzh3d//NxaNj+hYtBg++4qG8THLkPjGqm7O5e3ohfQv6aGkQxNI34ooxK8nwwdANbIZtlGLUJjajh1sEjzHOGHXowE1A629a0Pt2Vuym9zRji/EpSgocKsocLvyigh7Nz4LVCTxzenAhMXYsYJdglHHlvWwV6MaMCl76OI0PanROODDCp2/LH1vxo2Byhb5Z9XPu2qNu9JHhl1xwdJgQmTxYE6AkIVmUpaFKgcsFAUUwfrrJ/CIXnjGCC8vX8dS7GmZEECxwGO6jFQj+QnDqlhlX335H8goFAaqksdbLxx/ks9faGHqGxsW7ZeCNSbRei/WjvJz2Qi+jVsWYm6XSMtTNTic30tCnMHyjFMl13h+zNX5cUEjHUnCkgHZB5u1+DvYluW+cQXxsBkWmxa5zTQY1WNwmFELFGsPXCEYOOIysj6MZfoacY2IlVaTb4TeDLEAIW0UCqToDxxaIPJWzvQauiAuJRqo4RSoe46DP+5kxwYcEpABUSHyShn52GGwBUe23gvqQtU5J6EgNFFNF1UDRBcKrIoJ+SElEdwz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fsO4YJpyb5rkxO6Sw3FdtLTbm2YqnU1N7W1dwYd4lYLK6KgiQ2QomvZ74Es7oV906ahpbWqUDYgM8SQSn7zgC+XUA++fHXxT+Qo5n9gsipUYXPp9JqUyIdbS2VcxXGG20O65vQU0hHWtTSxrOnTu+CuUsaHBrarVaupG99OPPNWXOXzyI4ToVFFm/rjF1BeGRH99o/P3DLRVcrvB6fu3DtPCtU1mbNXTFzeGRsGCd0JP0BLHO0MJZ4Kw0AAAAASUVORK5CYII="
                };
                // Currently no way to know if it's certified or not
                var fresh;
                if (parseInt(tomato_score) >= 60)
                    fresh = 'fresh';
                else
                    fresh = 'rotten';
                var tomato_url = data.tomatoURL.replace('http://', 'https://');
                ratings.insertAdjacentHTML('beforeend',
                    "<a href=" + tomato_url + " target=_blank style='background:none'><span style='background: url(data:image/png;base64," + tomatoimg[fresh] + ") no-repeat; background-size: cover; width: 18px; height: 18px; margin: 0 2px; vertical-align: middle; display: inline-block'></span></a>" +
                    "<span style='vertical-align: middle; display: inline-block; line-height: 18px'>" + tomato_score + "</span>"
                );
                if (!isEmpty(data.tomatoUserMeter)) {
                    var userimage;
                    if (parseFloat(data.tomatoUserRating) >= 3.5)
                        userimage = "full";
                    else
                        userimage = "spilled";

                    ratings.insertAdjacentHTML('beforeend',
                        "<a href=" + tomato_url + " target=_blank style='background:none'><span style='background: url(data:image/png;base64," + tomatoimg[userimage] + ") no-repeat; background-size: cover; width: 18px; height: 18px; margin: 0 2px; vertical-align: middle; display: inline-block'></span></a>" +
                        "<span style='vertical-align: middle; display: inline-block; line-height: 18px'>" + data.tomatoUserMeter + "%</span>"
                    );
                }
            }

            // MPAA Rating
            if (!isEmpty(data.Rated)) {
                let info = document.querySelector('#info');
                if (info) {
                    if (info.lastChild.previousSibling.nodeName != 'BR')
                        info.insertAdjacentHTML('beforeend', '<br>');
                    info.insertAdjacentHTML('beforeend', '<span class="pl">MPAA评级:</span> ' + data.Rated + '<br>');
                }
            }
        });
    } else if (host === 'www.imdb.com') {
        var starbox = document.querySelector('.star-box-details');
        var new_ui = false;
        if (document.querySelector('div.imdbRating'))
            new_ui = true;
        if (!starbox && !new_ui)
            return;
        id = location.href.match(/tt\d+/);
        if (!id)
            return;
        getJSON_GM('https://api.douban.com/v2/movie/imdb/' + id + '?apikey=0df993c66c0c636e29ecbb5344252a4a', function (data) {
            if (isEmpty(data.alt))
                return;
            var url = data.alt.replace('/movie/', '/subject/') + '/';
            var num_raters = data.rating.numRaters.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
            if (!new_ui) {
                starbox.insertAdjacentHTML('beforeend',
                    '<br><a href="' + url + '" target=_blank>Douban</a>: ' +
                    '<strong><span itemprop=ratingValue>' + data.rating.average + '</span></strong>' +
                    '<span class=mellow>/<span itemprop=bestRating>' + data.rating.max + '</span></span>' +
                    ' from <a href="' + url + 'collections" target=_blank><span itemprop=ratingCount>' + num_raters + '</span> users</a>'
                );
                return;
            }
            var review_bar = document.querySelector('div.titleReviewBar');
            if (!review_bar) {
                review_bar = document.createElement('div');
                review_bar.setAttribute('class', 'titleReviewBar');
                var wrapper = document.querySelector('div.plot_summary_wrapper');
                if (!wrapper)
                    return;
                wrapper.appendChild(review_bar);
            } else {
                var divider = document.createElement('div');
                divider.setAttribute('class', 'divider');
                review_bar.insertBefore(divider, review_bar.firstChild);
            }
            review_bar.style.display = 'inline-table';
            var douban_item = document.createElement('div');
            douban_item.setAttribute('class', 'titleReviewBarItem');
            douban_item.insertAdjacentHTML('beforeend',
                '<div style="background: url(https://images-na.ssl-images-amazon.com/images/G/01/imdb/images/title/title_overview_sprite-1705639977._V_.png) no-repeat; background-position: -15px -124px; line-height: 14px; padding-left: 34px; font-size: 10px"><div class="ratingValue">' +
                '<strong><span style="font-size: 22px; font-weight: normal; font-family: Arial">' + data.rating.average + '</span></strong>' +
                '<span>/</span><span style="color: #6b6b6b">' + data.rating.max + '</span></div>' +
                '<span><a href="' + url + 'collections" target=_blank>' + num_raters + '</a>' +
                ' from <a href="' + url + '" target=_blank>Douban</a></span>'
            );
            // Style fix if titleReviewBar can't fit in one line
            if (document.querySelectorAll('div.titleReviewBarItem').length >= 3)
                if (document.querySelector('.minPosterWithPlotSummaryHeight'))
                    douban_item.style.marginBottom = '8px';
            review_bar.insertBefore(douban_item, review_bar.firstChild);
        });
    }
})();