AliExpress Rating Calculator

Takes product star ratings and puts a numerical values over them.

// ==UserScript==
// @name        AliExpress Rating Calculator
// @description  Takes product star ratings and puts a numerical values over them.
// @match        https://www.aliexpress.com/
// @match        https://www.aliexpress.com/*
// @match        https://*.aliexpress.com/*
// @grant       none
// @version     0.1
// @author      Anon
// @noframes
// @license      MIT
// @icon         https://www.google.com/s2/favicons?sz=64&domain=aliexpress.com
// @namespace https://greasyfork.org/users/1309046
// ==/UserScript==


(function() {
    'use strict';

    let scrollCount = 0;

    // Function to calculate and display the rating for each star list
    function calculateRatings() {
        // Select all elements with class containing "multi--starList--"
        const starListElements = document.querySelectorAll('[class*="multi--starList--"]');

        starListElements.forEach(starList => {
            // Select all elements with class containing "multi--evalutionModal--" within the current star list
            const modalElements = starList.querySelectorAll('[class*="multi--evalutionModal--"]');

            let totalWidth = 0;
            let starCount = 0;

            modalElements.forEach(modal => {
                // Select all the star elements within the current modal element
                const starElements = modal.querySelectorAll('[class*="multi--progress--"]');

                // Loop through each star element and sum up their widths
                starElements.forEach(star => {
                    const width = parseInt(star.style.width);
                    totalWidth += width;
                    starCount++;
                });
            });

            // Calculate the rating (assuming each full star is represented by 10px width)
            const rating = totalWidth / 10;

            // Log the rating to the console
            //console.log(`Rating for star list: ${rating.toFixed(1)} stars`);

            // Create a div to display the rating
            const ratingDisplay = document.createElement('div');
            ratingDisplay.style.backgroundColor = 'black';
            ratingDisplay.style.color = 'white';
            ratingDisplay.style.padding = '3px';
            ratingDisplay.style.position = 'absolute';
            ratingDisplay.style.zIndex = '1';
            ratingDisplay.textContent = `${rating.toFixed(1)} stars`;

            // Append the rating display
            starList.appendChild(ratingDisplay);
        });
    }

    // Run the function after the page has loaded
    window.addEventListener('load', calculateRatings);

    // Function to handle scroll events
    function handleScroll() {
        scrollCount++;
        if (scrollCount >= 10) {
            scrollCount = 0;
            calculateRatings();
        }
    }

    // Run the function after the page has loaded
    window.addEventListener('load', calculateRatings);

    // Add scroll event listener
    window.addEventListener('scroll', handleScroll);
})();