Last active
February 23, 2024 10:23
-
-
Save edspiner/bd497d9280f391cfe1bc7e0aea527534 to your computer and use it in GitHub Desktop.
JS code to be executed in browser console that will get list of org people an positions in JSON variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* WARNING: THIS SCRIPT RELIES HEAVILY ON LINKEDIN HTML STRUCTURE SO WHEN IT'S CHANGED IT MAY BREAK. | |
* YOU NEED TO ADJUST IT ON YOUR OWN THEN | |
* | |
* This is a JS script to get list of people from LinkedIn organisation `People` page. | |
* It gets names and positions in a JSON format. | |
* The list can be later used to geneate list of emails | |
* | |
*/ | |
const EMAIL_FORMAT = '{f}{last}@exmaple.com'; // supported vars {first}{f}{last}{l} | |
/* ------ */ | |
const people = [['Email','Position']]; | |
const pplTotal = parseInt(document.querySelector('div.org-people__insights-container h2').textContent.trim().match(/([0-9,]+)\s/)[1].replace(/,/g,'')); | |
// generate email from full name based on template | |
function generateEmail(fullName, template) { | |
const cleanName = fullName.replace(/\-/,' ').replace(/[\']/g, '').trim(); // TODO this needs to bo tunned | |
const nameParts = cleanName.split(/\s+/); | |
const firstName = nameParts[0]; | |
const lastName = nameParts[nameParts.length - 1]; | |
const variables = { | |
'{first}': firstName.toLowerCase(), | |
'{f}': firstName[0].toLowerCase(), | |
'{last}': lastName.toLowerCase(), | |
'{l}': lastName[0].toLowerCase(), | |
}; | |
return template.replace(/{first}|{f}|{last}|{l}/g, function(match) { | |
return variables[match]; | |
}); | |
} | |
// get People and generate CSV | |
let getPeopleRunningLock = 0; | |
function getPeople() { | |
getPeopleRunningLock = 1; | |
const profiles = document.querySelectorAll('.org-people-profile-card'); | |
if (profiles.length === pplTotal || !document.querySelector('.scaffold-finite-scroll__load-button')) { | |
console.log('Finished pagination, generating CSV'); | |
profiles.forEach(profile => { | |
const title = profile.querySelector('.org-people-profile-card__profile-title').innerText; | |
const subtitle = profile.querySelector('.artdeco-entity-lockup__subtitle .lt-line-clamp').innerText; | |
people.push([generateEmail(title, EMAIL_FORMAT), subtitle]) | |
}) | |
let csvContent = people.map(e => e.join(";")).join("\n"); | |
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); | |
const url = URL.createObjectURL(blob); | |
const a = document.createElement('a'); | |
a.href = url; | |
a.download = 'emails.csv'; | |
document.body.appendChild(a); | |
a.click(); | |
URL.revokeObjectURL(url); | |
document.body.removeChild(a); | |
} else { | |
console.log('Loading more people: ' + profiles.length + '/' + pplTotal) | |
} | |
getPeopleRunningLock = 0; | |
} | |
let moreBtn = document.querySelector('.scaffold-finite-scroll__load-button'); | |
const callback = (mutationList, observer) => { moreBtn = document.querySelector('.scaffold-finite-scroll__load-button'); if(moreBtn) { moreBtn.click(); } else { getPeople(); }} | |
const peopleCardContainer = document.querySelector('.org-people-profile-card').parentElement.parentElement.parentElement | |
const obs = new MutationObserver(callback); | |
obs.observe(peopleCardContainer, { attributes: true, childList: true, subtree: true }); | |
moreBtn.click(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment