Have a Firefox Extension Pop-up i'm testing that i'm having trouble getting to connected properly to background.js script, content script sends a message and get's this error message in return
Error sending message to background script: Error: Could not establish connection. Receiving end does not exist.
Here is the content script code
const pasteBtn = document.getElementById('pasteBtn');
pasteBtn.addEventListener('click', () => {
// Send a message to the background script to request the address object
browser.runtime.sendMessage({ action: "getAddressObject" })
.then(() => console.log('grabbing addressObj'))
.catch(error => {
console.error('Error sending message to background script:', error);
});
});
and here is the code in the background.js script that it's trying to send a message to, the code logs properly upon popup open, but seems to not be properly listening for message when it's sent
console.log('background.js is running') // for debug purposes to check script loaded
// Listen for messages from content scripts
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "getAddressObject") {
console.log('successfully connected to background.js')
// Send message to the active tab
browser.tabs.query({ active: true, currentWindow: true })
.then(tabs => {
browser.tabs.sendMessage(tabs[0].id, { action: "sendAddressObject", addressObj });
})
.catch(error => {
console.error('Error querying tabs:', error);
});
}
});
tried making message send an async function to await listen, checked manifest.json and it seems to be setup properly and listening for scripts upon extension open