I’m trying to send the “executeAction” message from my background script to my content script that is supposed to listen for it.
I have put console.log messages everywhere to see what was causing the problem:
(background.js)
function triggerAction(newTab) {
console.log('Triggering action number: ', actionStep);
chrome.scripting.executeScript({
target: { tabId: newTab.id },
files: ['content.js']
}, () => {
console.log('Content script injected');
chrome.runtime.sendMessage(newTab.id, { action: 'executeAction', actionStep: actionStep });
console.log('Message sent to content script');
});
}
In my console, the following message doesn’t appear : ‘Message sent to content script’.
Here is my listener on my content.js:
(content.js)
document.addEventListener('DOMContentLoaded', () => {
chrome.runtime.onMessage.addListener((message, _sender, _sendResponse) => {
console.log('Received message');
if (message.action === 'executeAction') {
console.log('Received message to execute action: ', message.actionStep);
executeAction(message.actionStep);
}
});
});
I tried to put an executeScript
to make sure that content.js was active when sending the message.
Anatole Martin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.