Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
2.5 kB
1
Indexable
/*global chrome*/
/*eslint no-undef: "error"*/
const profileNameSeparation = (profileName) => {
  const nameData = profileName.split(" ");
  const firstName = nameData[0];
  const lastName = nameData.slice(1).join(" ");
  return { firstName, lastName };
};

function removeEmojis(text) {
  const emojiRegex = /^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu;
  return text.replace(emojiRegex, "");
}

function extractBeforeCommaSpace(fullString) {
  const commaSpaceIndex = fullString.indexOf(", ");

  if (commaSpaceIndex === -1) {
    return fullString;
  }

  return fullString.slice(0, commaSpaceIndex);
}

const getProfileData = (callback) => {
  const profileNameElement = document.querySelector("main h1");
  const cityElement = document.querySelector("main > section > div > div > div > span");

  if (!profileNameElement) {
    callback(null);
    return;
  }

  const profileName = profileNameElement.innerText.trim();
  const cleanedProfileName = removeEmojis(profileName);
  const fullName = profileNameSeparation(cleanedProfileName);

  const firstName = removeEmojis(fullName.firstName);
  const lastName = removeEmojis(fullName.lastName);
  const city = extractBeforeCommaSpace(cityElement.innerText);
  const zipCode = "";
  const mobileNumber = "";
  const url = document.location.href;

  fetchEmail(function (email) {
    const profileData = { email, firstName, lastName, city, zipCode, mobileNumber, url };

    console.log("Profile Data:", profileData);

    callback(profileData);
  });
};

function fetchEmail(callback) {
  try {
    const contactInfoLink = document.querySelector("#top-card-text-details-contact-info");
    if (contactInfoLink) {
      contactInfoLink.click();
    } else {
      callback("");
      return;
    }

    setTimeout(() => {
      const emailElement = document.querySelector('a[href^="mailto:"]');
      const email = emailElement ? emailElement.innerText.trim() : "";

      const closeButton = document.querySelector(".artdeco-modal__dismiss");
      if (closeButton) {
        closeButton.click();
      }

      callback(email);
    }, 1000);
  } catch (error) {
    callback("");
  }
}

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.action === "getProfileData") {
    if (document.location.href.includes("linkedin.com/in/")) {
      getProfileData(function (profileData) {
        sendResponse(profileData);
      });
    } else {
      sendResponse(null);
    }
    return true;
  }
});
Leave a Comment