daksh / Fb Lead extractor

// ==UserScript==
// @name         Fb Lead extractor 
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  try to take over the world!
// @author       You
// @match        https://www.facebook.com/groups/amberstudent.*/*
// @grant        none
// @license      MIT
// ==/UserScript==

const getButton = (name, email, phone, message) => {
  let string = "?add=true&name=" + name;
  if (email && email.length > 0) {
    string += "&email=" + email;
  }

  if (phone && phone.length > 0) {
    string += "&phone=" + phone;
  }

  if (message && message.length > 0) {
    string += "&message=" + message;
  }

  string += "&utm_source=online&utm_campaign=facebook group";

  const aTag = document.createElement("a");
  aTag.setAttribute(
    "href",
    `https://amberstudent.com/dashboard/leads${string}`
  );
  aTag.setAttribute("target", "_blank");
  aTag.innerHTML = "Create Lead";
  aTag.style =
    "color: white; background-color: purple; padding:10px; border-radius: 4px;";
  return aTag;
};

const extractText = () => {
  const emailRegex = /\S+[a-z0-9]@[a-z0-9\.]+/gim;
  // const phoneRegex = new RegExp(
  //   "\\+?\\(?\\d*\\)? ?\\(?\\d+\\)?\\d*([\\s./-]\\d{2,})+",
  //   "g"
  // );

  const EMAIL_STRING =
    "To receive listings according to your preferences from AmberStudent, Please provide your email address";
  const MESSAGE_STRING =
    "Kindly share your preferences with us? (Location/University, Budget, Lease Duration, Move-in Date)";
  const PHONE_STRING =
    "Provide your contact number (with country code) to get prompt assistance with accommodation search, within 24 hours on WhatsApp/Call";

  const uls = document.querySelectorAll("ul");

  // Would have index issues because not everyone answers the questions.
  // const names = document.querySelectorAll("div > span > span > div > a");

  uls.forEach((ul) => {
    const lis = ul.querySelectorAll("li");
    // const name = names[index].textContent;
    // console.log(name);
    let email = "";
    let phoneNum = "";
    let message = "";
    for (let li of lis) {
      const qSpans = li.querySelectorAll("span");
      const span = li.querySelector("div > span");

      qSpans.forEach((qSpan) => {
        const textContent = qSpan.textContent;
        // if (textContent === EMAIL_STRING) {
        //   email = span.textContent;
        // }
        if (textContent.includes('your email address')) {
          email = span.textContent;
        }
        else if (textContent === PHONE_STRING) {
          phoneNum = span.textContent;
        }
        else if (textContent === MESSAGE_STRING) {
          message = span.textContent;
        }
      });
    }

    if (email.match(emailRegex)) {
      const buttonTag = getButton("", email, phoneNum, message);
      ul.appendChild(buttonTag);
    }
  });
};

(function () {
  window.addEventListener("load", () => {
    setTimeout(extractText, 5000);
  });
})();