亚马逊评论计算(Amazon Review Calculation)
// ==UserScript==
// @name 亚马逊评论计算(Amazon Review Calculation)
// @version 0.2
// @description 计算每个评分等级的Review数量,以及如果需要提成评分至4.3分需要补充多少个五星好评。
// @author Monty
// @match https://www.amazon.com/gp/*
// @match https://www.amazon.com/*/dp/*
// @match https://www.amazon.com/dp/*
// @match https://www.amazon.co.jp/*/dp/*
// @match https://www.amazon.co.jp/dp/*
// @match https://www.amazon.de/*/dp/*
// @match https://www.amazon.de/dp/*
// @match https://www.amazon.co.uk/*/dp/*
// @match https://www.amazon.co.uk/dp/*
// @match https://www.amazon.fr/*/dp/*
// @match https://www.amazon.fr/dp/*
// @match https://www.amazon.com.au/*/dp/*
// @match https://www.amazon.com.au/dp/*
// @match https://www.amazon.in/*/dp/*
// @match https://www.amazon.in/dp/*
// @match https://www.amazon.it/*/dp/*
// @match https://www.amazon.it/dp/*
// @match https://www.amazon.es/*/dp/*
// @match https://www.amazon.es/dp/*
// @match https://www.amazon.ca/*/dp/*
// @match https://www.amazon.ca/dp/*
// @match https://www.amazon.com.mx/*/dp/*
// @match https://www.amazon.com.mx/dp/*
// @icon
// @grant none
// @license MIT
// ==/UserScript==
function toDecimal(percent) {
let num = percent.replace('%', '');
num = num / 100;
return num;
}
function jisuanpingfen(numList){
fens = [5,4,3,2,1]
let sum =0;
let score = 0;
for (let index = 0; index < fens.length; index++) {
sum+=fens[index]*numList[index]
score+=numList[index]
}
return (sum/score).toFixed(2)
}
function getReviews(){
let rev_html = document.querySelector('.cr-widget-TitleRatingsAndHistogram')
console.log('评论内容-----')
console.log(rev_html)
let sum_review_counts_ele= document.querySelector('.cr-widget-TitleRatingsAndHistogram .averageStarRatingNumerical')
let sum_review = sum_review_counts_ele.textContent.trim().split(' ')[0]
console.log('总评论数:'+sum_review)
let ariaValueNowList = rev_html.querySelectorAll('.a-declarative tbody .a-meter')
let bils = []
ariaValueNowList.forEach(element => {
const bili = toDecimal(element.ariaValueNow)
console.log('评论百分比:'+bili)
bils.push(bili)
});
ri_eles = rev_html.querySelectorAll('.a-declarative tbody .a-text-right .a-size-base .a-link-normal')
let counts = []
for (let index = 0; index < bils.length; index++) {
const element = ri_eles[index];
d = document.createElement("span")
count = parseInt(sum_review)*bils[index]
counts.push(count)
d.innerHTML = `(`+count.toFixed(0)+`)`//评论数量四舍五入不保留小数位
element.appendChild(d)
}
fenshu = jisuanpingfen(counts)
let prompt_text = ''
if(fenshu<4.3){
bu = (sum_review*(4.3-fenshu)/(5-4.3)).toFixed(0)
prompt_text = ', 如果你想达到4.3分, 需要补充 '+bu+' 个五星评论.'
}
// 创建一个自己的结构
let revv = document.createElement("p")
revv.classList.add("a-link-normal")
// 给 example 这个 div 设置类名
// revv.classList.add("rev")
revv.innerHTML = `总评论数:`+sum_review+`, 评分:`+fenshu+prompt_text
sum_review_counts_ele.appendChild(revv)
}
(function() {
'use strict';
console.log('亚马逊评论计算(Amazon Review Calculation)')
getReviews()
})();