Skip to content

Instantly share code, notes, and snippets.

@JulianNorton
Last active August 14, 2025 19:08
Show Gist options
  • Select an option

  • Save JulianNorton/d063063a55032ffb8a88b979e6569d65 to your computer and use it in GitHub Desktop.

Select an option

Save JulianNorton/d063063a55032ffb8a88b979e6569d65 to your computer and use it in GitHub Desktop.
Removed old linkedin connection requests
(async function () {
const minDelay = 1000; // ms
const maxDelay = 3000; // ms
const maxInvitationAgeDays = 21; // 3 weeks
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
const randomDelay = () => Math.floor(Math.random() * (maxDelay - minDelay + 1)) + minDelay;
// Parse "Sent X months/weeks/days ago" into days
const parseAgeInDays = text => {
if (!text) return Infinity;
const t = text.toLowerCase().trim();
const match = t.match(/sent\s+(\d+)\s+(day|days|week|weeks|month|months|year|years)\s+ago/);
if (!match) return Infinity;
const num = parseInt(match[1], 10);
switch (match[2]) {
case 'day': case 'days': return num;
case 'week': case 'weeks': return num * 7;
case 'month': case 'months': return num * 30;
case 'year': case 'years': return num * 365;
default: return Infinity;
}
};
// Find all visible invitations
const getInvitations = () =>
Array.from(document.querySelectorAll('div[role="listitem"]'))
.map(el => {
// Look for the <p> element that starts with "Sent"
const ageTextEl = Array.from(el.querySelectorAll('p'))
.find(p => p.textContent.trim().toLowerCase().startsWith('sent'));
const ageText = ageTextEl ? ageTextEl.textContent.trim() : '';
const withdrawBtn = Array.from(el.querySelectorAll('button'))
.find(b => b.textContent.trim().toLowerCase() === 'withdraw');
return { el, ageText, withdrawBtn };
})
.filter(inv => inv.withdrawBtn);
console.log("Starting LinkedIn Withdraw Script...");
let counter = 0;
for (const invitation of getInvitations()) {
const daysAgo = parseAgeInDays(invitation.ageText);
if (daysAgo <= maxInvitationAgeDays) {
console.log(`Skipping recent invitation (${invitation.ageText})`);
continue;
}
console.log(`Withdrawing invitation: ${invitation.ageText}`);
invitation.withdrawBtn.click();
// Wait for confirmation dialog
let confirmBtn;
for (let i = 0; i < 10; i++) {
confirmBtn = Array.from(document.querySelectorAll('button'))
.find(b => b.textContent.trim().toLowerCase() === 'withdraw' && b.closest('dialog'));
if (confirmBtn) break;
await sleep(300);
}
if (confirmBtn) {
confirmBtn.click();
console.log(`Confirmed withdrawal (${invitation.ageText})`);
counter++;
} else {
console.warn("Confirmation button not found, skipping.");
}
await sleep(randomDelay());
}
console.log(`✅ Done! Withdrawn ${counter} invitations older than ${maxInvitationAgeDays} days.`);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment