亚马逊评论计算(Amazon Review Calculation)

计算每个评分等级的Review数量,以及如果需要提成评分至4.3分需要补充多少个五星好评。

Asenna tämä skripti?
Author's suggested script

Saatat myös pitää

Asenna tämä skripti
// ==UserScript==
// @name         亚马逊评论计算(Amazon Review Calculation)
// @namespace    
// @version      1.41
// @description  计算每个评分等级的Review数量,以及如果需要提成评分至4.3分需要补充多少个五星好评。
// @author       Monty
// @match        https://www.amazon.com/*/dp/*
// @match        https://www.amazon.com/dp/*
// @match        https://www.amzn.com/*/dp/*
// @match        https://www.amazon.co.uk/*/dp/*
// @match        https://www.amazon.de/*/dp/*
// @match        https://www.amazon.de/dp/*
// @match        https://www.amazon.fr/*/dp/*
// @match        https://www.amazon.fr/dp/*
// @match        https://www.amazon.it/*/dp/*
// @match        https://www.amazon.ca/*/dp/*
// @match        https://www.amazon.com.mx/*/dp/*
// @match        https://www.amazon.es/*/dp/*
// @match        https://www.amazon.co.jp/*/dp/*
// @match        https://www.amazon.in/*/dp/*
// @match        https://www.amazon.com.br/*/dp/*
// @match        https://www.amazon.nl/*/dp/*
// @match        https://www.amazon.com.au/*/dp/*
// @match        https://www.amazon.ae/*/dp/*
// @match        https://www.amazon.eg/*/dp/*
// @match        https://www.amazon.pl/*/dp/*
// @match        https://www.amazon.se/*/dp/*
// @match        https://www.amazon.sg/*/dp/*
// @match        https://www.amazon.com.tr/*/dp/*
// @match        https://www.amazon.cn/*/dp/*
// @match        https://www.amazon.sa/*/dp/*
// @match        https://www.amazon.com.be/*/dp/*
// @icon         
// @grant GM_getValue
// @grant GM_setValue
// @license      MIT
// ==/UserScript==

console.log('MONTY-----')


function toDecimal(percent) {
    console.log('-----percent:'+percent)
    let num = percent.replace('%', '');
    num = num / 100;
    return num;
    }

function jisuanpingfen(numList){
    fens = [5,4,3,2,1]
    let sum =0;
    for (let index = 0; index < fens.length; index++) {
        sum+=fens[index]*numList[index]
    }
    return sum.toFixed(2)
}

function getReviewsCount(reviewCountText){
    // let sum_review = sum_review_counts_ele.textContent.trim().split(' ')[0]
    var currentUrl = window.location.href;
    console.log("當前頁面:"+currentUrl);
    let sum_review;
    console.log("reviewCountText:"+reviewCountText);
    if(currentUrl.indexOf('amazon.fr')>-1){
        sum_review=reviewCountText.trim().split(' évaluations')[0]
    }else{
        sum_review= reviewCountText.trim().split(' ')[0]
    }

    if(sum_review.indexOf(',')>-1){
        sum_review = sum_review.replace(',','')
    }else if(sum_review.indexOf('.')>-1){
        sum_review = sum_review.replace('.','')
    }else if(sum_review.indexOf(' ')>-1){
        sum_review = sum_review.replace(' ','')
    }
    return sum_review

}


function getReviews(){
    let rev_html = document.querySelector('#cm_cr_dp_d_rating_histogram')
    console.log('评论内容-----')
    console.log(rev_html)

    let sum_review_counts_ele= document.querySelector('#cm_cr_dp_d_rating_histogram .averageStarRatingNumerical')
    
    let sum_review = getReviewsCount(sum_review_counts_ele.textContent)
    
    console.log('总评论数:'+sum_review)

    let ariaValueNowList = rev_html.querySelectorAll('tbody td.a-text-right')

    console.log('計算評論百分比-----ariaValueNowList:'+ariaValueNowList)
    let bils = []
    ariaValueNowList.forEach(element => {
        console.log(element.textContent)
        const bili = toDecimal(element.textContent)
        console.log('评论百分比:'+bili)
        bils.push(bili)
    });

    ri_eles = rev_html.querySelectorAll('tbody .a-text-right')
   
    console.log('計算評論百分比-----bils:'+bils)
    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)
        console.log('评论数:'+count)
        d.innerHTML = `(`+count.toFixed(0)+`)`//评论数量四舍五入不保留小数位
        console.log('评论数节点:'+d.textContent)
        element.appendChild(d)
    }


    fenshu = jisuanpingfen(bils)
    console.log('sum_review:', sum_review);
    let prompt_text = ''
    var score = GM_getValue("score")
    if(fenshu<score){
        console.log('fenshu:', fenshu);
        console.log('score:', score);
        try {
            bu = (sum_review*(score-fenshu)/(5-score)).toFixed(0)
           
            prompt_text = ', 还差 '+bu+' 个5星好评才能达到'+score+'分.'
            
         } catch (e) {
            // 捕获到异常后的处理逻辑
            console.log('捕获到异常:', e.message);
       
         }
       
    }
    
    // 创建一个自己的结构
    let revv = document.createElement("div")
    revv.classList.add("a-link-normal")
    // revv.classList.add("a-box-inner")
    // revv.classList.add("a-box")
    // revv.classList.add("a-last")
    // 给 example 这个 div 设置类名
    // revv.classList.add("rev")
    revv.innerHTML = `总评论数:`+sum_review+`, 评分:`+fenshu+prompt_text
    console.log('插入計算結果-----'+prompt_text)
    sum_review_counts_ele.appendChild(revv)
}

function setBtnClick(){
    // document.querySelector('#mt-btn').addEventListener("click",function(){
    //     var score = document.querySelector('#mt-rinput').value
    //     GM_setValue("score", score)
    // })
};



(function() {
    'use strict';
    let score = 4.3
    GM_setValue("score", score)
    console.log('亚马逊评论计算(Amazon Review Calculation)')
    var reviewPanle = null
    var count = 0
    var timer = setInterval(function(){
        count+=1
        console.log('亚马逊评论计算(Amazon Review Calculation),查找评论组件 '+count+' 次')
        reviewPanle = document.querySelector('.averageStarRatingNumerical')
        if (reviewPanle!=null){
            console.log('定時器執行 '+count+'次')
            clearInterval(timer)
            // let input = document.createElement("input")
            // input.className="a-input-text monty"
            // input.style = 'width: 70px;margin-left: 10px;margin-right: 10px;'
            // input.id = 'mt-rinput'
            // input.value = 4.3
            // document.querySelector('.averageStarRatingNumerical').appendChild(input)

            // let btn = document.createElement('button')
            // btn.id = 'mt-btn'
            // btn.innerHTML = '计算'
            // document.querySelector('.averageStarRatingNumerical').appendChild(btn)

            getReviews()

            // setBtnClick()
        }
    },1000)
})();