-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.js
151 lines (132 loc) · 6.8 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
async function linkedInConnectBot() {
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const randomDelay = () => Math.floor(Math.random() * 2000) + 1000;
const processedProfiles = new Set();
// Get a unique identifier for a profile
const getProfileId = (element) => {
try {
const profileContainer = element.closest('li') || element.closest('.entity-result');
if (profileContainer) {
const nameElement = profileContainer.querySelector('span[aria-hidden="true"], a[href*="/in/"]');
if (nameElement) return nameElement.textContent.trim();
}
return Math.random().toString();
} catch (e) {
return Math.random().toString();
}
};
for (let i = 0; i < 1000; i++) {
try {
// Clear any open modals first
const anyModalCloseButton = document.querySelector('button[data-test-modal-close-btn], button[aria-label="Dismiss"]');
if (anyModalCloseButton) {
console.log("Found open modal, closing it first...");
anyModalCloseButton.click();
await sleep(randomDelay());
}
// Find profile links that we haven't processed yet
const profileLinks = Array.from(document.querySelectorAll('a[href*="/in/"]')).filter(link => {
const container = link.closest('li') || link.closest('.entity-result');
if (!container) return false;
const profileId = getProfileId(link);
return profileId && !processedProfiles.has(profileId);
});
if (profileLinks.length === 0) {
console.log("No more unprocessed profiles found. Scrolling down...");
window.scrollTo(0, document.body.scrollHeight);
await sleep(3000);
// Check if we need to go to the next page
const nextButton = document.querySelector('button[aria-label="Next"]');
if (nextButton) {
nextButton.click();
console.log("Moving to next page...");
await sleep(5000);
continue;
} else {
console.log("Script completed - no more profiles to process.");
break;
}
}
// Get the first unprocessed profile link
const profileLink = profileLinks[0];
const profileId = getProfileId(profileLink);
processedProfiles.add(profileId);
console.log(`Visiting profile: ${profileId}`);
// Store current URL to go back later
const currentUrl = window.location.href;
// Click on the profile
profileLink.click();
await sleep(5000); // Wait for profile page to load
// Find and click the More button on the profile page
const moreButton = document.querySelector('button[aria-label="More actions"]');
if (moreButton) {
console.log("Clicking More button on profile page...");
moreButton.click();
await sleep(randomDelay());
// Find and click Connect in dropdown
const connectOptions = document.querySelectorAll('div[aria-label*="Invite"][aria-label*="connect"]');
if (connectOptions.length > 0) {
console.log("Clicking Connect option in dropdown...");
connectOptions[0].click();
await sleep(randomDelay());
// Handle any "Add a note" modal
const addNoteModal = document.querySelector('div[data-test-modal][role="dialog"].send-invite');
if (addNoteModal) {
const sendButton = addNoteModal.querySelector('button[aria-label="Send without a note"]');
if (sendButton && !sendButton.disabled) {
console.log("Clicking 'Send without a note'...");
sendButton.click();
await sleep(randomDelay());
console.log("Connection request sent!");
} else {
// If we can't send without a note, close the modal
const closeButton = addNoteModal.querySelector('button[aria-label="Dismiss"]');
if (closeButton) {
console.log("Can't send without a note, closing modal...");
closeButton.click();
await sleep(randomDelay());
}
}
} else {
// Look for direct Send button
const sendButton = document.querySelector('button[aria-label="Send now"]') ||
document.querySelector('button[aria-label="Send"]') ||
document.querySelector('button[aria-label="Done"]') ||
document.querySelector('button[aria-label="Send without a note"]');
if (sendButton) {
console.log("Clicking Send button...");
sendButton.click();
await sleep(randomDelay());
console.log("Connection request sent!");
}
}
} else {
console.log("Connect option not found in dropdown");
}
} else {
console.log("More button not found on profile page");
}
// Go back to search results
console.log("Going back to search results...");
window.history.back();
await sleep(3000);
// If URL didn't change properly, navigate directly
if (window.location.href !== currentUrl) {
window.location.href = currentUrl;
await sleep(5000);
}
} catch (error) {
console.error("Error:", error);
await sleep(3000);
// Try to go back to search results if there was an error
try {
window.history.back();
await sleep(3000);
} catch (e) {
console.error("Error going back:", e);
}
}
}
}
console.log("Starting LinkedIn profile-based connection bot...");
linkedInConnectBot();