Skip to content

Instantly share code, notes, and snippets.

@devinschumacher
Last active October 16, 2024 04:57
Show Gist options
  • Select an option

  • Save devinschumacher/ed53e8a3e5967a597e9d89b7075b0ed5 to your computer and use it in GitHub Desktop.

Select an option

Save devinschumacher/ed53e8a3e5967a597e9d89b7075b0ed5 to your computer and use it in GitHub Desktop.
get post urls from linkedin company page post feed
async function autoScroll(targetCount) {
return new Promise(resolve => {
let totalHeight = 0;
const distance = 100;
let count = 0;
const timer = setInterval(() => {
const scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
// Check if the desired number of posts is collected
const links = document.querySelectorAll('a.update-components-article__meta');
count = links.length;
if (count >= targetCount || totalHeight >= scrollHeight) {
clearInterval(timer);
resolve();
}
}, 100);
});
}
async function getPostUrls(limit) {
const targetCount = limit === 'all' ? Number.MAX_VALUE : parseInt(limit, 10);
await autoScroll(targetCount);
const links = Array.from(document.querySelectorAll('a.update-components-article__meta'))
.slice(0, targetCount)
.map(anchor => anchor.href.split('?')[0]);
return links;
}
function downloadUrls(links) {
const csvContent = links.join("\n");
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", "linkedin_post_urls.csv");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
async function promptAndDownload() {
const input = prompt('Enter the number of URLs to get or "all" for all URLs:');
try {
const links = await getPostUrls(input);
console.log(links); // Log the cleaned links to the console
downloadUrls(links);
alert(`Successfully collected ${links.length} URLs. If the download didn't start automatically, check your browser's download settings or try a different method.`);
} catch (error) {
console.error('An error occurred:', error);
alert('An error occurred while collecting URLs. Please check the console for more details.');
}
}
promptAndDownload();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment