-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
759 lines (643 loc) · 26.2 KB
/
Copy pathcontent.js
File metadata and controls
759 lines (643 loc) · 26.2 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
// Content Script - Hydration-Safe, Viewport-Scroll Lazy-Loaded & Exact About Node Extractor
const BACKEND_URL = 'http://localhost:5000/api';
const MATCH_ROLES = [
'ceo', 'founder', 'co-founder', 'owner', 'director', 'managing director',
'investor', 'partner', 'president', 'vp', 'vice president', 'cfo', 'cto', 'cmo', 'chief executive', 'managing partner', 'board chairman', 'founder | ceo', 'chief technology officer', 'coo', `entrepreneur`
];
const ALLOWED_LOCATIONS = [
'pakistan', 'united states', 'usa', 'turkey', 'türkiye', 'turkiye', 'canada', 'australia',
'germany', 'france', 'spain', 'italy', 'united kingdom', 'uk', 'uae',
'united arab emirates', 'dubai', 'istanbul'
];
const BLOCKED_LOCATIONS = ['israel', 'india'];
// ---------------------------------------------------------------------------
// 0. HUD LIVE LOGGER INTERCEPTOR
// ---------------------------------------------------------------------------
const originalConsoleLog = console.log;
const originalConsoleWarn = console.warn;
const originalConsoleError = console.error;
function pushToHUD(category, message, details = null) {
try {
if (typeof chrome === 'undefined' || !chrome.storage || !chrome.storage.local) return;
chrome.storage.local.get(['scraperLogs'], (res) => {
let logs = res.scraperLogs || [];
const timestamp = new Date().toISOString().replace('T', ' ').substring(0, 19);
logs.push({
timestamp,
category,
message,
details: details ? JSON.stringify(details, null, 2) : null
});
if (logs.length > 200) logs = logs.slice(logs.length - 200);
chrome.storage.local.set({ scraperLogs: logs });
});
} catch (e) {}
}
console.log = function(...args) {
originalConsoleLog.apply(console, args);
const str = args.join(' ');
const match = str.match(/^\[(.*?)\]\s*(.*)/);
if (match) {
pushToHUD(match[1], match[2]);
} else {
pushToHUD('LOG', str);
}
};
console.warn = function(...args) {
originalConsoleWarn.apply(console, args);
const str = args.join(' ');
const match = str.match(/^\[(.*?)\]\s*(.*)/);
if (match) {
pushToHUD(match[1], match[2]);
} else {
pushToHUD('WARN', str);
}
};
console.error = function(...args) {
originalConsoleError.apply(console, args);
pushToHUD('ERROR', args.join(' '));
};
// ---------------------------------------------------------------------------
// 1. EXTENSION CIRCUIT BREAKER & CONSOLE NOISE SUPPRESSORS
// ---------------------------------------------------------------------------
function isContextValid() {
try {
return typeof chrome !== 'undefined' && !!chrome.runtime && !!chrome.runtime.id;
} catch (e) {
return false;
}
}
window.addEventListener('unhandledrejection', (event) => {
if (!isContextValid() || (event.reason && String(event.reason).includes('Extension context invalidated'))) {
event.preventDefault();
event.stopPropagation();
}
}, true);
window.addEventListener('error', (event) => {
if (!isContextValid() || (event.message && String(event.message).includes('Extension context invalidated'))) {
event.preventDefault();
event.stopPropagation();
}
}, true);
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function getRandomDelay(minSeconds, maxSeconds) {
const min = minSeconds * 1000;
const max = maxSeconds * 1000;
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function shuffleArray(array) {
const arr = [...array];
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
function getCanonicalProfileUrl(url) {
if (!url) return '';
try {
const match = url.match(/https:\/\/(www\.)?linkedin\.com\/in\/[^/?#]+/i);
if (match) {
return match[0].toLowerCase();
}
} catch (e) {}
return url.split('?')[0].replace(/\/overlay\/contact-info\/?$/i, '').replace(/\/$/, '').toLowerCase();
}
// Visited Local Storage Cache Manager & Backend Sync
async function markUrlAsVisited(url) {
if (!isContextValid() || !url) return;
try {
const cleanUrl = getCanonicalProfileUrl(url);
const data = await chrome.storage.local.get(['visitedUrls']);
const visited = data.visitedUrls || [];
if (!visited.includes(cleanUrl)) {
visited.push(cleanUrl);
await chrome.storage.local.set({ visitedUrls: visited });
console.log(`[Cache] Recorded profile URL into visited local cache: ${cleanUrl}`);
// Ping background service worker to update visit count limits
chrome.runtime.sendMessage({ action: 'RECORD_VISIT' });
// Sync to MongoDB so it doesn't loop across sessions/queues
fetch(`${BACKEND_URL}/visited`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ profileUrl: cleanUrl })
}).catch(() => {});
}
} catch (e) {}
}
async function isProfileAlreadyScraped(profileUrl) {
try {
const cleanUrl = getCanonicalProfileUrl(profileUrl);
const encoded = encodeURIComponent(cleanUrl);
const response = await fetch(`${BACKEND_URL}/check-scraped?url=${encoded}`);
const data = await response.json();
return data.scraped;
} catch (err) {
return false;
}
}
// Pre-Filter Helper: Filters out visited/scraped URLs BEFORE enqueuing
async function filterUnvisitedUrls(urls) {
if (!isContextValid() || !urls || urls.length === 0) return [];
let visitedLocal = [];
try {
const data = await chrome.storage.local.get(['visitedUrls']);
visitedLocal = data.visitedUrls || [];
} catch (e) {}
const freshUrls = [];
for (const url of urls) {
if (!isContextValid()) break;
const cleanUrl = getCanonicalProfileUrl(url);
if (visitedLocal.includes(cleanUrl)) {
console.log(`[Pre-Filter Cache Skip] ${cleanUrl}`);
continue;
}
const isScraped = await isProfileAlreadyScraped(cleanUrl);
if (isScraped) {
console.log(`[Pre-Filter DB Skip] ${cleanUrl}`);
await markUrlAsVisited(cleanUrl);
continue;
}
freshUrls.push(cleanUrl);
}
return freshUrls;
}
// Safe Navigation Guard
async function safeNavigate(nextUrl, delaySeconds = getRandomDelay(10, 16) / 1000) {
if (!isContextValid()) return;
console.log(`[Pacing] Sleeping ${delaySeconds.toFixed(1)}s before navigating...`);
await sleep(delaySeconds * 1000);
if (!isContextValid()) return;
try {
chrome.runtime.sendMessage({ action: 'NAVIGATE_TO_URL', url: nextUrl });
} catch (err) {
window.location.href = nextUrl;
}
}
// Top Card Selector Extraction
function extractTopCardData() {
let name = '';
const allHeadings = Array.from(document.querySelectorAll('main h1, main h2, section h1, section h2, .pv-text-details__left-panel h1, .pv-text-details__left-panel h2'));
const nameEl = allHeadings.find(h => {
const text = (h.innerText || '').trim();
return text.length > 1 && text.length < 60 &&
!text.toLowerCase().includes('notifications') &&
!['about', 'activity', 'experience', 'education', 'skills', 'languages', 'certifications', 'projects', 'people also viewed'].includes(text.toLowerCase());
});
if (nameEl) name = nameEl.innerText.trim();
let headline = '';
const headlineEl = document.querySelector('.text-body-medium.break-words, div.text-body-medium, p.b36aec85');
if (headlineEl) {
headline = headlineEl.innerText.trim();
} else {
const topCard = document.querySelector('section[componentkey*="topcard"], main') || document.body;
const elements = Array.from(topCard.querySelectorAll('p, div, span'));
for (const el of elements) {
const text = el.innerText ? el.innerText.trim() : '';
const textLower = text.toLowerCase();
if (text.length > 5 && text.length < 180 && MATCH_ROLES.some(role => textLower.includes(role))) {
headline = text;
break;
}
}
}
let location = '';
const contactAnchor = getExactContactInfoLink();
if (contactAnchor) {
const container = contactAnchor.closest('div');
if (container) {
const locP = Array.from(container.querySelectorAll('p, span')).find(el => {
const text = (el.innerText || '').trim();
return text.length > 3 && !text.includes('Contact info') && !text.includes('·') && text !== name && text !== headline;
});
if (locP) location = locP.innerText.trim();
}
}
if (!location) {
const locationEls = Array.from(document.querySelectorAll('.pv-text-details__left-panel .text-body-small.inline, span.text-body-small.inline, p.a6523ea3'));
const locationEl = locationEls.find(el => {
const text = (el.innerText || '').trim();
return text !== name && text !== headline && text.length > 3 && !text.includes('Contact info') && !text.includes('·');
});
if (locationEl) {
location = locationEl.innerText.trim();
}
}
return { name, headline, location };
}
function validateRoleAndLocation(headlineText, locationText) {
const headlineLower = (headlineText || '').toLowerCase();
const locationLower = (locationText || '').toLowerCase();
if (BLOCKED_LOCATIONS.some(blocked => locationLower.includes(blocked))) {
console.log(`[Filter Reject] Blocked location: "${locationText}"`);
return false;
}
const matchesRole = MATCH_ROLES.some(role => headlineLower.includes(role));
if (!matchesRole) {
console.log(`[Filter Reject] Headline "${headlineText}" does not contain target roles.`);
return false;
}
return true;
}
// ---------------------------------------------------------------------------
// 2. EXACT NODE TARGETING FOR CONTACT INFO LINK
// ---------------------------------------------------------------------------
function getExactContactInfoLink() {
const candidateAnchors = Array.from(document.querySelectorAll('p > a[href*="/overlay/contact-info/"], span > a[href*="/overlay/contact-info/"]'));
for (const anchor of candidateAnchors) {
const text = (anchor.innerText || '').trim().toLowerCase();
const isLogoPhoto = anchor.getAttribute('componentkey')?.includes('logo-image');
const isInsideDropdown = !!anchor.closest('ul, li, [role="menu"], [role="menuitem"], .artdeco-dropdown');
if (text === 'contact info' && !isLogoPhoto && !isInsideDropdown) {
return anchor;
}
}
const allAnchors = Array.from(document.querySelectorAll('a[href*="/overlay/contact-info/"]'));
return allAnchors.find(a => {
const isLogo = a.getAttribute('componentkey')?.includes('logo-image');
const isInsideMenu = !!a.closest('ul, li, [role="menu"], [role="menuitem"]');
return !isLogo && !isInsideMenu;
}) || null;
}
function getContactInfoModalContainer() {
const mailtoLink = document.querySelector('a[href^="mailto:" i]');
if (mailtoLink) {
return mailtoLink.closest('div[data-component-type], section, div.artdeco-modal, div[role="dialog"]') || mailtoLink.parentElement;
}
const modals = Array.from(document.querySelectorAll('.artdeco-modal, div[role="dialog"], #pv-contact-info, div[componentkey*="contact"]'));
for (const m of modals) {
const text = (m.innerText || '').toLowerCase();
if (text.includes('contact info') || text.includes('email') || m.querySelector('a[href^="mailto:" i]')) {
return m;
}
}
return null;
}
async function extractEmailFromModal() {
console.log('[Contact Modal] Locating exact Contact Info node...');
let mailtoAnchor = document.querySelector('a[href^="mailto:" i]');
if (mailtoAnchor) {
const email = mailtoAnchor.href.replace(/^mailto:/i, '').trim();
console.log(`[Contact Modal] Fast-path email found on DOM: ${email}`);
return email;
}
let modal = getContactInfoModalContainer();
if (!modal) {
const contactAnchor = getExactContactInfoLink();
if (contactAnchor) {
console.log(`[Contact Modal] Found exact node: <a href="${contactAnchor.href}">. Invoking native click...`);
contactAnchor.click();
await sleep(3000);
modal = getContactInfoModalContainer();
} else {
console.warn('[Contact Modal] Exact contact info anchor node not found in top card.');
}
}
if (!modal && !window.location.href.includes('/overlay/contact-info/')) {
console.log('[Contact Modal] DOM click did not render dialog. Executing headless overlay fetch fallback...');
const currentBaseUrl = window.location.href.split('?')[0].replace(/\/$/, '');
const overlayUrl = `${currentBaseUrl}/overlay/contact-info/`;
try {
const res = await fetch(overlayUrl);
const htmlText = await res.text();
const emailRegex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi;
const matches = htmlText.match(emailRegex);
if (matches && matches.length > 0) {
const validEmails = matches.filter(e => !e.includes('sentry.io') && !e.includes('linkedin.com') && !e.includes('schema.org'));
if (validEmails.length > 0) {
console.log(`[Contact Modal] Headless fetch email match: ${validEmails[0]}`);
return validEmails[0].trim();
}
}
} catch (e) {}
}
let email = null;
const container = modal || document.body;
if (container) {
const mailto = container.querySelector('a[href^="mailto:" i]');
if (mailto) {
email = mailto.href.replace(/^mailto:/i, '').trim();
} else {
const containerText = container.innerText || '';
const emailRegex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi;
const matches = containerText.match(emailRegex);
if (matches && matches.length > 0) {
email = matches[0].trim();
}
}
if (modal) {
const closeBtn = modal.querySelector('button[aria-label="Dismiss"], button.artdeco-modal__dismiss, button[type="button"]');
if (closeBtn) closeBtn.click();
}
}
return email;
}
// ---------------------------------------------------------------------------
// 3. LAZY-LOADED ABOUT SECTION EXTRACTION
// ---------------------------------------------------------------------------
async function extractAboutDescription() {
console.log('[About Extractor] Triggering viewport scroll to hydrate About section...');
const aboutCard = document.querySelector('section[componentkey*="About"], #about');
if (aboutCard) {
try {
aboutCard.scrollIntoView({ behavior: 'smooth', block: 'center' });
await sleep(1500); // Wait for SDUI to hydrate content
} catch (e) {}
} else {
// Fallback scroll down ~35vh if explicit About card isn't matched
window.scrollBy({ top: window.innerHeight * 0.35, behavior: 'smooth' });
await sleep(1200);
}
// Target the exact span containing the bio description
const aboutSpan = document.querySelector(
'section[componentkey*="About"] span[data-testid="expandable-text-box"], ' +
'section[componentkey*="About"] span._411a3941, ' +
'section[componentkey*="About"] p, ' +
'#about ~ div span[aria-hidden="true"], ' +
'section.summary-section'
);
if (aboutSpan) {
const descText = aboutSpan.innerText ? aboutSpan.innerText.trim() : '';
console.log(`[About Extractor] Extracted bio description (${descText.length} chars)`);
return descText;
}
console.warn('[About Extractor] Bio description span node not found on DOM.');
return '';
}
// Core Individual Profile Processing Workflow
async function processCurrentProfile() {
if (!isContextValid()) return;
const currentUrl = getCanonicalProfileUrl(window.location.href);
console.log(`[Scraper] Processing Profile: ${currentUrl}`);
await sleep(3500); // Hydration stabilization delay
if (!isContextValid()) return;
await markUrlAsVisited(currentUrl);
// 1. Top Card Extraction
const { name, headline, location } = extractTopCardData();
console.log(`[Extracted Candidate] Name: "${name}" | Headline: "${headline}" | Location: "${location}"`);
// 2. Role Filter Validation
if (!validateRoleAndLocation(headline, location)) {
console.log('[Scraper] Candidate failed role filter. Skipping with pacing delay...');
return navigateToNextInQueue();
}
// 3. Contact Email Extraction
const email = await extractEmailFromModal();
// STRICT RULE: If no email exists, DO NOT SAVE
if (!email) {
console.warn(`[Scraper] No email found for profile: ${currentUrl}. Skipping MongoDB save.`);
return navigateToNextInQueue();
}
// 4. Viewport Scroll-Triggered About & Experience Extraction
const description = await extractAboutDescription();
async function extractExperienceData() {
console.log('[Experience Extractor] Triggering viewport scroll to hydrate Experience section...');
const expCard = document.querySelector('section[componentkey*="Experience"], #experience');
if (expCard) {
try {
expCard.scrollIntoView({ behavior: 'smooth', block: 'center' });
await sleep(1500);
} catch (e) {}
} else {
window.scrollBy({ top: window.innerHeight * 0.85, behavior: 'smooth' });
await sleep(1500);
}
let expSection = document.querySelector('section[componentkey*="ExperienceTopLevelSection"], #experience ~ div, #experience');
// Fallback: If section not found, scroll a bit more to trigger lazy load
if (!expSection) {
console.warn('[Experience Extractor] Experience section not found on first attempt. Scrolling further as fallback...');
window.scrollBy({ top: window.innerHeight * 0.85, behavior: 'smooth' });
await sleep(1500);
expSection = document.querySelector('section[componentkey*="ExperienceTopLevelSection"], #experience ~ div, #experience');
}
if (!expSection) {
console.warn('[Experience Extractor] Experience section not found on DOM after fallback.');
return '[]';
}
const experiences = [];
let expItems = expSection.querySelectorAll('div[componentkey^="entity-collection-item-"]');
// Fallback: If section is found but items haven't hydrated yet
if (expItems.length === 0) {
console.warn('[Experience Extractor] Experience section found, but items are empty. Waiting for hydration...');
window.scrollBy({ top: window.innerHeight * 0.85, behavior: 'smooth' });
await sleep(1500);
expItems = expSection.querySelectorAll('div[componentkey^="entity-collection-item-"]');
}
expItems.forEach(item => {
try {
const spans = Array.from(item.querySelectorAll('span[aria-hidden="true"]'))
.map(s => s.innerText.trim())
.filter(t => t.length > 0);
if (spans.length === 0) return;
let title = spans[0];
let company = '';
let duration = '';
let locationStr = '';
// Find which span looks like a duration (e.g., "Jan 2020 - Present · 2 yrs")
const durationIdx = spans.findIndex(text =>
text.includes(' - ') || text.includes(' – ') || /\b(?:yr|yrs|mo|mos|Present)\b/.test(text)
);
if (durationIdx !== -1) {
duration = spans[durationIdx];
// If duration is at index 2, index 1 is company
if (durationIdx === 2) {
company = spans[1];
} else if (durationIdx === 1) {
// Nested role (company is outside, index 1 is duration)
// Or they omitted company. We leave company blank.
}
// Location is usually right after duration
if (spans.length > durationIdx + 1) {
locationStr = spans[durationIdx + 1];
}
} else {
// Fallback if duration isn't detected
if (spans.length >= 2) company = spans[1];
if (spans.length >= 3) duration = spans[2];
if (spans.length >= 4) locationStr = spans[3];
}
if (company.includes('·')) company = company.split('·')[0].trim();
if (locationStr.includes('·')) locationStr = locationStr.split('·')[0].trim();
const descEl = item.querySelector('span[data-testid="expandable-text-box"], .inline-show-more-text');
let desc = descEl ? descEl.innerText.replace('… more', '').trim() : '';
if (title || company) {
experiences.push({
title,
company,
duration,
location: locationStr,
description: desc
});
}
} catch (e) {
console.warn('[Experience Extractor] Error parsing an experience item', e);
}
});
console.log(`[Experience Extractor] Extracted ${experiences.length} experience entries.`);
return JSON.stringify(experiences);
}
const experience = await extractExperienceData();
// 5. Post Payload to MongoDB Express Bridge
const leadPayload = {
name,
headline,
email,
description,
experience,
profileUrl: currentUrl,
location,
scrapedAt: new Date().toISOString()
};
try {
const saveRes = await fetch(`${BACKEND_URL}/leads`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(leadPayload)
});
const saveResult = await saveRes.json();
if (saveResult.success && isContextValid()) {
console.log(`[SUCCESS] Lead Saved to MongoDB Atlas: ${name} (${email})`);
chrome.runtime.sendMessage({ action: 'RECORD_LEAD_SUCCESS' });
}
} catch (err) {
console.error('[Save Error]', err);
}
// 6. Collect discovery sidebar links
await collectDiscoveryProfileUrls();
// 7. Move to next target
return navigateToNextInQueue();
}
// Randomized Traversal Link Collector
async function collectDiscoveryProfileUrls() {
try {
const targetSections = Array.from(document.querySelectorAll('section, div')).filter(sec => {
const heading = sec.querySelector('h2, h3, header');
if (!heading) return false;
const text = heading.innerText.toLowerCase();
return text.includes('more profiles for you') ||
text.includes('explore premium profiles') ||
text.includes('people you may know');
});
let extractedUrls = [];
if (targetSections.length > 0) {
targetSections.forEach(sec => {
const anchors = Array.from(sec.querySelectorAll('a[href*="/in/"]'));
anchors.forEach(a => {
const url = getCanonicalProfileUrl(a.href);
if (url.includes('/in/') && !url.endsWith('/in/')) {
extractedUrls.push(url);
}
});
});
} else {
const anchors = Array.from(document.querySelectorAll('aside a[href*="/in/"], .pv-browse-map a[href*="/in/"]'));
anchors.forEach(a => {
const url = getCanonicalProfileUrl(a.href);
if (url.includes('/in/') && !url.endsWith('/in/')) {
extractedUrls.push(url);
}
});
}
const uniqueUrls = Array.from(new Set(extractedUrls));
if (uniqueUrls.length > 0 && isContextValid()) {
const freshDiscoveryUrls = await filterUnvisitedUrls(uniqueUrls);
const randomizedBatch = shuffleArray(freshDiscoveryUrls);
const data = await chrome.storage.local.get(['queue']);
const currentQueue = data.queue || [];
const updatedQueue = Array.from(new Set([...currentQueue, ...randomizedBatch]));
await chrome.storage.local.set({ queue: updatedQueue });
console.log(`[Discovery] Pre-filtered & enqueued ${randomizedBatch.length} fresh discovery URLs.`);
}
} catch (err) {}
}
// Automated Infinite Scroll & Pre-Filtering on Connections Page
async function parseConnectionsPage() {
console.log('[Connections Page] Parsing 1st-Degree Connections... Starting automated scroll...');
await sleep(3500);
let previousHeight = 0;
let sameHeightCount = 0;
const maxScrollAttempts = 25;
let scrollAttempts = 0;
while (scrollAttempts < maxScrollAttempts) {
window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
await sleep(2500);
const currentHeight = document.body.scrollHeight;
if (currentHeight === previousHeight) {
sameHeightCount++;
if (sameHeightCount >= 2) {
console.log('[Connections Page] End of connections list reached.');
break;
}
} else {
sameHeightCount = 0;
previousHeight = currentHeight;
}
scrollAttempts++;
}
const connectionAnchors = Array.from(document.querySelectorAll('a[href*="/in/"]'));
const extractedUrls = Array.from(new Set(
connectionAnchors
.map(a => getCanonicalProfileUrl(a.href))
.filter(url => url.includes('/in/') && !url.endsWith('/in/'))
));
console.log(`[Connections Page] Extracted ${extractedUrls.length} connections. Pre-filtering already scraped/visited profiles...`);
const freshUrls = await filterUnvisitedUrls(extractedUrls);
console.log(`[Connections Page] ${freshUrls.length} fresh unvisited profiles ready to queue.`);
if (isContextValid() && freshUrls.length > 0) {
const data = await chrome.storage.local.get(['queue']);
const currentQueue = data.queue || [];
const mergedQueue = Array.from(new Set([...freshUrls, ...currentQueue]));
await chrome.storage.local.set({ queue: mergedQueue });
navigateToNextInQueue();
} else if (isContextValid()) {
console.log('[Connections Page] All connections were already processed. Routing existing queue...');
navigateToNextInQueue();
}
}
// Queue Router
async function navigateToNextInQueue() {
if (!isContextValid()) return;
try {
const data = await chrome.storage.local.get(['status', 'queue']);
if (data.status !== 'RUNNING') {
console.log('[Scraper] Scraper is paused or idle.');
return;
}
const queue = data.queue || [];
if (queue.length > 0) {
const nextUrl = queue.shift();
await chrome.storage.local.set({ queue });
console.log(`[Navigation] Navigating to: ${nextUrl}`);
await safeNavigate(nextUrl);
} else {
console.log('[Navigation] Queue empty. Returning to 1st degree connections...');
await safeNavigate('https://www.linkedin.com/mynetwork/invite-connect/connections/');
}
} catch (e) {}
}
async function runScraperFlow() {
if (!isContextValid()) return;
try {
const data = await chrome.storage.local.get(['status']);
if (data.status !== 'RUNNING') return;
const currentUrl = window.location.href;
if (currentUrl.includes('/in/')) {
await processCurrentProfile();
} else if (currentUrl.includes('/mynetwork/invite-connect/connections/')) {
await parseConnectionsPage();
}
} catch (e) {}
}
// Boot Guard
if (isContextValid()) {
chrome.runtime.onMessage.addListener((req) => {
if (req.action === 'START_DOM_SCRAPER') {
runScraperFlow();
}
});
(async () => {
await sleep(2000);
runScraperFlow();
})();
}