I’m trying to write a Chrome Extension that does these basic tasks:
- Gets information from a user via a popup (done)
- Relays that information to background.js (done)
- Background.js processes that data and sends out chunks to one tab and waits for the extension to do some interaction on that tab
- Once the first tab is done being interacted with, switch to another tab and do something else.
- Once the second tab is finished doing its thing, return to step 3 until all chunks are done.
I feel like I’m close, but am struggling with the waiting part. It interacts with the first page as expected, but doesn’t wait for that to finish and just immediately switches to the first page.
Here’s my code thus far:
popup.js
document.getElementById("exportButton").addEventListener("click", function() {
usnArr = document.getElementById('w3review').value.split(",");
chrome.runtime.sendMessage({ data: usnArr},
function (response) {
window.close();
}
);
});
background.js
chrome.runtime.onMessage.addListener((message, sender, reply) => {
const messageFromPopUp = message.data;
sendMessages(messageFromPopUp);
reply({ result: true })
return true;
});
async function sendMessages(messageFromPopUp){
chrome.tabs.query({currentWindow: true}, async function(tabs) {
const currentTab = tabs.find(tab => tab.active);
const currentIndex = tabs.indexOf(currentTab);
constcurrentID = tabs[currentIndex].id;
const binTab = tabs.filter(str => str.title.includes("Bin:"));
const binTabID = binTab[0].id;
const response = await chrome.tabs.sendMessage(currentID, {usns: messageFromPopUp[0]})
if (response.farewell === 'goodbye') {
chrome.tabs.update(binTabID, {active: true});
}
});
content.js
async function logMessage(message) {
completed = await exportValidations(message);
return completed;
}
chrome.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
waitForReponse = logMessage(request.usns);
sendResponse({ farewell: "goodbye" });
}
);
async function exportValidations(usnArr){
//Do some long running interaction with this page
}
Any ideas where to go from here? Or maybe I’m just thinking about doing this the wrong way. Any help would be greatly appreciated.