求修复基于用户角色的LinkedIn自动连接脚本
Hey there! Let's dig into fixing your LinkedIn "People You May Know" auto-invite script step by step. I spot a few key issues in your code, plus some LinkedIn-specific quirks we need to account for.
First, let's break down the gaps in your original code and adapt it to LinkedIn's current UI and constraints:
1. Fix the Inviter Object Initialization
Your original line var inviter = {} || inviter; is backwards—if inviter isn't defined yet, this just sets it to an empty object, but the logic should prioritize an existing inviter if it exists. Correct it to:
var userRole = ["CEO", "CIO"]; var inviter = inviter || {}; // Proper fallback: use existing instance or create new inviter.userList = [];
2. Update LinkedIn's Button Class Name
LinkedIn frequently updates its DOM classes, and button-secondary-small is no longer used for "Connect" buttons in the "People You May Know" section. We'll target buttons more reliably using current class patterns.
3. Add Role-Filtering Logic
You have a userRole array, but no code to actually match users against these roles. We'll add a function to check if a user's job title includes your target roles.
4. Add Loading Delays & Rate Limit Protection
LinkedIn blocks users who send too many connection requests too fast. We'll add delays between actions, plus wait for content to load after scrolling.
Full Fixed Script
Here's the revised, functional version with all fixes included:
var userRole = ["CEO", "CIO"]; var inviter = inviter || {}; inviter.userList = []; inviter.requestDelay = 2500; // 2.5 seconds between requests (adjust to avoid blocks) inviter.loadDelay = 3000; // Wait time after scrolling for new content // Check if a user's role matches our target list inviter.matchesTargetRole = function(userCard) { const roleElement = userCard.querySelector('.entity-result__primary-subtitle'); if (!roleElement) return false; const roleText = roleElement.textContent.trim().toLowerCase(); return userRole.some(targetRole => roleText.includes(targetRole.toLowerCase())); }; // Send connection request, handle the "Add a note" popup inviter.sendInvite = function(button) { if (button.textContent.trim() === "Connect") { button.click(); // Auto-close the note popup if it appears setTimeout(() => { const closePopupBtn = document.querySelector('.artdeco-modal__dismiss'); if (closePopupBtn) closePopupBtn.click(); }, 600); } }; // Process all visible matching users inviter.processVisibleUsers = function() { const userCards = document.querySelectorAll('.entity-result'); userCards.forEach(card => { if (inviter.matchesTargetRole(card) && !inviter.userList.includes(card)) { const connectBtn = card.querySelector('.artdeco-button--secondary'); if (connectBtn) { inviter.userList.push(card); inviter.sendInvite(connectBtn); } } }); }; // Refresh, load more users, and repeat inviter.refresh = function() { // Scroll to bottom to load additional users window.scrollTo(0, document.body.scrollHeight); // Wait for new content to load, then process setTimeout(() => { inviter.processVisibleUsers(); // Scroll back up (optional) window.scrollTo(0, 0); // Repeat the cycle (stop manually by refreshing the page) setTimeout(inviter.refresh, inviter.requestDelay * 3); }, inviter.loadDelay); }; // Start the script inviter.refresh();
Important Usage Tips
- Rate Limits: If you see LinkedIn warnings, increase the
requestDelayto 3-4 seconds. Avoid running this for extended periods in one session. - UI Changes: If the script stops working, right-click a user card or connect button and select "Inspect" to check for updated class names.
- Manual Stop: To halt the script, open your browser's dev console (F12) and run
clearTimeout(inviter.refresh)or simply refresh the page.
内容的提问来源于stack exchange,提问作者Kingx

