My goal is to inject frontend code in a tab if its url matches my url. For that I did:
background.js:
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo?.url?.includes("https://www.google.com/search?")) {
chrome.scripting.executeScript(
{
target: { tabId: tabId },
files: ['injection.js'],
}
);
}
})
It works but many people also do through content.js:
background.js:
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo?.url?.includes("https://www.google.com/search?")) {
await chrome.tabs.sendMessage(tabId, { isActive: true });
}
})
now we can add a listener in content.js.
My question is what are the difference? Which approach is better?