// LinkedIn Profile Importer for ATS // This script extracts candidate information from LinkedIn profiles  (function() {     'use strict';          // Configuration - Update this URL to match your setup     const ATS_BASE_URL = window.location.hostname === 'localhost'          ? 'http://localhost:8888/ats/public/'          : window.location.origin  '/ats/public/';          // Check if we're on a LinkedIn profile page     if (!window.location.href.includes('linkedin.com/in/')) {         alert('Please navigate to a LinkedIn profile page first!');         return;     }          // Show loading indicator     const showLoading = () => {         const loadingDiv = document.createElement('div');         loadingDiv.id = 'ats-loading';         loadingDiv.style.cssText = `             position: fixed;             top: 50%;             left: 50%;             transform: translate(-50%, -50%);             background: white;             padding: 20px 30px;             border-radius: 10px;             box-shadow: 0 10px 30px rgba(0,0,0,0.2);             z-index: 99999;             font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;             text-align: center;         `;         loadingDiv.innerHTML = `             <div style="font-size: 18px; font-weight: 600; color: #333; margin-bottom: 10px;">                 Extracting LinkedIn Profile...             </div>             <div style="font-size: 14px; color: #666;">                 Please wait while we gather the data             </div>             <div style="margin-top: 15px;">                 <div style="width: 200px; height: 4px; background: #e0e0e0; border-radius: 2px; overflow: hidden;">                     <div style="width: 50%; height: 100%; background: #4CAF50; animation: progress 2s infinite;"></div>                 </div>             </div>         `;                  const style = document.createElement('style');         style.innerHTML = `             @keyframes progress {                 0% { width: 0%; }                 50% { width: 100%; }                 100% { width: 0%; }             }         `;         document.head.appendChild(style);         document.body.appendChild(loadingDiv);                  return loadingDiv;     };          // Remove loading indicator     const removeLoading = (loadingDiv) => {         if (loadingDiv && loadingDiv.parentNode) {             loadingDiv.parentNode.removeChild(loadingDiv);         }     };          // Helper function to extract text safely     const extractText = (selector, parent = document) => {         try {             const element = parent.querySelector(selector);             return element ? element.textContent.trim() : '';         } catch (e) {             return '';         }     };          // Helper function to extract all text from multiple elements     const extractAllText = (selector, parent = document) => {         try {             const elements = parent.querySelectorAll(selector);             return Array.from(elements).map(el => el.textContent.trim());         } catch (e) {             return [];         }     };          // Main extraction function     const extractProfileData = () => {         const loading = showLoading();                  // Wait a bit for dynamic content to load         setTimeout(() => {             const profileData = {                 source: 'linkedin',                 linkedin_url: window.location.href.split('?')[0],                 timestamp: new Date().toISOString()             };                          try {                 // Extract name - try multiple selectors                 profileData.full_name =                      extractText('h1.text-heading-xlarge') ||                     extractText('h1[class*="inline"]') ||                     extractText('.pv-top-card--list li:first-child') ||                     extractText('h1');                                  if (profileData.full_name) {                     const nameParts = profileData.full_name.split(' ');                     profileData.first_name = nameParts[0] || '';                     profileData.last_name = nameParts.slice(1).join(' ') || '';                 }                                  // Extract headline/title                 profileData.current_title =                      extractText('.text-body-medium.break-words') ||                     extractText('[data-generated-suggestion-target]') ||                     extractText('.pv-top-card-section__headline') ||                     extractText('h2[class*="mt1"]');                                  // Extract location                 profileData.location =                      extractText('.text-body-small.inline.t-black--light.break-words') ||                     extractText('[class*="pv-top-card--list-bullet"]') ||                     extractText('.pv-top-card__location') ||                     extractText('span[class*="text-body-small"][class*="inline"]');                                  // Extract about section                 const aboutSection = document.querySelector('section[data-section="summary"]') ||                                     document.querySelector('#about')?.closest('section') ||                                    document.querySelector('[class*="pv-about-section"]');                 if (aboutSection) {                     profileData.about =                          extractText('.inline-show-more-text', aboutSection) ||                         extractText('[class*="pv-about__summary-text"]', aboutSection) ||                         extractText('.pv-about-section div', aboutSection);                 }                                  // Extract experience                 const experienceSection = document.querySelector('#experience')?.closest('section') ||                                         document.querySelector('section[data-section="experience"]') ||                                         document.querySelector('[class*="experience-section"]');                 if (experienceSection) {                     const experiences = [];                     const expItems = experienceSection.querySelectorAll('li[class*="pv-profile-section__list-item"]') ||                                    experienceSection.querySelectorAll('[class*="pvs-entity"]') ||                                    experienceSection.querySelectorAll('.pv-entity__position-group-pager');                                          expItems.forEach(item => {                         const exp = {                             title: extractText('[data-field="title"]', item) ||                                    extractText('h3', item) ||                                   extractText('[class*="t-bold"] span[aria-hidden="true"]', item),                             company: extractText('[class*="pv-entity__secondary-title"]', item) ||                                     extractText('[class*="t-14 t-normal"] span[aria-hidden="true"]', item) ||                                     extractText('p[class*="pv-entity__secondary-title"]', item),                             duration: extractText('[class*="pv-entity__date-range"] span:nth-child(2)', item) ||                                      extractText('[class*="pvs-entity__caption-wrapper"]', item),                             description: extractText('[class*="pv-entity__description"]', item) ||                                         extractText('[class*="inline-show-more-text"]', item)                         };                         if (exp.title || exp.company) {                             experiences.push(exp);                         }                     });                     profileData.experience = experiences;                 }                                  // Extract education                 const educationSection = document.querySelector('#education')?.closest('section') ||                                        document.querySelector('section[data-section="education"]');                 if (educationSection) {                     const educations = [];                     const eduItems = educationSection.querySelectorAll('[class*="pvs-entity"]') ||                                    educationSection.querySelectorAll('li[class*="pv-profile-section__list-item"]');                                          eduItems.forEach(item => {                         const edu = {                             school: extractText('[class*="pv-entity__school-name"]', item) ||                                    extractText('h3 span[aria-hidden="true"]', item),                             degree: extractText('[class*="pv-entity__degree-name"]', item) ||                                    extractText('[class*="t-14"] span[aria-hidden="true"]', item),                             duration: extractText('[class*="pv-entity__dates"]', item) ||                                      extractText('[class*="pvs-entity__caption-wrapper"]', item)                         };                         if (edu.school || edu.degree) {                             educations.push(edu);                         }                     });                     profileData.education = educations;                 }                                  // Extract skills                 const skillsSection = document.querySelector('#skills')?.closest('section') ||                                     document.querySelector('section[data-section="skills"]');                 if (skillsSection) {                     const skills = extractAllText('[class*="pv-skill-entity__skill-name"]', skillsSection)                         .concat(extractAllText('[class*="t-bold"] span[aria-hidden="true"]', skillsSection))                         .filter((skill, index, self) => skill && skill.length > 1 && skill.length < 50 && self.indexOf(skill) === index);                     profileData.skills = skills;                 }                                  // Extract profile picture                 const profilePic = document.querySelector('img[class*="pv-top-card-profile-picture__image"]') ||                                  document.querySelector('img[class*="profile-photo-edit__preview"]') ||                                  document.querySelector('.pv-top-card__photo img') ||                                  document.querySelector('img.ember-view[alt*="'  (profileData.first_name || '')  '"]');                 if (profilePic && profilePic.src) {                     profileData.profile_picture_url = profilePic.src;                 }                                  // Set current company from most recent experience                 if (profileData.experience && profileData.experience.length > 0) {                     const currentExp = profileData.experience[0];                     if (currentExp.duration &&                          (currentExp.duration.toLowerCase().includes('present') ||                           currentExp.duration.toLowerCase().includes('now') ||                          currentExp.duration.toLowerCase().includes('current'))) {                         profileData.current_company = currentExp.company;                     }                 }                              } catch (error) {                 console.error('Error extracting LinkedIn data:', error);                 removeLoading(loading);                 alert('Error extracting profile data. Please try again or report this issue.');                 return;             }                          // Remove loading and send data             removeLoading(loading);             sendToATS(profileData);                      }, 1500); // Wait 1.5 seconds for content to load     };          // Function to send data to ATS     const sendToATS = (profileData) => {         console.log('Sending profile data to ATS:', profileData);                  // Create form and submit         const form = document.createElement('form');         form.method = 'POST';         form.action = ATS_BASE_URL  'import-linkedin-candidate.php';         form.target = '_blank';         form.style.display = 'none';                  // Add data as form fields         Object.keys(profileData).forEach(key => {             const input = document.createElement('input');             input.type = 'hidden';             input.name = key;                          if (typeof profileData[key] === 'object') {                 input.value = JSON.stringify(profileData[key]);             } else {                 input.value = profileData[key] || '';             }                          form.appendChild(input);         });                  document.body.appendChild(form);         form.submit();                  // Clean up         setTimeout(() => {             document.body.removeChild(form);         }, 100);                  // Show success notification         const notification = document.createElement('div');         notification.style.cssText = `             position: fixed;             top: 20px;             right: 20px;             background: #4CAF50;             color: white;             padding: 15px 20px;             border-radius: 8px;             z-index: 10000;             font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;             box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);             display: flex;             align-items: center;             gap: 10px;             animation: slideIn 0.3s ease-out;         `;         notification.innerHTML = `             <svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">                 <path d="M10 0C4.48 0 0 4.48 0 10C0 15.52 4.48 20 10 20C15.52 20 20 15.52 20 10C20 4.48 15.52 0 10 0ZM8 15L3 10L4.41 8.59L8 12.17L15.59 4.58L17 6L8 15Z" fill="white"/>             </svg>             <span>Profile extracted! Check the new tab to complete the import.</span>         `;                  const style = document.createElement('style');         style.innerHTML = `             @keyframes slideIn {                 from { transform: translateX(100%); opacity: 0; }                 to { transform: translateX(0); opacity: 1; }             }         `;         document.head.appendChild(style);         document.body.appendChild(notification);                  setTimeout(() => {             notification.style.animation = 'slideOut 0.3s ease-in';             notification.style.animationFillMode = 'forwards';             setTimeout(() => {                 document.body.removeChild(notification);             }, 300);         }, 5000);     };          