// search_analyzer.js — LinkedIn People Search Investor Analyzer // Operates on linkedin.com/search/results/people/* (function () { 'use strict'; console.log('[LJA] Search Analyzer Script Loaded'); // Prevent double injection if (window.__linkedinSearchAnalyzerLoaded) return; window.__linkedinSearchAnalyzerLoaded = true; // ─── Utility: get stored settings ──────────────────────────────────────── function getSettings() { return new Promise(resolve => { if (!chrome || !chrome.storage) { resolve({}); return; } chrome.storage.sync.get(['apiKey', 'language', 'userProfile'], (syncData) => { if (syncData && syncData.apiKey) { resolve(syncData); } else { chrome.storage.local.get(['apiKey', 'language', 'userProfile'], (localData) => { resolve(localData || {}); }); } }); }); } // ─── Find Result Container ─────────────────────────────────────────────── function getSearchResultsContainer() { return document.querySelector('.search-results-container, .reusable-search__entity-result-list, ul.reusable-search__entity-result-list, .search-results__list'); } // ─── Extract Person Data ───────────────────────────────────────────────── function extractPersonData(cardEl) { const data = { name: '', headline: '', location: '', summary: '' }; try { // 1. Name: The most reliable way is the profile link const profileLinks = Array.from(cardEl.querySelectorAll('a[href*="/in/"]')) .filter(a => a.innerText.trim().length > 2 && !a.innerText.includes('LinkedIn')); if (profileLinks.length > 0) { // Remove any injected language tags let rawName = profileLinks[0].innerText.trim().split('\n')[0]; data.name = rawName.replace(/English \(Australia\)|Auto|Translate/gi, '').trim(); } else { const nameEl = cardEl.querySelector('.entity-result__title-text, .search-result__title, span[dir="ltr"], h3'); if (nameEl) data.name = nameEl.innerText.trim().split('\n')[0].replace(/English \(Australia\)|Auto/gi, '').trim(); } // 2. Headline const headlineEl = cardEl.querySelector('.entity-result__primary-subtitle, [class*="subtitle"], .linked-area'); if (headlineEl) data.headline = headlineEl.innerText.trim(); // 3. Location const locationEl = cardEl.querySelector('.entity-result__secondary-subtitle, .search-result__info'); if (locationEl) data.location = locationEl.innerText.trim(); // 4. Summary const summaryEl = cardEl.querySelector('.entity-result__summary, .search-result__snippets'); if (summaryEl) data.summary = summaryEl.innerText.trim(); // Clean up if (data.name) data.name = data.name.replace(/View .* profile/gi, '').trim(); if (!data.name) { data.name = 'مستثمر محتمل'; } } catch (e) { console.error('[LJA] Extraction failed', e); data.name = 'مستثمر محتمل'; } return data; } // ─── Find Cards Logic ────────────────────────────────────────────────────── function findCards() { let cards = []; let uniqueCards = new Set(); // Method 1: The official LinkedIn search result container class let containerElements = document.querySelectorAll('.reusable-search__result-container, .search-entity, .entity-result__item'); if (containerElements.length > 0) { containerElements.forEach(el => uniqueCards.add(el)); console.log('[LJA] Found cards via container classes:', uniqueCards.size); } // Method 2: Fallback to finding profile links and going up to the list item if (uniqueCards.size === 0) { let profileLinks = Array.from(document.querySelectorAll('a[href*="/in/"]')) .filter(a => a.innerText.trim().length > 2 && !a.querySelector('img')); profileLinks.forEach(link => { // Find the nearest list item or large div container let container = link.closest('li') || link.closest('div.mb1') || link.parentElement.parentElement.parentElement; if (container && container.innerText.length > 20) { uniqueCards.add(container); } }); console.log('[LJA] Found cards via profile links:', uniqueCards.size); } // Convert Set to Array and filter out translation extension dropdowns cards = Array.from(uniqueCards).filter(card => { const txt = card.innerText.toLowerCase(); // Must not be a language selector dropdown if (txt.includes('english (australia)') && txt.length < 50) return false; // Should have some decent amount of text return txt.length > 20; }); return cards; } // ─── Inject UI into Card ───────────────────────────────────────────────── function injectScanButton(cardEl) { if (cardEl.querySelector('.lja-scan-person-btn') || cardEl.querySelector('.lja-investor-result')) return; const btn = document.createElement('button'); btn.className = 'lja-scan-person-btn'; btn.innerHTML = '🔍 Scan Investor'; btn.style.margin = '10px 0'; btn.style.padding = '5px 15px'; btn.style.cursor = 'pointer'; btn.style.backgroundColor = '#6C63FF'; btn.style.color = '#fff'; btn.style.border = 'none'; btn.style.borderRadius = '5px'; btn.style.fontWeight = 'bold'; btn.style.zIndex = '999'; btn.style.position = 'relative'; const resultContainer = document.createElement('div'); resultContainer.className = 'lja-result-wrapper'; resultContainer.style.width = '100%'; resultContainer.style.marginTop = '10px'; btn.addEventListener('click', async (e) => { e.stopPropagation(); e.preventDefault(); await scanPerson(cardEl, btn, resultContainer); }); // Try to append to actions area const actionArea = cardEl.querySelector('.entity-result__actions, .search-result__actions'); if (actionArea) { actionArea.prepend(btn); actionArea.appendChild(resultContainer); } else { // Fallback: Find the name link and put it under it const profileLink = Array.from(cardEl.querySelectorAll('a[href*="/in/"]')).find(a => a.innerText.trim().length > 0); if (profileLink && profileLink.parentElement) { profileLink.parentElement.appendChild(btn); profileLink.parentElement.appendChild(resultContainer); } else { cardEl.appendChild(btn); cardEl.appendChild(resultContainer); } } console.log('[LJA] Injected button for a profile:', extractPersonData(cardEl).name); } // ─── Scan a Single Person ──────────────────────────────────────────────── async function scanPerson(cardEl, btnEl, resultContainer) { const settings = await getSettings(); if (!settings || !settings.apiKey) { alert('Please set your Gemini API key in the extension popup first.'); return; } const data = extractPersonData(cardEl); if (!data.name && !data.headline) { console.error('[LJA] Could not extract details, skipping.'); btnEl.innerHTML = '❌ Extraction Failed'; return; } btnEl.disabled = true; btnEl.innerHTML = ' Scanning...'; const prompt = `أنت مستشار استثماري خبير في الشركات الناشئة (Startups) في الشرق الأوسط ومصر. المستخدم يبحث عن مستثمرين (Angel Investors) أو أشخاص يمكنهم توصيله بمستثمرين لتطبيقاته "انطلق" (Intaleq) و "تريبز" (Tripz) وهي تطبيقات نقل ذكي (Ride-hailing) في مصر والشرق الأوسط. يرجى تقييم هذا الشخص بناءً على المعلومات التالية المستخرجة من لينكد إن: الاسم: ${data.name} المسمى الوظيفي: ${data.headline} الموقع: ${data.location} نبذة: ${data.summary} هل هذا الشخص مناسب للتواصل معه وعرض الاستثمار أو طلب توصية بمستثمرين؟ إذا كان الشخص مستثمرًا فعليًا أو يعمل في صندوق استثماري (VC) أو لديه شبكة علاقات قوية في ريادة الأعمال أو يدعم الشركات الناشئة، أعطه العلامة الخضراء (green). إذا كان الشخص لا يبدو مرتبطًا بالاستثمار (مثلاً مجرد موظف عادي، أو يبحث عن عمل، أو في مجال بعيد جداً عن دعم الشركات)، أعطه العلامة الحمراء (red) لكي لا يضيع المستخدم وقته معه. يجب أن يكون الرد بصيغة JSON فقط بهذا الشكل: { "status": "green" أو "red", "reason": "سبب قصير جداً باللغة العربية يشرح لماذا (سطر واحد)" } لا تقم بإضافة أي نص آخر أو Markdown.`; try { const response = await chrome.runtime.sendMessage({ type: 'GEMINI_REQUEST', payload: { apiKey: settings.apiKey, action: 'generateText', prompt: prompt } }); if (!response.success) { throw new Error(response.error || 'Unknown error'); } let rawText = response.data.text || response.data; rawText = rawText.replace(/```json/gi, '').replace(/```/g, '').trim(); let resultData; try { resultData = JSON.parse(rawText); } catch (parseError) { throw new Error('Failed to parse AI response. Raw: ' + rawText); } const isGreen = resultData.status === 'green'; const badgeIcon = isGreen ? '✅' : '❌'; const badgeText = isGreen ? 'تواصل معه' : 'تجاهله'; const colorClass = isGreen ? 'green' : 'red'; resultContainer.innerHTML = `