Amazon - Show absolute review numbers

Adds the number of reviews to each rating separately

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Amazon - Show absolute review numbers
// @namespace    graphen
// @version      1.2.6
// @description  Adds the number of reviews to each rating separately
// @license      MIT
// @author       Graphen
// @include      /^https?:\/\/(www|smile)\.amazon\.(cn|in|co\.jp|sg|se|ae|fr|de|pl|it|nl|es|co\.uk|ca|com(\.(mx|au|br|tr|be))?)\/.*(dp|gp\/(product|video)|exec\/obidos\/ASIN|o\/ASIN|product-reviews)\/.*$/
// @grant        none
// @noframes
// @icon         https://www.amazon.com/favicon.ico
// ==/UserScript==

/* jshint esversion: 6 */

// Testpages:
// https://www.amazon.de/s?k=roccat+kone+pure+2017&__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&qid=1556553434&ref=sr_pg_1
// https://www.amazon.de/dp/B078S8YZZ6/

(function(doc) {
    'use strict';

    var totalReviewCount = doc.querySelector('[data-hook="total-review-count"]').innerText;
    var arrPercentages = Array.from(doc.querySelectorAll("#histogramTable .a-text-right"));

    if (totalReviewCount && arrPercentages) {
        // Sanitize totalReviewCount
        // Remove all non-digits
        totalReviewCount = totalReviewCount.replace(/\D/g, '');
        // Convert string to integer
        totalReviewCount = parseInt(totalReviewCount, 10);
        // Check for nonsense (Most reviewed product has ~100000 at the moment)
        if (totalReviewCount < 250000) {
            for (var e of arrPercentages) {
                let percentValue = e.innerText;
                // Get rid of percentage sign and convert string to integer
                percentValue = parseInt(percentValue, 10);
                // Calculate absolute review count
                percentValue = Math.round(percentValue * totalReviewCount / 100);
                // Cancel if nonsense
                if (percentValue > totalReviewCount || percentValue < 0) {
                    break;
                }
                // Append calculated value to visible node
                var absNum = doc.createTextNode(" (" + percentValue + ")");
                e.appendChild(absNum);
            }
        }
    }

    // Insert own stylesheet
    let reviewStyle = doc.createElement("style");
    reviewStyle.innerHTML = "#histogramTable td:last-of-type { text-align: right !important; }";
    doc.head.appendChild(reviewStyle);


})(document);