Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
9.1 kB
2
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);
    }
    function waitforMarketo(trigger){
      var interval = setInterval(function() {
        if ( 
          window.MktoForms2
        ) {
          clearInterval(interval); 
          trigger(); 
        }
      }, 50); 
      setTimeout(function() {
        clearInterval(interval);
      }, 10000);
    }
    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 (el && found) callback.call(el, e);
        });
      }
      live(selector, event, callback, context);
    }


    live('.eg-submit', 'click', function () {
      submitForm();
    });

    const formHtmlDes = `
          <div class="eg-news-letter-des"><div class="eg-fields-wrapper"><div class="mktoFormRow"><div class="mktoFieldDescriptor mktoFormCol"><div class="mktoFieldWrap mktoRequiredField"><input name="egFirstName" placeholder="First name" maxlength="255" aria-labelledby="LblFirstName InstructFirstName" type="text" class="mktoField mktoTextField mktoHasWidth mktoRequired" aria-required="true" style=""><div class="eg-hidden eg-fn-err eg-field-err mktoError"><div class="mktoErrorArrowWrap"><div class="mktoErrorArrow"></div></div><div role="alert" tabindex="-1" class="mktoErrorMsg">This field is required</div></div></div></div></div><div class="mktoFormRow"><div class="mktoFieldDescriptor mktoFormCol"><div class="mktoFieldWrap mktoRequiredField"><input name="egEmail" placeholder="Email address" maxlength="255" aria-labelledby="LblEmail InstructEmail" type="email" class="mktoField mktoEmailField mktoHasWidth mktoRequired" aria-required="true" style=""><div class="eg-field-err eg-hidden eg-email-err mktoError"><div class="mktoErrorArrowWrap"><div class="mktoErrorArrow"></div></div><div role="alert" tabindex="-1" class="mktoErrorMsg">Must be valid email. <span class="mktoErrorDetail">example@yourdomain.com</span></div></div></div></div></div><div class="mktoButtonRow"><button type="submit" class="eg-submit mktoButton">Subscribe</button></div>
      <div class="eg-mgs eg-hidden text-base form-confirmation-message">
        <p>Thank you for your request</p>
      </div>
      </div></div>
    `;
    const init = () => {
      if (!document.querySelector(".eg-news-letter-des")) {
        document.querySelector(".site-header__content").insertAdjacentHTML("beforeend", formHtmlDes);
      }
    }

    const formHtmlMob = `
          <li class="eg-mob-frm">
    
<div class="eg-fields-wrapper"><h4 class="mb-3 text-sm font-medium lg:text-base">Sign up to our weekly insights newsletter</h4><div class=""><div class="mktoFormRow"><div class="mktoFieldDescriptor mktoFormCol"><div class="mktoFieldWrap mktoRequiredField"><input name="egFirstName" placeholder="First name" maxlength="255" aria-labelledby="LblFirstName InstructFirstName" type="text" class="mktoField mktoTextField mktoHasWidth mktoRequired mktoInvalid" aria-required="true" style="" aria-invalid="true" aria-describedby="ValidMsgFirstName"><div class="eg-hidden eg-fn-err eg-field-err mktoError">
                        <div class="mktoErrorArrowWrap">
                            <div class="mktoErrorArrow"></div>
                        </div>
                        <div role="alert" tabindex="-1" class="mktoErrorMsg">This field is required</div>
                    </div></div></div></div><div class="mktoFormRow"><div class="mktoFieldDescriptor mktoFormCol"><div class="mktoFieldWrap mktoRequiredField"><input name="egEmail" placeholder="Email address" maxlength="255" aria-labelledby="LblEmail InstructEmail" type="email" class="mktoField mktoEmailField mktoHasWidth mktoRequired mktoInvalid" aria-required="true" style="" aria-invalid="true" aria-describedby="ValidMsgEmail"><div class="eg-field-err eg-hidden eg-email-err mktoError">
                        <div class="mktoErrorArrowWrap">
                            <div class="mktoErrorArrow"></div>
                        </div>
                        <div role="alert" tabindex="-1" class="mktoErrorMsg">Must be valid email. <span class="mktoErrorDetail">example@yourdomain.com</span></div>
                    </div></div></div></div><div class="mktoButtonRow"><button type="submit" class="eg-submit mktoButton">Subscribe to our newsletter</button></div></div>

<div class="eg-mgs eg-hidden text-base form-confirmation-message">
            <p>Thank you for your request</p>
        </div>


</div></li>
    `;
    const initMOb = () => {
      if (!document.querySelector(".eg-mob-frm")) {
        document.querySelector(" .site-header__content>.site-header__navigation nav >ul").insertAdjacentHTML("beforeend", formHtmlMob);
      }
    }

    const detectFormSub = () => {
      const send = XMLHttpRequest.prototype.send
      XMLHttpRequest.prototype.send = function () {
        this.addEventListener('load', function () {
          if (this.responseURL.indexOf("/save2") !== -1) {
            console.log("form submitted",document.body.classList.add('hello'));
            document.querySelector(".eg-fields-wrapper") && document.querySelector(".eg-fields-wrapper").classList.add("eg-success");
          }
        })
        return send.apply(this, arguments)
      }

      waitforMarketo(function(){
        MktoForms2.whenReady(function (form) {
          form.onSuccess(function(values, followUpUrl) {
        console.log('hello', document.body.classList.add('ffsd'));
        document.querySelector(".eg-fields-wrapper") && document.querySelector(".eg-fields-wrapper").classList.add("eg-success");
          });
          form.onSubmit(function(){
            console.log('hello', document.body.classList.add('ffsd'));
            document.querySelector(".eg-fields-wrapper") && document.querySelector(".eg-fields-wrapper").classList.add("eg-success");
          });

        });
      });
    }

    const validateEmail = (email) => {
      return email.match(
        /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
      );
    };

    const submitForm = () => {
      const fNEl = document.querySelector('[name="egFirstName"]');
      const eMEl = document.querySelector('[name="egEmail"]');

      // ori
      const OFnel = document.querySelector("form.footer-subscribe #FirstName");
      const OEmel = document.querySelector("form.footer-subscribe #Email");

      if (fNEl.value.trim() == "") {
        fNEl.parentElement.querySelector(".eg-field-err").classList.remove("eg-hidden");
      } else if ((eMEl.value.trim() == "") || (!validateEmail(eMEl.value))) {
        eMEl.parentElement.querySelector(".eg-field-err").classList.remove("eg-hidden");
      } else {
        OFnel.value = fNEl.value;
        OEmel.value = eMEl.value;
        fNEl.parentElement.querySelector(".eg-field-err").classList.remove("eg-hidden");
        eMEl.parentElement.querySelector(".eg-field-err").classList.remove("eg-hidden");
        document.querySelector('form.footer-subscribe [type="submit"]').click();
      }
    }

    if (window.innerWidth > 1024) {
      waitForElement('.site-header__content', init, 50, 15000);
    } else {
      waitForElement('.site-header__content', initMOb, 50, 15000);
    }
    // form submit detect
    detectFormSub();
  } catch (e) {
    if (debug) console.log(e, "error in Test" + variation_name);
  }
})();