I’m trying to implement a function that users can use to take screenshots using browser.tabs.captureVisibleTab
.
But browser.runtime.sendMessage
immediately returns undefined.
In console.log("dataUrl ::: ", dataUrl)
of browser.runtime.onMessage.addListener
it logged data url, so it’s working. But somehow, it’s undefined in screenshot.addEventListener
My current code is as below: It works fine in Chrome (I changed browsers with Chrome, of course).
I appreciate any advice.
// manifest.json
"permissions": ["activeTab", "tabs", "storage", "webRequest", "<all_urls>"],
"host_permissions": ["<all_urls>"]
// popup.js
screenshot.addEventListener("click", async () => {
try {
const response = await browser.runtime.sendMessage({
type: "takeScreenshot",
});
console.log("response is ", response); // undefined
if (response.dataUrl) {
const img = document.createElement("img");
img.src = response.dataUrl;
document.body.appendChild(img);
}
} catch (error) {
console.error("Error sending message:", error);
}
});
// background.js
browser.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
if (message.type === "takeScreenshot") {
const option = { active: true, currentWindow: true };
await browser.tabs.query(option, async (tabs) => {
const activeTab = tabs[0];
if (!activeTab) {
await sendResponse({ error: "No active tab." });
return;
}
await browser.tabs.captureVisibleTab(
activeTab.windowId,
{ format: "png" },
async (dataUrl) => {
if (browser.runtime.lastError) {
await sendResponse({ error: browser.runtime.lastError.message });
return;
}
console.log("dataUrl ::: ", dataUrl); //it logs correct dataUrl
await sendResponse({ dataUrl: dataUrl });
}
);
});
return true;
}
});
I enabled the “<all_urls>” permission in the Add-ons Manager according to this . But it still returns the same result.