203 lines
6.3 KiB
JavaScript
203 lines
6.3 KiB
JavaScript
// content.js - Handles job extraction and login automation for app.andela.com
|
|
|
|
console.log('[Andela Content Script] Loaded on', window.location.href);
|
|
|
|
function isLoginPage() {
|
|
var emailInput = document.querySelector('input[type="email"]#email');
|
|
var emailLabel = document.querySelector('label[for="email"]');
|
|
var hasLoginForm = !!(emailInput && emailLabel);
|
|
var isLoginUrl = window.location.pathname.indexOf('/login') === 0;
|
|
|
|
console.log('[Andela Content Script] Login check - hasForm:', hasLoginForm, 'isLoginUrl:', isLoginUrl);
|
|
return hasLoginForm || (isLoginUrl && !!emailInput);
|
|
}
|
|
|
|
function enterEmailAndClickLogin(email) {
|
|
try {
|
|
console.log('[Andela Content Script] Attempting to enter email:', email);
|
|
|
|
var emailInput = document.querySelector('input[type="email"]#email');
|
|
if (!emailInput) {
|
|
console.log('[Andela Content Script] Email input not found');
|
|
return false;
|
|
}
|
|
|
|
emailInput.focus();
|
|
emailInput.value = email;
|
|
emailInput.dispatchEvent(new Event('input', { bubbles: true }));
|
|
emailInput.dispatchEvent(new Event('change', { bubbles: true }));
|
|
emailInput.dispatchEvent(new KeyboardEvent('keyup', { bubbles: true }));
|
|
|
|
console.log('[Andela Content Script] Email entered, looking for submit button...');
|
|
|
|
setTimeout(function() {
|
|
var submitButton = document.querySelector('button[type="submit"]');
|
|
|
|
if (!submitButton) {
|
|
var buttons = document.querySelectorAll('button');
|
|
for (var i = 0; i < buttons.length; i++) {
|
|
var btnText = buttons[i].textContent.toLowerCase();
|
|
if (btnText.indexOf('login') !== -1 ||
|
|
btnText.indexOf('sign in') !== -1 ||
|
|
btnText.indexOf('continue') !== -1 ||
|
|
btnText.indexOf('submit') !== -1) {
|
|
submitButton = buttons[i];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (submitButton) {
|
|
console.log('[Andela Content Script] Clicking submit button:', submitButton.textContent);
|
|
submitButton.click();
|
|
} else {
|
|
console.log('[Andela Content Script] Submit button not found, trying form submit');
|
|
var form = emailInput.closest('form');
|
|
if (form) {
|
|
if (form.requestSubmit) form.requestSubmit();
|
|
else form.submit();
|
|
}
|
|
}
|
|
}, 500);
|
|
|
|
return true;
|
|
} catch (err) {
|
|
console.error('[Andela Content Script] Error entering email:', err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function expandAllSeeMoreButtons(maxRounds) {
|
|
maxRounds = maxRounds || 25;
|
|
return new Promise(function(resolve) {
|
|
var rounds = 0;
|
|
|
|
function findSeeMoreButton() {
|
|
var buttons = document.querySelectorAll('button');
|
|
for (var i = 0; i < buttons.length; i++) {
|
|
var text = (buttons[i].textContent || '').trim().toLowerCase();
|
|
if (text.indexOf('see') === 0 && text.indexOf('more') !== -1) {
|
|
return buttons[i];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function clickNext() {
|
|
rounds++;
|
|
var btn = findSeeMoreButton();
|
|
if (!btn || rounds > maxRounds) {
|
|
console.log('[Andela Content Script] Done expanding "See more" buttons after', rounds - 1, 'click(s)');
|
|
resolve();
|
|
return;
|
|
}
|
|
console.log('[Andela Content Script] Clicking:', btn.textContent.trim());
|
|
btn.click();
|
|
setTimeout(clickNext, 700);
|
|
}
|
|
|
|
clickNext();
|
|
});
|
|
}
|
|
|
|
function extractJobFromLink(link) {
|
|
var href = link.getAttribute('href') || '';
|
|
var idMatch = href.match(/\/job-postings\/(\d+)/);
|
|
var id = idMatch ? idMatch[1] : href;
|
|
|
|
var card = link.closest('div.rounded-lg.border.border-neutral-200.bg-white') || link.parentElement;
|
|
|
|
var title = '';
|
|
var company = '';
|
|
var titleEl = card ? card.querySelector('.text-primary-900.font-semibold') : null;
|
|
|
|
if (titleEl) {
|
|
var clone = titleEl.cloneNode(true);
|
|
var companySpan = clone.querySelector('span.font-normal');
|
|
if (companySpan) {
|
|
company = companySpan.textContent.replace(/^[\s—-]+/, '').trim();
|
|
companySpan.remove();
|
|
}
|
|
title = clone.textContent.trim();
|
|
}
|
|
|
|
var paragraphs = card ? card.querySelectorAll('p') : [];
|
|
var startInfo = paragraphs[0] ? paragraphs[0].textContent.trim() : '';
|
|
var locationInfo = paragraphs[1] ? paragraphs[1].textContent.trim() : '';
|
|
|
|
var skillEls = card ? card.querySelectorAll('.flex.flex-wrap.gap-2 span') : [];
|
|
var skills = Array.prototype.map.call(skillEls, function(s) { return s.textContent.trim(); });
|
|
|
|
var url = href;
|
|
if (url && url.indexOf('http') !== 0) {
|
|
url = window.location.origin + url;
|
|
}
|
|
|
|
return {
|
|
id: id,
|
|
title: title,
|
|
company: company || 'Company',
|
|
posted_date: startInfo || '',
|
|
location: locationInfo || '',
|
|
skills: skills,
|
|
url: url
|
|
};
|
|
}
|
|
|
|
function extractJobsFromPage() {
|
|
console.log('[Andela Content Script] Extracting jobs...');
|
|
var jobs = [];
|
|
var seen = {};
|
|
var links = document.querySelectorAll('a[href*="/job-postings/"]');
|
|
console.log('[Andela Content Script] Found ' + links.length + ' job-posting links');
|
|
|
|
links.forEach(function(link) {
|
|
try {
|
|
var job = extractJobFromLink(link);
|
|
if (!job.id || seen[job.id]) return;
|
|
seen[job.id] = true;
|
|
if (job.title) {
|
|
jobs.push(job);
|
|
console.log('[Andela Content Script] Extracted job:', job.title, '(' + job.id + ')');
|
|
}
|
|
} catch (err) {
|
|
console.error('[Andela Content Script] Error extracting a job card:', err);
|
|
}
|
|
});
|
|
|
|
console.log('[Andela Content Script] Extracted ' + jobs.length + ' jobs total');
|
|
return jobs;
|
|
}
|
|
|
|
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
|
|
console.log('[Andela Content Script] Received message:', request);
|
|
|
|
if (request.action === 'extractJobs') {
|
|
expandAllSeeMoreButtons().then(function() {
|
|
setTimeout(function() {
|
|
try {
|
|
var jobs = extractJobsFromPage();
|
|
sendResponse({ jobs: jobs });
|
|
} catch (error) {
|
|
console.error('[Andela Content Script] Error:', error);
|
|
sendResponse({ jobs: [] });
|
|
}
|
|
}, 500);
|
|
});
|
|
return true;
|
|
}
|
|
|
|
if (request.action === 'checkLoginPage') {
|
|
var result = isLoginPage();
|
|
console.log('[Andela Content Script] Is login page:', result);
|
|
sendResponse({ isLoginPage: result });
|
|
}
|
|
|
|
if (request.action === 'enterEmailAndLogin') {
|
|
var success = enterEmailAndClickLogin(request.email);
|
|
sendResponse({ success: success });
|
|
}
|
|
|
|
return true;
|
|
});
|