Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
7.2 kB
1
Indexable
Never
(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);
    }
    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);
    };

    const addMessage = () => {
      setTimeout(() => {
        let message = `Congratulations, you've unlocked discounted three-day shipping!`;
        let grandTotal = document.querySelector(".grand-total .grand-total-sum");
        if (grandTotal) {
          let grandTotalValue = +grandTotal.innerText.replace("$", "");
          let messageEl = document.querySelector('.cart-info-messaging > .bonus-product-msg');
          if (messageEl) {
            if (grandTotalValue >= 50) {
              message = "Congratulations, you've unlocked free three-day shipping!";
            }
            let egMessageHTML = `
            <div class="bonus-product-msg cart-info-msg deal-alert eg-message">
              <span tabindex="-1">
                <svg role="img" class="icon svg-icon circle-success" width="16" height="16" aria-hidden="false" tabindex="-1">
                  <title>Success</title>
                  <desc>Success</desc>
                  <use role="img" xlink:href="#circle-success-fill-green" alt="Success"></use>
                </svg>
              </span>
              <p class="msg-wrap m-0" role="alert">
                <span>${message}</span>
              </p>
            </div>`;
            if (!document.querySelector(".eg-message")) {
              messageEl.insertAdjacentHTML("beforebegin", egMessageHTML);
            } else {
              document.querySelector(".eg-message").outerHTML = egMessageHTML;
            }
          }
        }
      }, 50);
    }

    function listener() {
      /* These are the modifications: */
      window.addEventListener("locationchange", function () {
        waitForElement('.shipping-method-list input[value="FXG"]', changeCost, 50, 15000);
        waitForElement(".grand-total .grand-total-sum,.cart-info-messaging > .bonus-product-msg", addMessage, 500, 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);
          waitForElement(".grand-total .grand-total-sum,.cart-info-messaging > .bonus-product-msg", addMessage, 500, 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);
    waitForElement(".grand-total .grand-total-sum,.cart-info-messaging > .bonus-product-msg", addMessage, 500, 15000);
    listener();
    waitForElement("html body", checkForParam, 50, 15000);
  } catch (e) {
    if (debug) console.log(e, "error in Test" + variation_name);
  }
})();


html body .shipping-method-list>div:nth-of-type(-n+2) {
    display: none !important;
}