I need to get tab capture in content script. So, I used messages to transfer information.
I have:
manifest.json
{
"manifest_version": 3,
"name": "Color picker",
"description": "Color picker",
"version": "1.0.0",
"icons": {"128": "icon_128.png"},
"permissions": ["activeTab", "tabs", "scripting"],
"host_permissions": ["<all_urls>"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
"background": {
"service_worker": "background.js"
},
"action": {"default_popup": "popup.html"}
}
content.js (content script)
function prepare() {
chrome.runtime.sendMessage({ to: "background", event: "getScreen" }, (response) => {
...
});
}
background.js
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
if (request.to != "background" && request.to != "all") return;
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if (request.event == "getScreen") {
chrome.tabs.captureVisibleTab(null, { format: "png" }, (data) => {
sendResponse(data);
});
}
});
return true;
});
and I am getting this error
Unchecked runtime.lastError: The message port closed before a response was received.