RatingEverywhere

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

当前为 2018-01-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name RatingEverywhere
  3. // @namespace https://tonylee.name
  4. // @version 1.0
  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. // ==/UserScript==
  13.  
  14. (function() {
  15. //找到所有含有豆瓣链接的文字节点
  16. $('*:contains("movie.douban.com"):not(:has(*))').each(function( index ) {
  17. var node=this;
  18. // 抓取URL
  19. var url=$(node).text().match(/(http|https):\/\/(\S*)\b/i)[0];
  20. // 换成API访问
  21. apiUrl=url.replace("movie.douban.com","api.douban.com/v2/movie");
  22. // 获取douban评分
  23. getJSON_GM(apiUrl,function(data){
  24. if (isEmpty(data.rating) || isEmpty(data.rating.average)){
  25. return;
  26. }
  27. $(node).append("<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>");
  28.  
  29. });
  30. });
  31. })();
  32.  
  33.  
  34. function getJSON_GM(url, callback) {
  35. GM_xmlhttpRequest({
  36. method: 'GET',
  37. url: url,
  38. onload: function(response) {
  39. if (response.status >= 200 && response.status < 400)
  40. callback(JSON.parse(response.responseText));
  41. else
  42. console.log('Error getting ' + url + ': ' + response.statusText);
  43. },
  44. onerror: function(response) {
  45. console.log('Error during GM_xmlhttpRequest to ' + url + ': ' + response.statusText);
  46. }
  47. });
  48. }
  49.  
  50. function isEmpty(s) {
  51. return !s || s === 'N/A';
  52. }