add IMDb rating & votes next to all IMDb Movie/Series Links (Working 2023 on new view)

Adds movie ratings and number of voters to any imdb link on any webpage. Modified version of http://userscripts.org/scripts/show/96884 And from [StackOverflow community (especially Brock Adams)]

当前为 2023-11-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name add IMDb rating & votes next to all IMDb Movie/Series Links (Working 2023 on new view)
  3. // @description Adds movie ratings and number of voters to any imdb link on any webpage. Modified version of http://userscripts.org/scripts/show/96884 And from [StackOverflow community (especially Brock Adams)]
  4. // @author [StackOverflow community (especially Brock Adams)]
  5. // @version 2023-11-20
  6. // @include *
  7. // @grant GM_xmlhttpRequest
  8. // @namespace Extra2020
  9. // ==/UserScript==
  10.  
  11.  
  12. var maxLinksAtATime = 75; //-- pages can have 75's of links to fetch. Don't spam server or browser.
  13. var fetchedLinkCnt = 0;
  14.  
  15. function processIMDB_Links () {
  16. //--- Get only links that could be to IMBD movie/TV pages.
  17. var linksToIMBD_Shows = document.querySelectorAll ("a[href*='/title/']");
  18.  
  19. for (var J = 0, L = linksToIMBD_Shows.length; J < L; J++) {
  20. var currentLink = linksToIMBD_Shows[J];
  21.  
  22. /*--- Strict tests for the correct IMDB link to keep from spamming the page
  23. with erroneous results.
  24.  
  25. if ( ! /^(?:www\.)?IMDB\.com$/i.test (currentLink.hostname) || ! /^\/title\/tt\d+\/?$/i.test (currentLink.pathname) || ! /nm_knf_t_/i.test (currentLink.pathname) )
  26. */
  27.  
  28. if ( ! /^(?:www\.)?IMDB\.com$/i.test (currentLink.hostname) || ! /^\/title\/tt\d+\/?$/i.test (currentLink.pathname))
  29. continue;
  30.  
  31.  
  32. if (! currentLink.getAttribute ("data-gm-fetched") ){
  33. if (fetchedLinkCnt >= maxLinksAtATime){
  34. //--- Position the "continue" button.
  35. continueBttn.style.display = 'inline';
  36. currentLink.parentNode.insertBefore (continueBttn, currentLink);
  37. break;
  38. }
  39.  
  40. fetchTargetLink (currentLink); //-- AJAX-in the ratings for a given link.
  41.  
  42. //---Mark the link with a data attribute, so we know it's been fetched.
  43. currentLink.setAttribute ("data-gm-fetched", "true");
  44. fetchedLinkCnt++;
  45. }
  46. }
  47. }
  48.  
  49. function fetchTargetLink (linkNode) {
  50. //--- This function provides a closure so that the callbacks can work correctly.
  51.  
  52. /*--- Must either call AJAX in a closure or pass a context.
  53. But Tampermonkey does not implement context correctly!
  54. (Tries to JSON serialize a DOM node.)
  55. */
  56. GM_xmlhttpRequest ( {
  57. method: 'get',
  58. url: linkNode.href,
  59. //context: linkNode,
  60. onload: function (response) {
  61. prependIMDB_Rating (response, linkNode);
  62. },
  63.  
  64. onabort: function (response) {
  65. prependIMDB_Rating (response, linkNode);
  66. }
  67. } );
  68. }
  69.  
  70.  
  71.  
  72. function prependIMDB_Rating (resp, targetLink) {
  73. var isError = true;
  74. var ratingTxt = "*Err!";
  75. var colnumber = 0;
  76. var justrate =0;
  77. var votess="";
  78. var color="#ddd";
  79. if (resp.status != 200 && resp.status != 304) {
  80. ratingTxt = '** ' + resp.status + ' Error!';
  81. }
  82. else {
  83.  
  84. // var parser = new DOMParser();
  85. // var xmlDoc = parser.parseFromString(resp.responseText, "application/xml");
  86. var votesM = resp.responseText.match (/bjjENQ">(.*)<\/div><\/div><\/div><\/span><\/a><\/div><div data-testid="hero-rating-bar__user-rating/);
  87. var ratingM = resp.responseText.match (/iZlgcd">(.*)<\/span><span>\/<!-- -->10<\/span><\/div><div class=\"sc-bde20123-5/);
  88. //console.log(ratingM);
  89. //console.log(votesM);
  90. /*
  91. var elem =
  92. doc.querySelector('.score-meta');
  93. doc.querySelector(ratingSelectorNew);
  94. resp.querySelector('.sc-e457ee34-1 .squoh');
  95. */
  96. isError = false;
  97.  
  98. justrate = ratingM[1].substring(0,3);
  99. votess = votesM[1].substring(0,4);
  100.  
  101. ratingTxt =justrate + " - " + votess+ " ";
  102.  
  103.  
  104. colnumber = Math.floor(justrate);
  105. }
  106.  
  107.  
  108.  
  109. color = ["#Faa","#Faa","#Faa","#F88", "#F88","#FAA", "#FF7","#7E7", "#5e5", "#0e0", "#ddd"]
  110. var resltSpan = document.createElement ("span");
  111. resltSpan.innerHTML = '<b><font style="color:black; background-color:'+ color[colnumber] + ';">' + ratingTxt + '</font></b>&nbsp;';
  112. // resltSpan.innerHTML = '<b><font style="background-color:' + justrate + '">' + ' [' + ratingTxt + '] </font></b>&nbsp;';
  113.  
  114.  
  115. if (isError) resltSpan.style.color = 'red';
  116.  
  117. //var targetLink = resp.context;
  118. //console.log ("targetLink: ", targetLink);
  119.  
  120. targetLink.parentNode.insertBefore (resltSpan, targetLink);
  121. }
  122.  
  123. //--- Create the continue button
  124. var continueBttn = document.createElement ("button");
  125. continueBttn.innerHTML = "continueRatings";
  126.  
  127. continueBttn.addEventListener ("click", function (){
  128. fetchedLinkCnt = 0;
  129. continueBttn.style.display = 'none';
  130. processIMDB_Links ();
  131. },
  132. false
  133. );
  134.  
  135. processIMDB_Links ();