Lemnisk_Validation_Checker / Engage360_RegexChecker

// ==UserScript==
// @name Engage360_RegexChecker
// @description Regex Checker for Form Validations
// @updateURL https://openuserjs.org/meta/Lemnisk_Validation_Checker/Engage360_RegexChecker.meta.js
// @version 1.2
// @author Lemnisk
// @include https://engage.lemnisk.co/*
// @license MIT
// @icon https://www.lemnisk.co/images/Lemnisk_logo.png
// @run-at document-end
// @grant none
// ==/UserScript==

var title = null;
var lp = null;
var message = null;
var ctaText = null;
var mutationObserver = null;

window.addEventListener("hashchange", assignValues, true);

function assignValues() {
  if (
    /(engage.lemnisk.co)*(bannerpersonalization)/.test(window.location.href)
  ) {
    disconnectObserver();
    setMutationForBP();
    setObserver();
  }
  else if (/(engage.lemnisk.co)*(notification)/.test(window.location.href)) {
    disconnectObserver();
    setMutationForOnsiteNot();
    setObserver();
  }
  else if (/(engage.lemnisk.co)*(webpush)/.test(window.location.href)) {
    disconnectObserver();
    setMutationForWebpush();
    setObserver();
  }
  else {
    disconnectObserver();
  }
}

function disconnectObserver() {
  if (mutationObserver) {
    mutationObserver.disconnect();
  }
}

/**
 * Mutation Observer for Banner Personalization
 */
function setMutationForBP() {
  mutationObserver = new MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
      if (
        mutation.target.id == "title" &&
        mutation.target.getAttribute("listener") !== "true"
      ) {
        title = mutation.target;
        title.addEventListener("blur", bpTitleValidation);
        title.setAttribute("listener", "true");
      }

      if (
        mutation.target.id == "message" &&
        mutation.target.getAttribute("listener") !== "true"
      ) {
        message = mutation.target;
        message.addEventListener("blur", bpMessageValidation);
        message.setAttribute("listener", "true");
      }

      if (
        mutation.target.id == "ctaText" &&
        mutation.target.getAttribute("listener") !== "true"
      ) {
        ctaText = mutation.target;
        ctaText.addEventListener("blur", bpCtaButtonValidation);
        ctaText.setAttribute("listener", "true");
      }

      if (
        mutation.target.id == "lp" &&
        mutation.target.getAttribute("listener") !== "true"
      ) {
        lp = mutation.target;
        lp.addEventListener("blur", bpClientLPValidation);
        lp.setAttribute("listener", "true");
      }
    });
  });
}

/**
 * Mutation Observer for Onsite Notification
 */

function setMutationForOnsiteNot() {
  mutationObserver = new MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
      if (
        mutation.target.id == "st_title" &&
        mutation.target.getAttribute("listener") !== "true"
      ) {
        title = mutation.target;
        title.addEventListener("blur", onsiteTitleValidation);
        title.setAttribute("listener", "true");
      }

      if (
        mutation.target.id == "st_message" &&
        mutation.target.getAttribute("listener") !== "true"
      ) {
        message = mutation.target;
        message.addEventListener("blur", onSiteMessageValidation);
        message.setAttribute("listener", "true");
      }

      if (
        document.querySelector('[id$="_ifr"]') &&
        document.querySelector('[id$="_ifr"]').getAttribute("listener") !==
        "true"
      ) {
        var iframe = document.querySelector('[id$="_ifr"]');
        var innerdoc = iframe.contentDocument || iframe.contentWindow.document;
        if (innerdoc && innerdoc.getElementById("tinymce")) {
          ctaText = innerdoc.getElementById("tinymce");
          ctaText.addEventListener("blur", onsiteCtaButtonValidation);
          iframe.setAttribute("listener", "true");
        }
      }

      if (
        mutation.target.id == "lp" &&
        mutation.target.getAttribute("listener") !== "true"
      ) {
        lp = mutation.target;
        lp.addEventListener("blur", wpClientLPValidation);
        lp.setAttribute("listener", "true");
      }

      if (
        mutation.target.id == "st_lp" &&
        mutation.target.getAttribute("listener") !== "true"
      ) {
        lp = mutation.target;
        lp.addEventListener("blur", onsiteClientLPValidation);
        lp.setAttribute("listener", "true");
      }
    });
  });
}

/**
 * Mutation Observer for Web Push
 */

function setMutationForWebpush() {
  mutationObserver = new MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
      if (
        mutation.target.id == "title" &&
        mutation.target.getAttribute("listener") !== "true"
      ) {
        title = mutation.target;
        title.addEventListener("blur", wpTitleValidation);
        title.setAttribute("listener", "true");
      }

      if (
        mutation.target.id == "body" &&
        mutation.target.getAttribute("listener") !== "true"
      ) {
        message = mutation.target;
        message.addEventListener("blur", wpMessageValidation);
        message.setAttribute("listener", "true");
      }

      if (
        mutation.target.id == "ctaText" &&
        mutation.target.getAttribute("listener") !== "true"
      ) {
        ctaText = mutation.target;
        ctaText.addEventListener("blur", bpCtaButtonValidation);
        ctaText.setAttribute("listener", "true");
      }

      if (
        mutation.target.id == "lp" &&
        mutation.target.getAttribute("listener") !== "true"
      ) {
        lp = mutation.target;
        lp.addEventListener("blur", wpClientLPValidation);
        lp.setAttribute("listener", "true");
      }
    });
  });
}

// Starts listening for changes in the root HTML element of the page.
function setObserver() {
  mutationObserver.observe(document.documentElement, {
    attributes: true,
    characterData: true,
    childList: true,
    subtree: true,
    attributeOldValue: true,
    characterDataOldValue: true
  });
}

/**
 * Webpush Title Validation
 **/
function wpTitleValidation() {
  if (!/^[a-zA-Z0-9\., %!\?]*$/.test(title.value)) {
    if (document.activeElement.id !== "title") {
      window.alert("Wrong Input key!");
    }
  }
}

/**
 * Web Push Message Validation
 */

function wpMessageValidation() {
  if (!/^[a-zA-Z0-9\., %!\?]*$/.test(message.value)) {
    if (document.activeElement.id !== "body") {
      window.alert("Wrong Input key!");
    }
  }
}

/**
 * Web push Client Landing Page Validation
 */
function wpClientLPValidation() {
  if (
    !/^$|(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/.test(
      lp.value
    )
  ) {
    if (document.activeElement.id !== "lp") {
      window.alert("Wrong Input key!");
    }
  }
}

/**
 * Onsite Notification Message Validatiion
 */
function onSiteMessageValidation() {
  if (!/^[a-zA-Z0-9\., %!\?&'_"#*]*$/.test(message.value)) {
    if (document.activeElement.id !== "st_message") {
      window.alert("Wrong Input key!");
    }
  }
}

/**
 * Onsite Notification Title Validation
 */

function onsiteTitleValidation() {
  if (!/^[a-zA-Z0-9\., %!\?&'_"#*]*$/.test(title.value)) {
    if (document.activeElement.id !== "st_title") {
      window.alert("Wrong Input key!");
    }
  }
}

/**
 * Onsite CTA Button Validation
 */
function onsiteCtaButtonValidation() {
  if (!/^[a-zA-Z0-9\., %!\?&'_"#*]*$/.test(ctaText.innerText)) {
    if (document.activeElement.id !== "tinymce") {
      window.alert("Wrong Input key!");
    }
  }
}
/**
 * Onite Client Landing Page Validation
 */
function onsiteClientLPValidation() {
  if (
    !/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/.test(
      lp.value
    )
  ) {
    if (
      document.activeElement.id !== "st_lp"
    ) {
      window.alert("Wrong Input key!");
    }
  }
}

/**
 * Banner Personalization Title Validation
 */
function bpTitleValidation() {
  if (!/^[a-zA-Z0-9\., %!\?&'_"#*]*$/.test(title.value)) {
    if (document.activeElement.id !== "title") {
      window.alert("Wrong Input key!");
    }
  }
}

/**
 * Banner Personalization Message Validation
 */
function bpMessageValidation() {
  if (!/^[a-zA-Z0-9\., %!\?&'_"#*]*$/.test(message.value)) {
    if (document.activeElement.id !== "message") {
      window.alert("Wrong Input key!");
    }
  }
}

/**
 * Banner Personalization CTA Button Validation
 */
function bpCtaButtonValidation() {
  if (!/^[a-zA-Z0-9\., %!&'_"#*]*$/.test(ctaText.value)) {
    if (document.activeElement.id !== "ctaText") {
      window.alert("Wrong Input key!");
    }
  }
}

/**
 * Banner Personalization Client Landing Page Validation
 */
function bpClientLPValidation() {
  if (
    !/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/.test(
      lp.value
    )
  ) {
    if (document.activeElement.id !== "lp") {
      window.alert("Wrong Input key!");
    }
  }
}