Context: the content script file in my MV3 extension is injected into every page as such via the manifest.json
:
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"js/contentScript.js"
],
"match_origin_as_fallback": true,
"match_about_blank": true,
"all_frames": true,
"run_at": "document_idle"
}
],
When the extension is first installed, I additionally run the following via the background.js to inject the script into existing tabs:
chrome.scripting.executeScript({ files: ['js/contentScript.js',], target: { tabId: 123, allFrames: true }, })
When a user takes some action on the page, like keydown or mouse click, the content script file sends a message using chrome.runtime.sendMessage
, which is received by a background.js
file using chrome.runtime.onMessage.addListener((msg, sender) => { })
.
Problem: I have noticed the sender.tab
value is often undefined for many of my extension users. When I try to access its id, I get an error: “Cannot read properties of undefined (reading ‘id’)”. I have been unable to reproduce this issue on my own. I have checked that this specific message is only sent by the content script (not by the offscreen document, for example)
Question: In what particular cases will the sender.tab
value be undefined when sending this message from the content script?
2