Thork / Sorare - Trading Long Terme

// ==UserScript==
// @name         Sorare - Trading Long Terme
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Identify the underrated players
// @author       You
// @include        /^(?:https?:\/\/)?(?:www\.)?soraredata.com\/publicOffers/
// @icon         https://www.google.com/s2/favicons?domain=soraredata.com
// @grant        none
// @require http://code.jquery.com/jquery-3.4.1.min.js
// @license MIT
// ==/UserScript==
(function() {
    'use strict';

    window.addEventListener("load", () => {
quickCheckOffers();
        addButton("Quick Check offers");
        addBlueLegend("Blue");
        addGreenLegend("Green");
        addYellowLegend("Yellow");
    });

    function quickCheckOffers() {
        $('.infinite-scroll-component > div > div').each(function(index) {

            let box = $(this);
            let infoBox = box.find('.iMQCHk');
            // valeurs
            let ethValues = infoBox.children("div:first")
            let ethVal = ethValues.find('div:eq(1) p').text();

            let num = ethVal.match(/[\d\.]+/g);
            let ethValNum = parseFloat(num[0]);

            let marketVal = ethValues.find('div[data-tip="Best market price"] p').text();
            let marketValNum = marketVal.match(/[\d\.]+/g);
            if (marketValNum == null) {
                marketValNum = 10000000;
            } else {
                marketValNum = parseFloat(marketValNum[0]);
            }

            let monthVal = ethValues.find('div[data-tip="3 days average"] p').text();
            let monthValNum = monthVal.match(/[\d\.]+/g);
            if (monthValNum == null) {
                monthValNum = 10000000;
            } else {
                monthValNum = parseFloat(monthValNum[0]);
            }

            //percent 5 games.
            let pointValues = infoBox.find('.eZKQvz');
            let percent5Game = pointValues.find("span[data-tip='% of games played over the past 5 games']").text();
            let percent5GameNum = percent5Game.match(/[\d\.]+/g);
            percent5GameNum = percent5GameNum && percent5GameNum[0]?parseInt(percent5GameNum[0]):0;

            //points 5 games.
            let points5Game = pointValues.find("span[data-tip='Average score over the past 5 games']").text();
            let points5GameNum = points5Game.match(/[\d\.]+/g);
            points5GameNum = points5GameNum && points5GameNum[0]?parseInt(points5GameNum[0]):0;

//percent 15 games.
let percent15Game = pointValues.find("span[data-tip='% of games played over the past 15 games']").text();
            let percent15GameNum = percent15Game.match(/[\d\.]+/g);
            percent15GameNum = percent15GameNum && percent15GameNum[0]?parseInt(percent15GameNum[0]):0;

            //points 15 games.
            let points15Game = pointValues.find("span[data-tip='Average score over the past 15 games']").text();
            let points15GameNum = points15Game.match(/[\d\.]+/g);
            points15GameNum = points15GameNum && points15GameNum[0]?parseInt(points15GameNum[0]):0;

            if ((ethValNum <= marketValNum) && (ethValNum <= monthValNum) && points15GameNum >= (points5GameNum+10) && percent15GameNum > 40) {
                box.css('border', '5px solid  rgb(0,0,255)');
            } else if ((ethValNum <= marketValNum) && (ethValNum <= monthValNum) && points15GameNum >= points5GameNum && percent15GameNum > 40) {
                box.css('border', '5px solid rgb(0,255,0)');
            } else if ((ethValNum <= marketValNum) && (ethValNum > monthValNum) && points15GameNum >= (points5GameNum+10) && percent15GameNum > 40) {
                box.css('border', '5px solid yellow');
            }
            else {
                box.css('border', '5px solid grey');
            }
        });
    }

    function addButton(text, onclick, cssObj) {
        cssObj = cssObj || {
            position: "fixed",
            bottom: "5%",
            right: "1%",
            "z-index": 3,
            fontWeight: "600",
            fontSize: "14px",
            backgroundColor: "#00cccc",
            color: "white",
            border: "none",
            padding: "10px 20px"
        };
        let button = document.createElement("button"),
            btnStyle = button.style;
        document.body.appendChild(button);
        button.innerHTML = text;
        // Setting function for button when it is clicked.
        button.onclick = quickCheckOffers;
        Object.keys(cssObj).forEach(key => (btnStyle[key] = cssObj[key]));
        return button;
    }

    function showBlueInfo()
    {
        alert("- Lowest price on the market\n- Price below the average of the last month\n- L15 at least 10 points higher than L5\n- At least 40% of the last 15 games played");
    }

    function showGreenInfo()
    {
        alert("- Lowest price on the market\n- Price below the average of the last month\n- L15 less than 10 points higher than L5\n- At least 40% of the last 15 games played");
    }

    function showYellowInfo()
    {
        alert("- Lowest price on the market\n- Price above last month's average\n- L15 more than 10 points above L5\n- At least 40% of the last 15 games played");
    }

    function addBlueLegend(text, onclick, cssObj) {
        cssObj = cssObj || {
            position: "fixed",
            bottom: "10%",
            right: "1%",
            "z-index": 4,
            fontWeight: "600",
            fontSize: "14px",
            backgroundColor: "#55acee",
            color: "white",
            border: "none",
            padding: "10px 20px"
        };
        let button = document.createElement("button"), 
            btnStyle = button.style;
        document.body.appendChild(button);
        button.innerHTML = text;
        // Setting function for button when it is clicked.
        button.onclick = showBlueInfo;
        Object.keys(cssObj).forEach(key => (btnStyle[key] = cssObj[key]));
        return button;
    }

    function addGreenLegend(text, onclick, cssObj) {
        cssObj = cssObj || {
            position: "fixed",
            bottom: "15%",
            right: "1%",
            "z-index": 4,
            fontWeight: "600",
            fontSize: "14px",
            backgroundColor: "#78b159",
            color: "white",
            border: "none",
            padding: "10px 20px"
        };
        let button = document.createElement("button"),
            btnStyle = button.style;
        document.body.appendChild(button);
        button.innerHTML = text;
        // Setting function for button when it is clicked.
        button.onclick = showGreenInfo;
        Object.keys(cssObj).forEach(key => (btnStyle[key] = cssObj[key]));
        return button;
    }

    function addYellowLegend(text, onclick, cssObj) {
        cssObj = cssObj || {
            position: "fixed",
            bottom: "20%",
            right: "1%",
            "z-index": 4,
            fontWeight: "600",
            fontSize: "14px",
            backgroundColor: "#fdcb58",
            color: "white",
            border: "none",
            padding: "10px 20px"
        };
        let button = document.createElement("button"),
            btnStyle = button.style;
        document.body.appendChild(button);
        button.innerHTML = text;
        // Setting function for button when it is clicked.
        button.onclick = showYellowInfo;
        Object.keys(cssObj).forEach(key => (btnStyle[key] = cssObj[key]));
        return button;
    }

    setInterval(function(){
        quickCheckOffers();
    }, 500);

})();