Untitled

mail@pastecode.io avatar
unknown
plain_text
4 months ago
8.1 kB
6
Indexable
(function () {
  try {
    /* main variables */
    var debug = 0;
    var variation_name = "";
    var $;

    /* all Pure helper functions */
    function waitForElement(selector, trigger, delayInterval, delayTimeout) {
      var interval = setInterval(function () {
        if (
          document &&
          document.querySelector(selector) &&
          document.querySelectorAll(selector).length > 0
        ) {
          clearInterval(interval);
          trigger();
        }
      }, delayInterval);
      setTimeout(function () {
        clearInterval(interval);
      }, delayTimeout);
    }

    const thresholdPrices = {
      "$": {
        "currency": "USD",
        "thresholdvalue": 75,
        "symbol": "$",
      },
      "₹": {
        "currency": "INR",
        "thresholdvalue": 6294.55,
        "symbol": "₹",
      },
      "£": {
        "thresholdvalue": 57.10,
        "symbol": "£",
      }
    };

    function valuechange() {
      let gettext = document.querySelector('.footer-row .slidecart-subtotal span').innerText.trim();
      let text = gettext.charAt(0);
      let matchedData = thresholdPrices[text];

      if (matchedData) {
        let currency = matchedData.currency;
        let thresvalue = matchedData.thresholdvalue;
        let symbol = matchedData.symbol;

        console.log(currency);
        console.log(thresvalue);
        console.log(symbol);
        updateProgressBar(currency, thresvalue, symbol);
      }
    }

    var marginRight;
    let progressbar = `<div class="eg-progressbar">
    <div class="eg-shippingmessage">
        <p class="eg-shipping">Free Shipping!</p>
        <p class="eg-message">Add $75 worth of products to unlock FREE SHIPPING!</p>
    </div>
    <div class="eg-progresscontainer">
        <div class="eg-bar">
            <div style="width: 0%;"></div>
        </div>
        <div class="eg-worthofproduct">
            <p></p>
        </div>
        <div class="eg-amount">
            <p>$0</p>
            <p>FREE <br> SHIPPING</p>
        </div>
    </div>
    </div>`;

    let egstring = `<div class="eg-minicartprice">
    <p class="eg-shippingmessage"><span>Shipping</span><span></span></p>
    <p class="eg-total"><span>Total</span><span></span></p>
    </div>`
    let egPriceNew;

    function getPriceFromString(priceString) {
      return parseFloat(priceString.replace('$', '').replace('USD', '').trim());
    }

    function animateProgressBar(targetWidth) {
      const barElement = document.querySelector('.eg-progressbar .eg-progresscontainer .eg-bar > div');
      let currentWidth = parseFloat(barElement.style.width) || 0;
      marginRight = (100 - targetWidth);
      const difference = targetWidth - currentWidth;
      const increment = difference / 100;
      const interval = setInterval(function () {
        currentWidth += increment;

        if ((increment > 0 && currentWidth >= targetWidth) || (increment < 0 && currentWidth <= targetWidth)) {
          currentWidth = targetWidth;
          clearInterval(interval);
        }
        barElement.style.width = currentWidth + '%';
      }, 10);
      document.querySelector('.AB031 .eg-progressbar .eg-progresscontainer .eg-worthofproduct p').style.marginRight = marginRight + '%';

    }

    function updateProgressBar(currency, thresvalue, symbol) {
      let barShip = document.querySelector('.AB031 .eg-progressbar .eg-progresscontainer .eg-amount p:last-of-type');
      let congratulationMsg = document.querySelector('.AB031 .eg-progressbar .eg-shippingmessage .eg-shipping');
      let shippingmsg = document.querySelector('.AB031 .eg-progressbar .eg-shippingmessage .eg-message');
      let remainingMsg = document.querySelector('.AB031 .eg-progressbar .eg-progresscontainer .eg-worthofproduct');
      let egPrice = document.querySelector('.eg-minicartprice .eg-total > span:last-of-type');
      let free = document.querySelector('.eg-minicartprice .eg-shippingmessage > span:last-of-type');
      const minicartPriceElement = document.querySelector('.footer .slidecart-subtotal span');
      let cartprice = document.querySelector('.AB031 .eg-progressbar .eg-progresscontainer .eg-worthofproduct p');
      if (minicartPriceElement) {
        const minicartPrice = egPriceNew;
        // let priceelement = minicartPriceElement.textContent.trim();
        if (minicartPrice >= thresvalue) {
          animateProgressBar(100);
          congratulationMsg.innerText = 'Congratulations!';
          shippingmsg.innerText = 'Your order will ship to you for free.';
          remainingMsg.style.display = 'none';
          egPrice.innerText = `${symbol}` + minicartPrice + `${currency}`;
          free.innerText = 'FREE';
          cartprice.style.display = "none";
          cartprice.style.fontSize = "12px !important";
          barShip.style.marginRight = '0px';

        } else if (minicartPrice < thresvalue) {
          const targetWidth = (minicartPrice / thresvalue) * 100;
          animateProgressBar(targetWidth);
          const amountNeeded = thresvalue - minicartPrice;
          congratulationMsg.innerText = 'Free Shipping!';
          shippingmsg.innerText = `Add ${symbol}${amountNeeded.toFixed(2)} worth of products to unlock FREE SHIPPING!`;
          remainingMsg.style.display = 'flex';
          cartprice.innerText = `${symbol}` + minicartPrice;
          egPrice.innerText = `${symbol}` + (minicartPrice + 5).toFixed(2) + ` ${currency}`;
          free.innerText = '$5 USD';
          cartprice.style.display = "block";

        }
        if (minicartPrice > thresvalue) {
          cartprice.style.fontSize = "10px";
          barShip.style.marginRight = '-15px';
          document.querySelector('.AB031 .eg-progressbar .eg-progresscontainer .eg-worthofproduct p').style.marginRight = (marginRight + 4) + '%';

        }
      }
    }

    function init() {
      if (!document.querySelector('.eg-progressbar')) {
        document.body.classList.add("AB031");
        document.querySelector('#slidecarthq .header').insertAdjacentHTML('afterend', progressbar);
        waitForElement('.footer > .footer-row:not([style])', function () {
          document.querySelector('.footer > .footer-row:not([style])').insertAdjacentHTML('afterend', egstring);
        }, 50, 2000);
      }
      valuechange();
      setTimeout(() => {
        var slideCartHQ = document.querySelector('#slidecarthq .slidecarthq');
        var progressBar = document.querySelector('.eg-progressbar');
        if (slideCartHQ.classList.contains('cartEmpty')) {
          progressBar.style.display = 'none';
        } else {
          progressBar.style.display = 'block';
        }
      }, 1000);
    }

    function update() {
      var cartCount = 0;
      const send = XMLHttpRequest.prototype.send
      XMLHttpRequest.prototype.send = function () {
        this.addEventListener('load', function () {
          if (this.responseURL.indexOf('/cart.js') != -1) {
            var data = JSON.parse(this.responseText);
            var cartNewCount = data.item_count;
            let price = document.querySelector('.footer-row .slidecart-subtotal span').innerText.trim();
            let egPriceNew = price.replace(/[^0-9.,]/g, '').replace(/,/g, '');
            console.log(egPriceNew);
            if (cartNewCount == 0 || cartNewCount == '0') {
              document.querySelector('.eg-progressbar') && document.querySelector('.eg-progressbar').remove();
              return;
            }
            if (cartCount != cartNewCount && cartNewCount != 0) {
              cartCount = data.item_count;
              init();
            }
          }
        })
        return send.apply(this, arguments)
      }
    }
    update();

    waitForElement('.footer .slidecart-subtotal span', init, 100, 15000);
  } catch (e) {
    if (debug) console.log(e, "error in Test" + variation_name);
  }
})();
Leave a Comment