Amazon - Goodreads metadata

Shows the ratings from Goodreads on Amazon book pages

  1. // ==UserScript==
  2. // @name Amazon - Goodreads metadata
  3. // @namespace https://github.com/bricemciver/GreasemonekeyScripts
  4. // @description Shows the ratings from Goodreads on Amazon book pages
  5. // @license MIT
  6. // @version 0.0.2
  7. // @match https://amazon.com/*
  8. // @match https://*.amazon.com/*
  9. // @match https://amazon.co.uk/*
  10. // @match https://*.amazon.co.uk/*
  11. // @match https://amazon.ca/*
  12. // @match https://*.amazon.ca/*
  13. // @match https://amazon.de/*
  14. // @match https://*.amazon.de/*
  15. // @match https://amazon.fr/*
  16. // @match https://*.amazon.fr/*
  17. // @match https://amazon.es/*
  18. // @match https://*.amazon.es/*
  19. // @match https://amazon.it/*
  20. // @match https://*.amazon.it/*
  21. // @match https://amazon.co.jp/*
  22. // @match https://*.amazon.co.jp/*
  23. // @match https://amazon.cn/*
  24. // @match https://*.amazon.cn/*
  25. // @match https://amazon.com.br/*
  26. // @match https://*.amazon.com.br/*
  27. // @match https://amazon.in/*
  28. // @match https://*.amazon.in/*
  29. // @match https://amazon.com.mx/*
  30. // @match https://*.amazon.com.mx/*
  31. // @match https://amazon.com.au/*
  32. // @match https://*.amazon.com.au/*
  33. // @grant GM_xmlhttpRequest
  34. // @grant GM.xmlHttpRequest
  35. // @icon https://www.google.com/s2/favicons?sz=64&domain=amazon.com
  36. // ==/UserScript==
  37.  
  38.  
  39.  
  40. /* jshint esversion: 6 */
  41. "use strict";
  42. (() => {
  43. // src/main/amazon-goodreads-meta/amazon-goodreads-meta.user.ts
  44. (() => {
  45. const asinRegex = /\/([A-Z0-9]{10})/;
  46. const findASIN = () => {
  47. const asinArray = [];
  48. const array = asinRegex.exec(document.location.pathname);
  49. const asin = array && array.length > 1 ? array[1] : "";
  50. console.log(`ASIN in pathname: ${asin}`);
  51. const dp = document.getElementById("dp");
  52. if (dp == null ? void 0 : dp.className.includes("book")) {
  53. asinArray.push(asin);
  54. } else {
  55. const images = document.getElementsByTagName("img");
  56. const coverImages = Array.from(images).filter((item) => item.classList.contains("cover-image"));
  57. coverImages.forEach((image) => {
  58. const parentElem = image.parentElement;
  59. if (parentElem instanceof HTMLAnchorElement) {
  60. const link = parentElem.href;
  61. const ciArray = asinRegex.exec(link);
  62. const ciAsin = ciArray && ciArray.length > 1 ? ciArray[1] : "";
  63. console.log(`ASIN on book image: ${ciAsin}`);
  64. asinArray.push(ciAsin);
  65. }
  66. });
  67. }
  68. return asinArray;
  69. };
  70. const findInsertPoint = () => {
  71. const insertPoint = [];
  72. const reviewElement = document.getElementById("averageCustomerReviews");
  73. if (reviewElement) {
  74. insertPoint.push(reviewElement);
  75. } else {
  76. const reviewArray = document.getElementsByClassName("pf-image-w");
  77. insertPoint.push(...Array.from(reviewArray));
  78. }
  79. return insertPoint;
  80. };
  81. const insertElement = (isbn, insertPoint) => {
  82. GM.xmlHttpRequest({
  83. method: "GET",
  84. url: `https://www.goodreads.com/book/isbn/${isbn}`,
  85. onload(response) {
  86. const node = new DOMParser().parseFromString(response.responseText, "text/html");
  87. const head = document.getElementsByTagName("head")[0];
  88. const styles = Array.from(node.getElementsByTagName("link")).filter((item) => item.rel === "stylesheet");
  89. styles.forEach((item) => {
  90. item.href = item.href.replace("amazon", "goodreads");
  91. head.appendChild(item);
  92. });
  93. const meta = node.getElementById("ReviewsSection");
  94. if (meta) {
  95. const rating = meta.querySelector("div.RatingStatistics");
  96. if (rating) {
  97. Array.from(rating.getElementsByTagName("a")).forEach((item) => {
  98. item.href = response.finalUrl + item.href.replace(item.baseURI, "");
  99. return item;
  100. });
  101. Array.from(rating.getElementsByTagName("span")).forEach((item) => {
  102. item.classList.replace("RatingStar--medium", "RatingStar--small");
  103. item.classList.replace("RatingStars__medium", "RatingStars__small");
  104. });
  105. Array.from(rating.getElementsByTagName("div")).filter((item) => item.classList.contains("RatingStatistics__rating")).forEach((item) => {
  106. item.style.marginBottom = "-0.8rem";
  107. item.style.fontSize = "2.2rem";
  108. });
  109. const labelCol = document.createElement("div");
  110. labelCol.classList.add("a-column", "a-span12", "a-spacing-top-small");
  111. const labelRow = document.createElement("div");
  112. labelRow.classList.add("a-row", "a-spacing-small");
  113. labelRow.textContent = "Goodreads";
  114. const lineBreak = document.createElement("br");
  115. labelCol.appendChild(labelRow);
  116. labelRow.appendChild(lineBreak);
  117. labelRow.appendChild(rating);
  118. insertPoint.appendChild(labelCol);
  119. }
  120. }
  121. }
  122. });
  123. };
  124. const main = () => {
  125. const ASIN = findASIN();
  126. const insertPoint = findInsertPoint();
  127. for (let i = 0; i < ASIN.length && i < insertPoint.length; i++) {
  128. const insertPointElement = insertPoint[i].parentElement;
  129. if (insertPointElement) {
  130. insertElement(ASIN[i], insertPointElement);
  131. }
  132. }
  133. };
  134. main();
  135. })();
  136. })();
  137. //# sourceMappingURL=amazon-goodreads-meta.user.js.map

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址