vivalemuc / Sorare Data Enhancer

// ==UserScript==
// @name        Sorare Data Enhancer
// @version     1.0.2
// @description Enhances the Sorare Ongoing Auctions
// @license     MIT
// @author      Vivalemuc
// @match       https://www.soraredata.com/ongoingAuctions
// @match       https://www.soraredata.com/publicOffers
// @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant    GM_xmlhttpRequest
// ==/UserScript==


(function() {
    'use strict';

    var myFunctions = window.myFunctions = {};
    myFunctions.reportAJAX_Error = function (rspObj){
        console.error (`TM scrpt => Error ${rspObj.status}!  ${rspObj.statusText}`);
    }

   


    myFunctions.waitForGrids = function (){
        const grids = document.getElementsByClassName("infinite-scroll-component");

        if (grids.length) {
            console.log("Grid found");

            var buttonZone = document.getElementsByClassName("nnjwZ")[0];
            if(buttonZone == undefined){
                buttonZone = document.getElementsByClassName("cbgmAf")[0];
            }
            buttonZone.style.gridTemplateColumns = "auto auto auto auto auto";
            var aP = document.createElement('button');
            // Create the text node for anchor element.
            var linkP = document.createTextNode("Show tips");
            // Append the text node to anchor element.
            aP.appendChild(linkP);
            aP.setAttribute('onclick','myFunctions.gridInformation();'); // for FF
            aP.onclick = function() {myFunctions.gridInformation();}; // for IE
            aP.setAttribute('class','StyledBox-sc-13pk1d4-0 eZKQvz');
            aP.setAttribute('style','background: rgb(244, 82, 6);border: 1px solid rgb(244, 82, 6);border-radius: 5Px;margin: 10px;color: white;padding: 8px 10px !important;')
            buttonZone.appendChild(aP);


        } else {
            setTimeout(myFunctions.waitForGrids,500); // try again in 300 milliseconds
        }
    }



    myFunctions.gridInformation = function (){
        const grids = document.getElementsByClassName("infinite-scroll-component");

        window.grid = grids[0].children[0];

        console.log(window.grid);




        for (var i = 0; i < window.grid.children.length; i++) {
            var color = "white";
            var priceInfo = window.grid.children[i].getElementsByClassName("iGcTdH")[0];
            var price = priceInfo.children[1];
            var bestPrice = priceInfo.children[2];
            console.log("Price");
            var priceAuction = price.textContent.match("^[0-9.]*")[0];
            var priceMarket =bestPrice.textContent.match("^[0-9.]*")[0];
            if(parseFloat(priceAuction) <= parseFloat(priceMarket)){
                console.log("Interessant");
                color="#fff176";
            }
             console.log("Color intermediaire :"+color);
            if(color == "#fff176"){
                var pourcentPlayer = window.grid.children[i].getElementsByClassName("pl-0.5")[0];
                var pourcent = pourcentPlayer.textContent.match("[0-9]+")[0];
                var injury = window.grid.children[i].getElementsByClassName("gbynNy")[1];
                if(injury == undefined){
                     injury = window.grid.children[i].getElementsByClassName("gbynNy")[0];
                }
                console.log(injury);
                if(injury.children[0].children[0].getAttribute("data-icon")=="check-circle" && parseInt(pourcent) >= 80){
                    color="#ffa726";
                }
            }

            if(color == "#ffa726"){
                var ratingPlayer = window.grid.children[i].getElementsByClassName("w-4")[0];
                var rating = ratingPlayer.textContent.match("[0-9]+")[0];

                console.log(rating);
                if(parseInt(rating) >= 50){
                    color="#9ccc65";
                }
            }

            if(color == "#9ccc65"){
                var rating15Player = window.grid.children[i].getElementsByClassName("w-4")[1];
                console.log(rating15Player)
                if(rating15Player != undefined){
                    var ratingString = rating15Player.textContent.match("[0-9]+");
                    if(ratingString != null){
                        var rating15 = rating15Player.textContent.match("[0-9]+")[0];

                        var pourcent15Player = window.grid.children[i].getElementsByClassName("pl-0.5")[1];
                        var pourcent15 = pourcent15Player.textContent.match("[0-9]+")[0];

                        if(parseInt(rating15) >= 50 && parseInt(pourcent15)>=80){
                            color="#388e3c";
                        }
                    }
                }
            }
            if(color == "#388e3c"){
               if(parseFloat(priceAuction) <= parseFloat(priceMarket)*0.9){
                   color="red";
               }

            }
            

            window.grid.children[i].style.border="thick solid "+color;
            window.grid.children[i].style.borderRadius ="18px 18px 18px 18px";


        }

    }
    console.log("Script custom");
    myFunctions.waitForGrids();



}
)();