imdb.com — Ratings from other websites

Show ratings on the imdb.com movie page from Filmweb, Rotten Tomatoes and Metacritic.

当前为 2021-11-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name imdb.com — Ratings from other websites
  3. // @description Show ratings on the imdb.com movie page from Filmweb, Rotten Tomatoes and Metacritic.
  4. // @author Rafal Enden
  5. // @namespace https://github.com/rafenden
  6. // @homepageURL https://github.com/rafenden/userscripts/blob/master/imdb-ratings-from-other-websites
  7. // @supportURL https://github.com/rafenden/userscripts/issues
  8. // @license MIT
  9. // @version 1.0
  10. // @match https://www.imdb.com/title/*
  11. // @connect www.filmweb.pl
  12. // @connect www.omdbapi.com
  13. // @grant GM_xmlhttpRequest
  14. // ==/UserScript==
  15.  
  16. function getMovieID() {
  17. const IMDbID_RegEx = /\/title\/(tt\d{7})\//
  18. return IMDbID_RegEx.exec(window.location.href)[1]
  19. }
  20.  
  21. function getMovieTitle() {
  22. return document.querySelector('[data-testid="hero-title-block__title"]').textContent
  23. }
  24.  
  25. function getMovieYear() {
  26. return document.querySelector('[data-testid="hero-title-block__metadata"]').firstElementChild.firstElementChild.textContent
  27. }
  28.  
  29. function addRating(siteName, url, rating, count) {
  30. //const ratingsWrapper = document.querySelector('.RatingBar__ButtonContainer-sc-85l9wd-1')
  31. const ratingsWrapper = document.querySelector('[class*="TitleBlock__HideableRatingBar"]')
  32. const ratingItem = document.querySelector('[data-testid="hero-rating-bar__aggregate-rating"]')
  33.  
  34. const newRatingItem = ratingItem.cloneNode(true)
  35. newRatingItem.firstElementChild.innerText = siteName
  36. newRatingItem.querySelector('[class*="RatingBarButtonBase__Button"]').setAttribute('href', url)
  37. newRatingItem.querySelector('[data-testid="hero-rating-bar__aggregate-rating__score"]').firstElementChild.innerText = rating
  38. newRatingItem.querySelector('[class*="AggregateRatingButton__TotalRatingAmount"]').innerText = count || ''
  39.  
  40. ratingsWrapper.prepend(newRatingItem)
  41. }
  42.  
  43. function showFilmwebRating() {
  44. GM_xmlhttpRequest({
  45. method: 'GET',
  46. url: `https://www.filmweb.pl/search?q=${getMovieTitle()}+${getMovieYear()}`,
  47. onload: (response) => {
  48. const parser = new DOMParser();
  49. const doc = parser.parseFromString(response.responseText, 'text/html');
  50.  
  51. const rating = doc.querySelector('.rateBox__rate').textContent
  52. const count = doc.querySelector('.rateBox__votes--count').textContent
  53. const url = doc.querySelector('.filmPreview__link').getAttribute('href')
  54.  
  55. addRating('Filmweb', `https://www.filmweb.pl${url}`, rating, count)
  56. },
  57. })
  58. }
  59.  
  60. function showOtherRatings() {
  61. const OMDBAPI_API_KEY = '6be019fc'
  62. GM_xmlhttpRequest({
  63. method: 'GET',
  64. url: `http://www.omdbapi.com/?apikey=${OMDBAPI_API_KEY}&tomatoes=true&i=${getMovieID()}`,
  65. onload: (response) => {
  66. const json = JSON.parse(response.responseText)
  67. console.log(json)
  68. if (json) {
  69. if (json.Error) {
  70. console.error(`Error: ${json.Error}`)
  71. }
  72. else {
  73. json.Ratings.forEach((rating) => {
  74. if (rating.Source === 'Rotten Tomatoes' && json.tomatoURL && json.tomatoURL !== 'N/A') {
  75. addRating('Rotten Tomatoes', json.tomatoURL, rating.Value)
  76. }
  77. else if (rating.Source === 'Metacritic') {
  78. addRating('Metacritic', `https://www.metacritic.com/search/all/${json.Title}/results`, rating.Value)
  79. }
  80. });
  81. }
  82. }
  83. else {
  84. console.error('Unknown error')
  85. }
  86. }
  87. })
  88. }
  89.  
  90. showFilmwebRating()
  91. showOtherRatings()
  92.  
  93. document.querySelectorAll('[data-testid="hero-rating-bar__aggregate-rating__score"]').forEach((element) => {
  94. element.lastElementChild.remove()
  95. })