BushRod / Sorare - quick check Offers

// ==UserScript==
// @name         Sorare - quick check Offers
// @namespace    http://tampermonkey.net/
// @version      0.1.12
// @description  Quick check of the offers in SorareData page.
// @author       You
// @include        /^(?:https?:\/\/)?(?:www\.)?soraredata.com\/liveMarket/
// @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
// @updateURL https://openuserjs.org/meta/BushRod/Sorare_-_quick_check_Offers.meta.js
// ==/UserScript==
(function () {
  'use strict';

  addButton("Quick Check offers<br/>(only works for football cards)");

  function quickCheckOffers() {

    /*let sportVal = $('input[placeholder="Select one scarcity or more"]').val();
    if (sportVal != 'Sorare Football') {
      alert('You must select the "Sorare Football" on Sports Filter (don\'t forget to click on the "filter" button aswell)');
      return;
    }*/

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

      let box = $(this);
      let styledBox = box.children("div:first");
      let primary = styledBox.children("div:first");
      let secondary = primary.children("div:first");
      let infoBox = secondary.children('div:eq(1)');
      let infoBoxPlays = infoBox.children('div:eq(1)');
      let imageBox = secondary.children('div:eq(0)');
      // valeurs
      let ethValues = infoBox.children("div:first");
      let ethVal = ethValues.find('div:eq(2) p').text();

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

      //3 days average
      let threedaysVal = ethValues.find('div[data-tip="3 days average"] p').text();
      let threedaysValNum = threedaysVal.match(/[\d\,.]+/g);
      threedaysValNum = threedaysValNum && threedaysValNum[0] ? parseFloat(threedaysValNum[0].replace(/\./g, '').replace(',', '.')) : 0;

      //1 week average
      let oneweekVal = ethValues.find('div[data-tip="1 week average"] p').text();
      let oneweekValNum = oneweekVal.match(/[\d\,.]+/g);
      oneweekValNum = oneweekValNum && oneweekValNum[0] ? parseFloat(oneweekValNum[0].replace(/\./g, '').replace(',', '.')) : 0;

      let pointValues = infoBox.children('div:eq(1)');

      //percent 5 games.
      let L5width = infoBoxPlays.children('div:eq(1)').children('div:eq(0)').children('a:eq(0)').children('div:eq(0)').children('div:eq(1)').children().children().children().attr('style');
      let percent5GameNum = L5width.replace(/\D/g, '');
      /*let percent5Game = pointValues.find('div[data-tip$="games played over the past 5 games"]').next('.text-center').text();
      let percent5GameNum = percent5Game.match(/[\d\,.]+/g);*/
      percent5GameNum = percent5GameNum ? parseInt(percent5GameNum) : 0;

      //percent 15 games.
      let L15width = infoBoxPlays.children('div:eq(1)').children('div:eq(1)').children('a:eq(0)').children('div:eq(0)').children('div:eq(1)').children().children().children().attr('style');
      let percent15GameNum = L15width.replace(/\D/g, '');
      /*let percent15Game = pointValues.find('div[data-tip$="games played over the past 15 games"]').next('.text-center').text();
      let percent15GameNum = percent15Game.match(/[\d\,.]+/g);*/
      percent15GameNum = percent15GameNum ? parseInt(percent15GameNum) : 0;

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

      // if there is 3days average or 1 week average AND diff beetween price and average higher than 10%, add triangle.
      let percentDiff = 0;
      if (threedaysValNum > 0) {
        let decreaseValue = threedaysValNum - ethValNum;
        percentDiff = (decreaseValue / threedaysValNum) * 100;
      }
      else if (oneweekValNum > 0) {
        let decreaseValue = oneweekValNum - ethValNum;
        percentDiff = (decreaseValue / oneweekValNum) * 100;
      }

      // if price better by 10%, super offer, append triangle.
      if (percentDiff >= 15) {
        $('<span class="triangle-topleft"></span>').appendTo(box);
      }
      else {
        //box.remove();
      }

      // If price <= best market price AND price <= average 3 days or average 1 week AND percent 5 games >= 80 AND points 5 games >= 45 = blue.
      if ((ethValNum <= marketValNum) && ((ethValNum <= threedaysValNum) || (ethValNum <= oneweekValNum)) && percent5GameNum >= 80 && points5GameNum >= 45) {
        imageBox.css('border', '2px solid    rgb(0,0,255)');
        imageBox.css('border-bottom', '0');
        infoBox.css('border', '2px solid    rgb(0,0,255)');
        infoBox.css('border-top', '0');

        // If price <= best market price AND price <= average 3 days or average 1 week AND percent 5 games >= 40 AND points 5 games >= 35 = green.
      }
      else if ((ethValNum <= marketValNum) && ((ethValNum <= threedaysValNum) || (ethValNum <= oneweekValNum)) && (percent5GameNum >= 40) && points5GameNum >= 35) {
        imageBox.css('border', '2px solid rgb(0,255,0)');
        imageBox.css('border-bottom', '0');
        infoBox.css('border', '2px solid rgb(0,255,0)');
        infoBox.css('border-top', '0');

        // If price <= best market price AND percent 5 games > 60% = yellow.
      }
      else if ((ethValNum <= marketValNum) && percent5GameNum >= 60) {
        imageBox.css('border', '2px solid rgb(204,204,0)');
        imageBox.css('border-bottom', '0');
        infoBox.css('border', '2px solid rgb(204,204,0)');
        infoBox.css('border-top', '0');
        //box.remove();

        // If price <= best market price AND percent 5 games < 60% = orange.
      }
      else if ((ethValNum <= marketValNum) && percent5GameNum < 60) {
        imageBox.css('border', '2px solid orange');
        imageBox.css('border-bottom', '0');
        infoBox.css('border', '2px solid orange');
        infoBox.css('border-top', '0');
        //box.remove();
      }

      // If price > best market price OR percent 15 games = 0 (DNP) OR price > three days average (if exists) = RED.
      if ((ethValNum > marketValNum) || (percent15GameNum <= 0) || ((ethValNum > threedaysValNum) && threedaysValNum > 0)) {
        imageBox.css('border', '2px solid red');
        imageBox.css('border-bottom', '0');
        infoBox.css('border', '2px solid red');
        infoBox.css('border-top', '0');
        //box.remove();
      }

      // If no market price, can't calculate = grey.
      if (marketValNum == 10000000) {
        imageBox.css('border', '2px solid grey');
        imageBox.css('border-bottom', '0');
        infoBox.css('border', '2px solid grey');
        infoBox.css('border-top', '0');
        //box.remove();
      }
    });
  }

  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 GM_addStyle(css) {
    const style = document.getElementById("GM_addStyleBy8626") || (function () {
      const style = document.createElement('style');
      style.type = 'text/css';
      style.id = "GM_addStyleBy8626";
      document.head.appendChild(style);
      return style;
    })();
    const sheet = style.sheet;
    sheet.insertRule(css, (sheet.rules || sheet.cssRules || []).length);
  }

  GM_addStyle(".triangle-topleft{width:0;height:0;border-top:70px solid gold;border-right:70px solid transparent;position: absolute; z-index: 1;}");

})();