Create confirmation as CS after clicking on context menu

I created a context menu that save an image after clicking on the menu item. I would like to create a nice confirmation as CS. What would be the best way to send a message from the BGSW to CS? Here the code I have in BGSW in background/index.ts:
chrome.contextMenus.create({
id: "kapture",
title: "Kapture",
contexts: ["image"]
});

chrome.contextMenus.onClicked.addListener(async function (info) {
const { pageUrl, srcUrl } = info;

if (!srcUrl || !pageUrl) return;

const response = await fetch("https://kapture.so/api/save-image", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
srcUrl,
pageUrl
})
});

const result = await response.json();
console.log(result);
});
chrome.contextMenus.create({
id: "kapture",
title: "Kapture",
contexts: ["image"]
});

chrome.contextMenus.onClicked.addListener(async function (info) {
const { pageUrl, srcUrl } = info;

if (!srcUrl || !pageUrl) return;

const response = await fetch("https://kapture.so/api/save-image", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
srcUrl,
pageUrl
})
});

const result = await response.json();
console.log(result);
});
And just to confirm, I would like a message to send some data from the context menu listener to CS as well as a message so I can open the confirmation modal. Should I use Porta for that? And how to send data from my contextMenu listener to CS?
1 Reply
Jingles
Jingles•16mo ago
can try window.postMessage() at BG and receive it with window.addEventListener("message", (event) => {})
Want results from more Discord servers?
Add your server