Skip to content

Instantly share code, notes, and snippets.

@dd0754 dd0754/imdbRate.js Secret
Created Mar 21, 2018

Embed
What would you like to do?
// ==UserScript==
// @name 豆瓣显示IMDb评分&MPAA评级
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author eve8
// @connect imdb.com
// @grant GM_xmlhttpRequest
// @require https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js
// @match https://movie.douban.com/subject/*
// ==/UserScript==
(async function() {
'use strict';
const MPAA = {
'G': '大众级,任何人都可以观看',
'PG': '辅导级,该级别电影中的一些内容可能不适合儿童观看。',
'PG-13': '特别辅导级,建议13岁后儿童观看',
'R': '限制级,建议17岁以上观看',
'NC-17': '17岁以下(包括17岁)禁止观看',
'NR/OR': '未定级',
'M/X/P': '不准在大院线放映的电影',
};
const imdbHtml = `<div class="rating_wrap clearbox" rel="v:rating" style="border-top: 1px solid #eaeaea; padding-top: 15px;">
<div class="clearfix">
<div class="rating_logo ll">IMDb评分</div>
</div>
<div class="rating_self clearfix" typeof="v:Rating">
<strong class="ll rating_num" property="v:average"></strong>
<span property="v:best" content="10.0"></span>
<div class="rating_right ">
<div class="ll bigstar"></div>
<div class="rating_sum">
<a href="javascript:;" class="rating_people"><span property="v:votes"></span>人评价</a>
</div>
</div>
</div>
</div>`;
const request = options => new Promise((resolve, reject) => {
if (!(options instanceof Object)) {
return reject('options is not an object');
}
if (!options.url) {
return reject('url required');
}
options = $.extend({
method: 'GET',
headers: {
'User-agent': window.navigator.userAgent,
},
onload: function(res) {
if (200 !== res.status) {
// console.log(res);
return reject('request error');
}
return resolve(res.responseText);
},
}, options || {});
GM_xmlhttpRequest(options);
});
try {
const imdb = $('div#info a[href^=\'http://www.imdb.com/title/\']');
if (!imdb.length) { return false; }
const href = imdb.attr('href');
const rs = await request({ url: href });
const html = $(rs);
const mpaaRate = html.find('meta[itemprop="contentRating"]').attr('content');
const imdbRateing = html.find('span[itemprop="ratingValue"]').text();
const imdbRateingCount = html.find('span[itemprop="ratingCount"]').text();
if (mpaaRate) {
const mpaaText = MPAA[mpaaRate] || '';
console.log(mpaaText);
imdb.after(`<br/><span class="pl">MPAA评级:</span> ${mpaaRate}${mpaaText}`);
}
if (imdbRateing && imdbRateingCount) {
const imdbRate = $(imdbHtml);
const imdbRateStar = Math.round(imdbRateing) / 2 * 10;
imdbRate.find('.rating_num').text(imdbRateing).siblings('.rating_right').find('.bigstar').addClass(`bigstar${imdbRateStar}`).next().find('span').text(imdbRateingCount).end();
$('#interest_sectl').append(imdbRate);
}
} catch (e) {
console.error(e);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.