Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.1 kB
1
Indexable
Never
(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', '6609001578670',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);
  }

})();