Untitled

 avatar
unknown
plain_text
25 days ago
2.9 kB
1
Indexable
(function () {
  try {
    /* Main variables */
    var debug = 0;
    var variation_name = "";
    
    /* Helper function: wait for the element to exist */
    function waitForElement(selector, trigger, delayInterval, delayTimeout) {
      var interval = setInterval(function () {
        if (document.querySelector(selector)) {
          clearInterval(interval);
          trigger();
        }
      }, delayInterval);
      setTimeout(function () {
        clearInterval(interval);
      }, delayTimeout);
    }

    function init() {
      document.body.classList.add('CTABehavior');
      var addToCartButton = document.querySelector('.upload_design .upload_now_btn');
    
      if (addToCartButton) {
        var dynamicUrl = addToCartButton.getAttribute('href');
        if (dynamicUrl) {
          fetch(dynamicUrl)
            .then(response => response.text())
            .then(html => {
              var tempDiv = document.createElement('div');
              tempDiv.innerHTML = html;
    
              var newButton = tempDiv.querySelector('#buynowUpoadLaterDiv > div a#buynowUpoadLater');
              if (newButton) {
                var newButtonHref = newButton.getAttribute('href');
                addToCartButton.setAttribute('href', newButtonHref);
                console.log("Href updated successfully:", newButtonHref);
              } else {
                console.error("New button not found in the fetched HTML.");
              }
            })
            .catch(err => console.error("Error fetching URL:", err));
        } else {
          console.error("The .upload_now_btn button does not have an href attribute.");
        }
      } else {
        console.error(".upload_design .upload_now_btn button not found on the page.");
      }
    }       

    function changeDesignCart() {
      document.body.classList.add('ABCARTPAGE');
      const specifications = document.querySelectorAll(".specifications");
      specifications.forEach(function (spec) {
        const btnLight = spec.querySelector(".btn-light");
        if (btnLight) {
          const thumbnail = spec.querySelector(".specifications > div:first-child .product .thumbnail");
          if (thumbnail) {
            const uploadContainer = document.createElement("div");
            uploadContainer.className = "eg-upload-container";
            thumbnail.insertAdjacentElement("afterend", uploadContainer);
            uploadContainer.appendChild(btnLight);
          }
        }
      });
    }

    /* Initialize the variation */
    if(window.innerWidth > 1200) {
      waitForElement('.upload_design .upload_now_btn', init, 50, 15000);
      waitForElement('.specifications', changeDesignCart, 50, 15000);
    }
  } catch (e) {
    console.error(e, "Error in Test: " + variation_name);
  }
})();
Editor is loading...
Leave a Comment