RatingEverywhere

Show imdb.com or douban.com rating in real time

  1. // ==UserScript==
  2. // @name RatingEverywhere
  3. // @namespace https://tonylee.name
  4. // @version 1.0.1
  5. // @description Show imdb.com or douban.com rating in real time
  6. // @match *
  7. // @include *
  8. // @copyright 2018+, tonylee.name
  9. // @require http://code.jquery.com/jquery-latest.js
  10. // @grant GM_xmlhttpRequest
  11. // @connect api.douban.com
  12. // @connect www.imdb.com
  13. // ==/UserScript==
  14.  
  15. $(
  16. function() {
  17. //找到所有含有豆瓣链接的文字节点
  18. $('*:contains("movie.douban.com"):not(:has(*))').each(function(index) {
  19. var node = this;
  20. // 抓取URL
  21. var url = $(node).text().match(/(http|https):\/\/(\S*)\b/i)[0];
  22. // 换成API访问
  23. apiUrl = url.replace("movie.douban.com", "api.douban.com/v2/movie");
  24. // 获取douban评分
  25. getJSON_GM(apiUrl, function(data) {
  26. if (isEmpty(data.rating) || isEmpty(data.rating.average)) {
  27. return;
  28. }
  29. $(node).append("<a target='_blank' href='"+url+"'><div style='display: inline-flex;border: 1px;border-style: solid;'><img id='doubanIcon' src='https://img3.doubanio.com/pics/douban-icons/favicon_24x24.png'/>" + data.rating.average + "</div></a>");
  30.  
  31. });
  32. });
  33. // 找到所有含有imdb链接的文字节点
  34. $('*:contains("www.imdb.com"):not(:has(*))').each(function(index) {
  35. var node = this;
  36. // 抓取URL
  37. var url = $(node).text().match(/(http|https):\/\/(\S*)\b/i)[0];
  38. // 获取imdb评分
  39. GM_xmlhttpRequest({
  40. method: 'GET',
  41. url: url,
  42. onload: function(response) {
  43. if (response.status >= 200 && response.status < 400){
  44. var $doc=$($.parseHTML(response.responseText));
  45. $(node).append("<a target='_blank' href='"+url+"'><div style='display: inline-flex;border: 1px;border-style: solid;'><img id='doubanIcon' src='https://images-na.ssl-images-amazon.com/images/G/01/imdb/images/plugins/imdb_46x22-2264473254._CB514892074_.png'/>" + $doc.find("span[itemprop='ratingValue']").text() + "</div></a>");
  46. }
  47. else{
  48. console.log('Error getting ' + url + ': ' + response.statusText);
  49. }
  50. },
  51. onerror: function(response) {
  52. console.log('Error during GM_xmlhttpRequest to ' + url + ': ' + response.statusText);
  53. }
  54. });
  55. });
  56. }
  57. );
  58.  
  59. function getJSON_GM(url, callback) {
  60. GM_xmlhttpRequest({
  61. method: 'GET',
  62. url: url,
  63. onload: function(response) {
  64. if (response.status >= 200 && response.status < 400)
  65. callback(JSON.parse(response.responseText));
  66. else
  67. console.log('Error getting ' + url + ': ' + response.statusText);
  68. },
  69. onerror: function(response) {
  70. console.log('Error during GM_xmlhttpRequest to ' + url + ': ' + response.statusText);
  71. }
  72. });
  73. }
  74.  
  75. function isEmpty(s) {
  76. return !s || s === 'N/A';
  77. }