Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
9.3 kB
4
Indexable
Never
(function () {
  try {
    var debug = 0;
    var variation_name = "";
    var testData = {
      "cpnSubmsg": "",
      progreesteps: {
        "step1ElSel": 'html body .checkout .progressbar.step1',
        "step2ElSel": 'html body .checkout .progressbar.step2',
        "step3ElSel": 'html body .checkout .progressbar.step3'
      }
    }

    var live = function (selector, event, callback, context) {
      /****Helper Functions****/
      // helper for enabling IE 8 event bindings
      var addEvent = function (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
      var live = function (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 (el && found) callback.call(el, e);
        });
      }
      live(selector, event, callback, context);
    }

    var waitForElement = function (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);
    }

    var init = function () {
      // move coupon block just above "Sous-total"
      moveCoupon();
      // move progreesteps altogether
      moveProgressSteps();
      // change cta text
      changeText();
    }

    var testEvents = function () {
      if (!document.querySelector('eg-events-added')) {
        document.body.classList.add('eg-events-added');
        // evnts
        live('.eg-coupon-toggle-cta', 'click', function () {
          this.classList.toggle('eg-show');
        });

        live('.eg-cpn-submit', 'click', submitCpnDynamic);

        live('.eg-new-step', 'click', function () {
          var key = this.getAttribute('data-key');
          var sel = testData.progreesteps[key];
          if (sel) {
            var el = document.querySelector(sel);
            if (el) {
              el.click();
            }
          }

        });

        // to make active progressbar
        var targetNode = document.querySelector('html body .checkout');
        var config = {
          attributes: true,
          childList: true,
          subtree: true,
          attributeFilter: ['style']
        };
        // Callback function to execute when mutations are observed
        var callback = function (mutationsList, observer) {
          mutationsList.forEach(function (mutation) {
            if (mutation.type === 'attributes') {
              var actEl1 = document.querySelector(testData.progreesteps.step1ElSel + ' + .step-content[style="display: block;"]') || document.querySelector(testData.progreesteps.step1ElSel + ' + .step-content:not([style="display: none;"])');
              var actEl2 = document.querySelector(testData.progreesteps.step2ElSel + ' + .step-content[style="display: block;"]') || document.querySelector(testData.progreesteps.step2ElSel + ' + .step-content:not([style="display: none;"])');
              var actEl3 = document.querySelector(testData.progreesteps.step3ElSel + ' + .step-content[style="display: block;"]') || document.querySelector(testData.progreesteps.step3ElSel + ' + .step-content:not([style="display: none;"])');

              var newel1 = document.querySelector('.eg-new-step.eg-step-1');
              var newel2 = document.querySelector('.eg-new-step.eg-step-2');
              var newel3 = document.querySelector('.eg-new-step.eg-step-3');
              if (actEl1 && newel1 && newel2 && newel3) {
                newel1.classList.add('eg-active');
                // rm
                newel2.classList.remove('eg-active');
                newel3.classList.remove('eg-active');
              }
              if (actEl2 && newel1 && newel2 && newel3) {
                newel1.classList.add('eg-active');
                newel2.classList.add('eg-active');
                // rm
                newel3.classList.remove('eg-active');
              }
              if (actEl3 && newel1 && newel2 && newel3) {
                newel1.classList.add('eg-active');
                newel2.classList.add('eg-active');
                newel3.classList.add('eg-active');
              }
            }
          });
        };
        var observer = new MutationObserver(callback);
        observer.observe(targetNode, config);

        // monitor requests
        monitorRequest();
      }
    }

    var monitorRequest = function () {
      var send = XMLHttpRequest.prototype.send
      XMLHttpRequest.prototype.send = function () {
        this.addEventListener('load', function () {
          if (this.responseURL.includes('?wc-ajax=apply_coupon')
            || this.responseURL.includes('?wc-ajax=update_order_review')
            || this.responseURL.includes('sync_user_data.php')
          ) {
            // coupon response check
            if (this.responseURL.includes('?wc-ajax=apply_coupon')) {
              testData.cpnSubmsg = this.response;
            }
            waitForElement('footer', init, 50, 15000);
          }
        })
        return send.apply(this, arguments)
      }
    }

    var moveCoupon = function () {
      var msg = testData.cpnSubmsg;
      waitForElement('html body .cart_total_checkout', function () {
        if (!document.querySelector('html body .cart_total_checkout .eg-coupon-block')) {
          var putTarget = document.querySelector('html body .cart_total_checkout');
          var couponNewHTML = '' +
            '  <div class="eg-coupon-block">' +
            '      <span class="eg-coupon-toggle-cta">Code promo</span>' +
            '      <div class="eg-coupon-box">' +
            '          <span class="eg-coupon-err">' + msg + '</span>' +
            '          <div class="eg-coupon-group coupon">' +
            '              <input type="text" class="eg-newcpn-input input-text" id="coupon_code" value="" placeholder="Code promo">' +
            '              <button type="button" class="eg-cpn-submit button action-button" value="Appliquer le code promo">Appliquer</button>' +
            '          </div>' +
            '      </div>' +
            '  </div>';;
          putTarget.insertAdjacentHTML('afterbegin', couponNewHTML);
        }
      }, 50, 15000);
    }

    var submitCpnDynamic = function () {
      var cpnVal = document.querySelector('.eg-newcpn-input').value;
      var oriCpnInput = document.querySelector('html body .checkout_coupon #coupon_code');
      if (oriCpnInput) {
        oriCpnInput.value = cpnVal;
        oriCpnInput.dispatchEvent(new Event('input'));
        document.querySelector('html body .checkout_coupon .action-button').click();
      }
    }

    var moveProgressSteps = function () {
      waitForElement('html body .checkout #customer_details', function () {
        var newProgressHTML = '' +
          '  <div class="eg-new-progress-step-wrapper">' +
          '      <span class="eg-new-step eg-step-1" data-key="step1ElSel">Coordonnées</span>' +
          '      <span class="eg-new-step eg-step-2" data-key="step2ElSel">Livraison</span>' +
          '      <span class="eg-new-step eg-step-3" data-key="step3ElSel">Paiement</span>' +
          '  </div>';;
        if (!document.querySelector('.eg-new-progress-step-wrapper')) {
          document.querySelector('html body .checkout #customer_details').insertAdjacentHTML('afterbegin', newProgressHTML);
        }
      }, 50, 15000);
    }

    var changeText = function () {
      // big yello cta text update
      waitForElement('html body #place_order', function () {
        document.querySelector('html body #place_order').innerText = "Commander";
      }, 50, 15000);
      // removing text from check box text
      waitForElement('html body .checkbox.terms-and-conditions', function () {
        document.querySelector('html body .checkbox.terms-and-conditions').innerHTML = document.querySelector('html body .checkbox.terms-and-conditions').innerHTML.replace("sans réserve", '');
      }, 50, 15000);
    }
    waitForElement('footer', init, 50, 15000);
    waitForElement('footer', testEvents, 50, 15000);
  } catch (e) {
    if (debug) console.log(e, "error in Test" + variation_name);
  }
})();


Leave a Comment