Skip to content

Instantly share code, notes, and snippets.

@hamsahmedansari
Created May 1, 2024 16:20
Show Gist options
  • Save hamsahmedansari/e94f3873e1ef5629e7ab887f4830b6c5 to your computer and use it in GitHub Desktop.
Save hamsahmedansari/e94f3873e1ef5629e7ab887f4830b6c5 to your computer and use it in GitHub Desktop.
const getElementsByClassName = (name) => document.getElementsByClassName(name);
const getSingleByClassName = (name) => getElementsByClassName(name)?.[0];
const scrollDivToBottom = (className) => {
var divElement = getSingleByClassName(className);
divElement.scrollTop = divElement.scrollHeight;
};
const wait = (number = 1000) =>
new Promise((res, rej) =>
setTimeout(() => {
res();
}, number)
);
const allApplicantsArray = [];
const getSingleApplicantDetails = (node) =>
new Promise(async (res, rej) => {
try {
{
console.log("firing", node);
await wait(1000);
scrollDivToBottom("hiring-applicants__right-column");
await wait(1000);
scrollDivToBottom("hiring-applicants__right-column");
// select applicant
node.querySelector("a").click();
console.log("๐Ÿš€ ~ newPromise ~ node:", node);
// showing more
getSingleByClassName("hiring-screening-questions__show-more")
?.querySelector("button")
?.click();
// selecting basic information
const name = getSingleByClassName(
"hiring-applicant-header"
).querySelector("h1").innerText;
const subTitle = getSingleByClassName("hiring-applicant-header")
.querySelector("div")
.innerText.split("\n")[3];
const location = getSingleByClassName("hiring-applicant-header")
.querySelector("div")
.innerText.split("\n")[4];
const postedAgo = getSingleByClassName("hiring-applicant-header")
.querySelector("div")
.innerText.split("\n")[5];
let resumeURL;
await wait(1000);
try {
resumeURL =
getSingleByClassName("ui-attachment").querySelector("a").href;
} catch (error) {
console.log("๐Ÿš€ ~ newPromise ~ error:", error);
resumeURL = getSingleByClassName(
"hiring-resume-viewer__resume-wrapper--collapsed"
)?.querySelector("a")?.href;
}
const screeningNode = getSingleByClassName(
"hiring-screening-questions__content-wrapper"
).querySelectorAll("ul");
const screeningQuestion = [];
screeningNode.forEach((element) => {
element.querySelectorAll("li").forEach((li) => {
const item = li.innerText.split("\n");
screeningQuestion.push(item);
});
});
allApplicantsArray.push({
name,
subTitle,
location,
postedAgo,
resumeURL,
screeningQuestion,
});
await wait(1000);
res();
}
} catch (error) {
console.log("๐Ÿš€ ~ newPromise ~ error:", error);
res();
}
});
const getAllItemInPage = () =>
new Promise(async (res, rek) => {
const hiringCountDiv = getSingleByClassName(
"hiring-applicants__count"
).textContent;
// Extract the number from the string
let hiringCount = parseInt(hiringCountDiv.match(/\d+/)[0]);
console.log("๐Ÿš€ ~ hiringCount:", hiringCount);
let allApplicants = getElementsByClassName("hiring-applicants__list-item");
console.log("๐Ÿš€ ~ allApplicants:", allApplicants);
for (let index = 0; index < allApplicants.length; index++) {
const element = allApplicants[index];
await getSingleApplicantDetails(element);
}
console.log("allApplicantsArray", allApplicantsArray);
res();
});
const getHeaders = (item) => {
const headers = ["name", "subTitle", "location", "postedAgo", "resumeURL"];
item?.screeningQuestion?.forEach((scrren) => {
headers.push(scrren[0]);
});
return headers;
};
function arrayToCSV(data) {
const csvRows = [];
// Convert each row to CSV format
data.forEach((row) => {
const csvRow = row.map((value) => `"${value}"`).join(",");
csvRows.push(csvRow);
});
// Combine all CSV rows into a single string
return csvRows.join("\n");
}
const getItem = (item, headers) => {
const body = [];
headers?.forEach((header) => {
const valueFromKey = item[header];
if (valueFromKey) body.push(valueFromKey);
else {
const itemValue = item?.screeningQuestion?.find((screen) =>
screen.includes(header)
);
body.push(itemValue?.[5]);
}
});
return body;
};
async function run() {
// const pagination = getSingleByClassName(
// "artdeco-pagination__pages"
// ).querySelectorAll("li");
// for (let index = 0; index < pagination.length; index++) {
// pagination[index]?.querySelector("button").click();
// }
await getAllItemInPage();
const headers = getHeaders(allApplicantsArray[0]);
const body = [];
allApplicantsArray.forEach((ele) => {
body.push(getItem(ele, headers));
});
const csvData = [
headers,
...body
];
// Convert data to CSV format
const csv = arrayToCSV(csvData);
// Create a Blob from the CSV data
const blob = new Blob([csv], { type: 'text/csv' });
// Create a URL for the Blob
const url = window.URL.createObjectURL(blob);
// Create a link element with the URL of the Blob
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'applicant_data.csv');
// Trigger a click event on the link to start the download automatically
link.click();
// Clean up by revoking the URL
window.URL.revokeObjectURL(url);
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment