Untitled

 avatar
unknown
plain_text
2 years ago
13 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);
        }

        function waitForEmbla(trigger) {
            var interval = setInterval(function() {
                if (window.EmblaCarousel != undefined) {
                    clearInterval(interval);
                    trigger();
                } else {
                    addEmbla();
                }
            }, 50);
            setTimeout(function() {
                clearInterval(interval);
            }, 15000)
        }

        function addEmbla() {
            var emblaScript = document.createElement('script');
            // check for updated jquery cdn
            emblaScript.src = 'https://unpkg.com/embla-carousel/embla-carousel.umd.js';
            document.getElementsByTagName('head')[0].appendChild(emblaScript);
        }


        function listener() {
            /* These are the modifications: */
            window.addEventListener("locationchange", function() {
                if ((window.location.href.indexOf("https://www.vacation.inc/products/scent") != -1) && !document.querySelector(".eg-sld")) {
                    waitForElement(".shopping-block-pusher >div:nth-child(2) > div:nth-child(1) ul", function() {
                        if (!document.querySelector(".eg-right-txt")) {
                            document.querySelector(".shopping-block-pusher >div:nth-child(2) > div:nth-child(1) ul").insertAdjacentHTML("beforeend", `<li class="eg-right-txt"><em>FREE 1oz Classic Lotion SPF 30 Sample With Purchase!<em></li>`);
                            live(".shopping-block-pusher >div:nth-child(2) > div:nth-child(1) > div > button[title='toggle']", "click", function() {
                                if (!document.querySelector(".eg-right-txt")) {
                                    document.querySelector(".shopping-block-pusher >div:nth-child(2) > div:nth-child(1) ul").insertAdjacentHTML("beforeend", `<li class="eg-right-txt"><em>FREE 1oz Classic Lotion SPF 30 Sample With Purchase!<em></li>`)
                                }
                            });
                        }

                    }, 50, 15000);

                    /* Initialize variation */
                    waitForElement('.embla__container', init, 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"));
            });
        }



        function live(selector, event, callback, context) {
            /****Helper Functions****/
            // helper for enabling IE 8 event bindings
            function addEvent(el, type, handler) {
                if (el.attachEvent) el.attachEvent("on" + type, handler);
                else el.addEventListener(type, handler);
            }
            // matches polyfill
            this.Element &&
                (function(ElementPrototype) {
                    ElementPrototype.matches =
                        ElementPrototype.matches ||
                        ElementPrototype.matchesSelector ||
                        ElementPrototype.webkitMatchesSelector ||
                        ElementPrototype.msMatchesSelector ||
                        function(selector) {
                            var node = this,
                                nodes = (node.parentNode || node.document).querySelectorAll(selector),
                                i = -1;
                            while (nodes[++i] && nodes[i] != node);
                            return !!nodes[i];
                        };
                })(Element.prototype);
            // live binding helper using matchesSelector
            function live(selector, event, callback, context) {
                addEvent(context || document, event, function(e) {
                    var found,
                        el = e.target || e.srcElement;
                    while (el && el.matches && el !== context && !(found = el.matches(selector))) el = el.parentElement;
                    if (found) callback.call(el, e);
                });
            }
            live(selector, event, callback, context);
        }

        var embla;


        live(['.product__hero-thumb', '.product__hero-dot'], 'click', function() {

            if (this.parentElement.children[0] == this) {
                embla.scrollTo(0, false);
            } else if (this.parentElement.children[1] == this) {
                embla.scrollTo(1, false);
            } else if (this.parentElement.children[2] == this) {
                embla.scrollTo(2, false);
            } else if (this.parentElement.children[3] == this) {
                embla.scrollTo(3, false);
            } else if (this.parentElement.children[4] == this) {
                embla.scrollTo(4, false);
            } else if (this.parentElement.children[5] == this) {
                embla.scrollTo(5, false);
            } else if (this.parentElement.children[6] == this) {
                embla.scrollTo(6, false);
            } else if (this.parentElement.children[7] == this) {
                embla.scrollTo(7, false);
            }

            this.parentElement.querySelectorAll(".active").forEach(act => {
                act.classList.remove("active")
            })
            this.classList.add("active");
        });



        /* Variation Init */
        function init() {
            waitForEmbla(function() {
                if (!document.querySelector(".eg-sld")) {
                    var emblaNode = document.querySelector('.embla')
                    var options = { dots: true }

                    embla = EmblaCarousel(emblaNode, options);
                    const egSlide = document.createElement("div");
                    egSlide.classList.add("embla__slide", "eg-sld");
                    egSlide.style.cssText = `flex:0 0 100%;height:100%;width:100%;position:relative`
                    'flex:0 0 100%;height:100%;width:100%;position:relative'
                    egSlide.innerHTML = `<div class="image pa x y top left">
                    <picture>
                        <source srcset="https://expogrowth.in/wp-content/uploads/2023/02/Vacation_EDT-GWP_ClassicSample-Square.jpg" media="(min-width: 1000px)">
                        <source srcset="https://expogrowth.in/wp-content/uploads/2023/02/Vacation_EDT-GWP_ClassicSample-Square.jpg" media="(min-width: 600px)"><img src="https://expogrowth.in/wp-content/uploads/2023/02/Vacation_EDT-GWP_ClassicSample-Square.jpg"></picture>
                </div>`;
                    const egSlideThumb = `<div class="product__hero-thumb mb1 pr"><button aria-label="Select image slide" class="image pa x y top left eg-thumb">
                    <picture><img alt="video thumbnail" src="https://expogrowth.in/wp-content/uploads/2023/02/Vacation_EDT-GWP_ClassicSample-Square.jpg?width=300&amp;fit_mode=preserve&amp;time=0"></picture>
                </button></div>`;


                    document.querySelector(".product__hero-carousel-thumbs > div:nth-child(3)").insertAdjacentHTML("afterend", egSlideThumb);

                    const egSlideDot = `<button aria-label="Select image slide" class="product__hero-dot eg-dot"></button>`;
                    document.querySelector(".product__hero-dots > button:nth-child(3)").insertAdjacentHTML("afterend", egSlideDot);

                    document.querySelector(".embla__container > div:nth-child(3)").insertAdjacentElement("afterend", egSlide);
                    embla.reInit();

                }
            });
            // changing first carousel img src
            waitForElement("html body .embla__container > .embla__slide:nth-child(1) picture source", function() {
                document.querySelectorAll("html body .embla__container > .embla__slide:nth-child(1) picture source").forEach(source => {
                    source.srcset = "https://cdn.shopify.com/s/files/1/0553/2984/9518/files/EDT_GWP_Carousel_Update_slide_1-square.jpg?v=1676962071";
                    if (source.nextElementSibling && source.nextElementSibling.tagName === "IMG") {
                        source.nextElementSibling.src = "https://cdn.shopify.com/s/files/1/0553/2984/9518/files/EDT_GWP_Carousel_Update_slide_1-square.jpg?v=1676962071";
                    }
                })
            }, 50, 15000);

        }

        if (window.location.href.indexOf("https://www.vacation.inc/products/scent") != -1) {
            waitForElement(".shopping-block-pusher >div:nth-child(2) > div:nth-child(1) ul", function() {
                if (!document.querySelector(".eg-right-txt")) {
                    document.querySelector(".shopping-block-pusher >div:nth-child(2) > div:nth-child(1) ul").insertAdjacentHTML("beforeend", `<li class="eg-right-txt"><em>FREE 1oz Classic Lotion SPF 30 Sample With Purchase!<em></li>`);
                    live(".shopping-block-pusher >div:nth-child(2) > div:nth-child(1) > div > button[title='toggle']", "click", function() {
                        if (!document.querySelector(".eg-right-txt")) {
                            document.querySelector(".shopping-block-pusher >div:nth-child(2) > div:nth-child(1) ul").insertAdjacentHTML("beforeend", `<li class="eg-right-txt"><em>FREE 1oz Classic Lotion SPF 30 Sample With Purchase!<em></li>`)
                        }
                    });
                }

            }, 50, 15000);

            /* Initialize variation */
            waitForElement('.embla', init, 50, 15000);
        }
        listener();
    } catch (e) {
        if (debug) console.log(e, "error in Test" + variation_name);
    }
})();


(function () {
  var debug = false;
  try {

    function waitForElement(selector, callback) {
      var maxCheck = 200;
      var interval = window.setInterval(function() {
          if(document.querySelectorAll(selector).length > 0) {
              callback();
              window.clearInterval(interval);
          } else if(--maxCheck < 0) {
              window.clearInterval(interval);
          }
      }, 50);
    }

    function createCookie(name, value, days) {
      // CREATE COOKIE
      var expires = "";
      if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toUTCString();
      }
      document.cookie = name + "=" + (value || "") + expires + "; path=/";
    }

    function mutationForCart() {
        var targetNode = document.querySelector('.header__nav .grid-container');
        var config = { attributes: true, characterData: true, childList: true, subtree: true };
        function callback (mutationList, observer) {
            for(var i = 0; i < mutationList.length; i++) {
                waitForElement('.cart__item .cart__item__content .mb1 > .cart__content:last-child', function() {
                    var vcCartItems = document.querySelectorAll('.cart__item .cart__item__content .mb1 > .cart__content:last-child');
                    console.log('product found >>', vcCartItems.length)
                    for(var i = 0; i < vcCartItems.length; i++) {
                        console.log(vcCartItems[i].innerHTML)
                        if(vcCartItems[i].innerHTML == 'Eau de Toilette') {
                            createCookie('gwp_ab', '7828599144681',30);               
                            break;
                        }
                    }
                });
            }
        };
        var observer = new MutationObserver(callback);
        observer.observe(targetNode, config);
    }

    waitForElement('.header__nav .grid-container', mutationForCart)

  } catch (e) {
    if (debug) console.log(e, "error in " + variation_name);
  }

})();
Editor is loading...