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-03-05 提交的版本,查看 最新版本

  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-03-03
  6. // @include *
  7. // @grant GM_xmlhttpRequest
  8. // @namespace Extra2020
  9. // ==/UserScript==
  10.  
  11.  
  12. var maxLinksAtATime = 48; //-- pages can have 100'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) )
  26. continue;
  27.  
  28. if (! currentLink.getAttribute ("data-gm-fetched") ){
  29. if (fetchedLinkCnt >= maxLinksAtATime){
  30. //--- Position the "continue" button.
  31. continueBttn.style.display = 'inline';
  32. currentLink.parentNode.insertBefore (continueBttn, currentLink);
  33. break;
  34. }
  35.  
  36. fetchTargetLink (currentLink); //-- AJAX-in the ratings for a given link.
  37.  
  38. //---Mark the link with a data attribute, so we know it's been fetched.
  39. currentLink.setAttribute ("data-gm-fetched", "true");
  40. fetchedLinkCnt++;
  41. }
  42. }
  43. }
  44.  
  45. function fetchTargetLink (linkNode) {
  46. //--- This function provides a closure so that the callbacks can work correctly.
  47.  
  48. /*--- Must either call AJAX in a closure or pass a context.
  49. But Tampermonkey does not implement context correctly!
  50. (Tries to JSON serialize a DOM node.)
  51. */
  52. GM_xmlhttpRequest ( {
  53. method: 'get',
  54. url: linkNode.href,
  55. //context: linkNode,
  56. onload: function (response) {
  57. prependIMDB_Rating (response, linkNode);
  58. },
  59.  
  60. onabort: function (response) {
  61. prependIMDB_Rating (response, linkNode);
  62. }
  63. } );
  64. }
  65.  
  66. function prependIMDB_Rating (resp, targetLink) {
  67. var isError = true;
  68. var ratingTxt = "*Err!";
  69. var colnumber = 0;
  70. var justrate =0;
  71. var votess="";
  72. var color="#ddd";
  73. if (resp.status != 200 && resp.status != 304) {
  74. ratingTxt = '** ' + resp.status + ' Error!';
  75. }
  76. else {
  77. var ratingM = resp.responseText.match (/squoh">(.*)<\/span><span>\/<!-- -->10<\/span><\/div><div class=\"sc-e457ee34-5/);
  78. var votesM = resp.responseText.match (/frEfSL">(.*)<\/div><\/div><\/div><\/span><\/a><\/div><div data-testid="hero-rating-bar/);
  79.  
  80.  
  81. isError = false;
  82.  
  83. justrate = ratingM[1].substring(0,3);
  84. votess = votesM[1].substring(0,4);
  85.  
  86. ratingTxt = justrate + " - " + votess+ " ";
  87.  
  88.  
  89. colnumber = Math.floor(justrate);
  90. }
  91.  
  92.  
  93.  
  94. color = ["#Faa","#Faa","#Faa","#F88", "#F88","#FAA", "#FF7","#7E7", "#5e5", "#0e0", "#ddd"]
  95. var resltSpan = document.createElement ("span");
  96. resltSpan.innerHTML = '<b><font style="color:black; background-color:'+ color[colnumber] + ';">' + ratingTxt + '</font></b>&nbsp;';
  97. // resltSpan.innerHTML = '<b><font style="background-color:' + justrate + '">' + ' [' + ratingTxt + '] </font></b>&nbsp;';
  98.  
  99.  
  100. if (isError) resltSpan.style.color = 'red';
  101.  
  102. //var targetLink = resp.context;
  103. //console.log ("targetLink: ", targetLink);
  104.  
  105. targetLink.parentNode.insertBefore (resltSpan, targetLink);
  106. }
  107.  
  108. //--- Create the continue button
  109. var continueBttn = document.createElement ("button");
  110. continueBttn.innerHTML = "continueRatings";
  111.  
  112. continueBttn.addEventListener ("click", function (){
  113. fetchedLinkCnt = 0;
  114. continueBttn.style.display = 'none';
  115. processIMDB_Links ();
  116. },
  117. false
  118. );
  119.  
  120. processIMDB_Links ();