amberstudent / Fb Lead extractor

// ==UserScript==
// @name         Fb Lead extractor
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.facebook.com/groups/amberstudent.*/requests/
// @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 joining request";

    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; position: absolute; top: 5px; left: 250px";
    return aTag;

};

const extractText = function() {
    const list = document.querySelector('#member_requests_pagelet .uiList');
    Array.from(list.children).forEach((child)=>{
          if(!child){return};
        child.style.position ="relative";
        const name = child.querySelectorAll('a[data-hovercard]')[1].text;
        const nodeList = child.querySelectorAll('text[truncate]');
        let data = [];
        const emailRegex = /\S+[a-z0-9]@[a-z0-9\.]+/img;
        const phoneRegex = new RegExp(
            "\\+?\\(?\\d*\\)? ?\\(?\\d+\\)?\\d*([\\s./-]\\d{2,})+",
            "g"
        );
        let email = '';
        let phone = '';
        let message = '';

        nodeList.forEach((node, index)=>{
            const text = node.innerHTML;
            data.push(text);
            const emailsMatched = text.match(emailRegex)
            if(emailsMatched && emailsMatched.length > 0){
                email = emailsMatched[0];
            }
            const phonesMatched = text.match(phoneRegex);
            if(phonesMatched && phonesMatched.length > 0){
                phone = phonesMatched[0];
            }

            message += text + "  |  ";
        });

        if(email && email.length>0){
            const buttonTag = getButton(name, email, phone, message);
            child.append(buttonTag);
        }
        console.log(name, email, phone, message);
    });



};

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