I am new to building chrome extensions, this is the first one I am trying. I am trying to build a simple extension that can help a user in certain job sites filter jobs using certain keywords without going through the ‘unnecessary’ listings.
I have seen similar questions here but all the answers, none of them solve my problem. I am trying to get the active tab url so I can check whether it matches the sites I am trying to get jobs from. Here is an example:
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'filter_jobs') {
// check the tab url to ensure the script should be injected
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
if (tabs.length > 0) {
const activeTab = tabs[0];
const url = activeTab.url;
console.log(url);
if (url.includes('linkedin.com') || url.includes('upwork.com') || url.includes('glassdoor.com'))
{
// Relay the message to the content script of the active tab
chrome.tabs.sendMessage(activeTab.id, message, (response) => {
sendResponse(response); // Relay the response back to the popup
});
}
else
{
// Notify the popup that the site is not supported
sendResponse({
error: 'This site is not supported. Please navigate to LinkedIn, Upwork, or Glassdoor.',
});
}
};
return true; // Keeps the message channel open for asynchronous response
})
}
When I run the above code with the debugger open, I keep getting the activeTab as ‘undefined.’I have “activeTab” as a permission in my manifest.json. Basically, I have a search button in my popup, where I type a keyword that should be sent to my background script shown below and in return the background script should communicate to the content script to get the jobs from the site. Here is how my content-script.js file looks like.
if (message.action === 'filter_jobs') {
const keyword = message.keyword.toLowerCase();
// Scrape job data
const jobs = Array.from(document.querySelectorAll('.job-listing')).map(job => ({
title: job.querySelector('.job-title')?.innerText || '',
company: job.querySelector('.company-name')?.innerText || '',
link: job.querySelector('a')?.href || ''
}));
// Filter jobs
const filteredJobs = jobs.filter(job => job.title.toLowerCase().includes(keyword));
// Respond with filtered jobs
sendResponse(filteredJobs);
}
});
Any help with this is much appreciated :)

