manifest.json:
{
"manifest_version": 3,
"name": "Test",
"version": "1.0",
"description": "test",
"permissions": ["tabs", "storage", "contextMenus"],
"icons": {
"16": "icons/small.png",
"48": "icons/medium.png",
"128": "icons/large.png"
},
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"js": ["scripts/content.js"],
"matches": ["https://*/*"]
}
]
}
file structure:
background.js
chrome.runtime.onInstalled.addListener(
() => {
chrome.contextMenus.create({
id: "999",
title: "test",
contexts: ["all"], // ContextType
})
})
chrome.contextMenus.onClicked.addListener(async () => {
const [tab] = await chrome.tabs.query({
active: true,
lastFocusedWindow: true,
})
await chrome.tabs.sendMessage(tab.id, { greeting: "hello" })
})
content.js
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log({ request, sender, sendResponse })
})
The example code here also throws the same error:
https://developer.chrome.com/docs/extensions/mv3/messaging/
and this is a message in the background console: