I’ve been fighting this for a couple of days now.
Basically I am on a website, and there is a text field in my extension which then opens a new tab and goes to the same website but with specific search terms. Then it needs to scrape that new tab, but instead it scrapes the original tab I was on. It correctly does the scrolling part of the script on the new tab, but the scraping itself is done on the original tab.
I have the following files:
background.js
content.js
manifest.json
popup.js
popup.html
I am not sure what exactly is the issue.
But here’s my background.js:
//open a new browser window and navigate to the LinkedIn search URL
chrome.action.onClicked.addListener((tab) => {
// Open a new window
chrome.windows.create(
{
url: "LINK",
type: "popup",
state: "maximized",
},
(newWindow) => {
// Inject content script when the tab is fully loaded
chrome.tabs.onUpdated.addListener(function listener(tabId, info) {
if (info.status === "complete" && newWindow.tabs[0].id === tabId) {
chrome.scripting.executeScript({
target: { tabId: newWindow.tabs[0].id },
files: ["content.js"],
});
// Remove the listener after injecting the script
chrome.tabs.onUpdated.removeListener(listener);
}
});
}
);
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "processPageData") {
// Process the HTML content received from content script
const pageHTML = request.pageHTML;
// Send the page HTML to content script for processing
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, {
action: "processPageData",
pageHTML,
});
});
}
});
And this is my popup.js:
document.addEventListener("DOMContentLoaded", function () {
// Function to send LinkedIn search data to content.js
document
.getElementById("getDataButton")
.addEventListener("click", function () {
const searchKeywords = document.getElementById("searchField").value;
const maxScrolls = document.getElementById("maxScrollField").value;
if (!searchKeywords) {
alert("Please enter search keywords.");
return;
}
// Format the search keywords
const formattedKeywords = searchKeywords.trim().replace(/s+/g, "%20");
const searchUrl = `LINK`;
// Send message to content.js in the currently active tab
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
action: "getLinkedInData",
searchUrl: searchUrl,
maxScrolls: parseInt(maxScrolls, 10) || 5, // Default to 5 if not a number
});
});
});
});
I suspect that the issue is here somewhere, but in case the content.js is needed as well it can be found here – https://pastebin.com/WzFx7GFq
