Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.2 kB
3
Indexable
Never
(function () {
  try {
    /* main variables */
    var debug = 0;
    var variation_name = "";
    var $;
    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);
    }
    let onload = true;
    const changeCost = () => {
      if (window.location.href.indexOf('https://www.yankeecandle.com/checkout?stage') !== -1) {
        waitForElement('html body .shipping-method-list >div:has(input:checked),.grand-total .grand-total-sum', function () {
          let grandTotal = document.querySelector(".grand-total .grand-total-sum");
          if (grandTotal) {
            let grandTotalValue = +grandTotal.innerText.replace("$", "");
            let eg3dayEl = document.querySelector('.shipping-method-list input[value="FXG"]');
            if (eg3dayEl) {
              if (onload) {
                eg3dayEl.click();
                onload = false;
              }
              if (grandTotalValue < 50) {
                eg3dayEl.parentElement.nextElementSibling.innerText = "$5.99";
              } else if (grandTotalValue >= 50) {
                eg3dayEl.parentElement.nextElementSibling.innerText = "FREE";
              }
            }
          }
        }, 50, 15000);
      }
    }

    const addParam = () => {
      waitForElement('a[href="https://www.yankeecandle.com/cart"]', function () {
        document.querySelectorAll('a[href="https://www.yankeecandle.com/cart"]').forEach(mCrt => {
          if (mCrt.href.indexOf("src=1D0CA6R") == -1) {
            mCrt.href = mCrt.href + "?src=1D0CA6R";
          }
        });
      }, 1000, 15000);
    };
    function listener() {
      /* These are the modifications: */
      window.addEventListener("locationchange", function () {
        waitForElement('.shipping-method-list input[value="FXG"]', changeCost, 50, 15000);
      });
      history.pushState = ((f) =>
        function pushState() {
          var ret = f.apply(this, arguments);
          window.dispatchEvent(new Event("pushstate"));
          window.dispatchEvent(new Event("locationchange"));
          return ret;
        })(history.pushState);
      history.replaceState = ((f) =>
        function replaceState() {
          var ret = f.apply(this, arguments);
          window.dispatchEvent(new Event("replacestate"));
          window.dispatchEvent(new Event("locationchange"));
          return ret;
        })(history.replaceState);
      window.addEventListener("popstate", () => {
        window.dispatchEvent(new Event("locationchange"));
      });
    }

    const send = XMLHttpRequest.prototype.send
    XMLHttpRequest.prototype.send = function () {
      this.addEventListener('load', function () {

        if (this.responseURL.indexOf("/CheckoutShippingServices-SelectShippingMethod") != -1 || this.responseURL.indexOf("/Cart-AddCoupon") != -1 || this.responseURL.indexOf("/CheckoutShippingServices-UpdateShippingMethodsList") != -1 || this.responseURL.indexOf("/Cart-RemoveCouponLineItem") != -1) {
          waitForElement('.shipping-method-list input[value="FXG"]', changeCost, 50, 15000);
        }

        if ((this.responseURL.indexOf("Cart-AddProduct") != -1) || (this.responseURL.indexOf("Cart-MiniCartShow")) || (this.responseURL.indexOf("Cart-RemoveProductLineItem")) || (this.responseURL.indexOf("Cart-UpdateQuantity"))) {
          waitForElement('.minicart .sub-total', initHref, 50, 15000);
        }
      })
      return send.apply(this, arguments)
    }

    const checkForParam = () => {
      fetch("https://www.yankeecandle.com/cart").then(res => res.text()).then(data => {
        const el = document.createElement("div");
        el.innerHTML = data;
        const grandTotal = el.querySelector(".grand-total .grand-total-sum");
        if (grandTotal) {
          let grandTotalValue = +grandTotal.innerText.replace("$", "");
          if (grandTotalValue >= 50) {
            console.log("hello param can be added");
            addParam();
          }
        }
      })
    }

    const initHref = () => {
      var subtotal = document.querySelector('.minicart .sub-total');
      if (subtotal) {
        let price = +document.querySelector('.minicart .sub-total').innerText.replace("Subtotal: ", "").replace("$", "");
        if (price > 50) {
          if (document.querySelector('.minicart a.go-to-cart')) {
            document.querySelector('.minicart a.go-to-cart').href = 'https://www.yankeecandle.com/cart?src=1D0CA6R';
            document.querySelector('.minicart a.minicart-link').href = 'https://www.yankeecandle.com/cart?src=1D0CA6R';
          }
        }
      }
    }

    /* Initialize variation */
    waitForElement('.shipping-method-list input[value="FXG"]', changeCost, 50, 15000);
    listener();
    waitForElement("html body", checkForParam, 50, 15000);
  } catch (e) {
    if (debug) console.log(e, "error in Test" + variation_name);
  }
})();