Skip to content

Instantly share code, notes, and snippets.

@tackme31
Last active August 22, 2024 14:52
Show Gist options
  • Save tackme31/124a4c3a0c08f971e812a56dbd6dfd82 to your computer and use it in GitHub Desktop.
Save tackme31/124a4c3a0c08f971e812a56dbd6dfd82 to your computer and use it in GitHub Desktop.
Chrome extension: display confirm dialog from background (manifest v3)
const showConfirm = async (tab, message) => {
return new Promise(resolve => {
chrome.tabs.sendMessage(
tab.id,
{ id: "confirm", message },
async (confirmResult) => resolve(confirmResult)
);
});
};
chrome.runtime.onMessage.addListener((msg, _, sendResponse) => {
if (msg.id === "confirm") {
const result = confirm(msg.message);
sendResponse(result);
return;
}
});
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "confirm_test",
title: "Show confirm dialog",
contexts: ["link"],
type: "normal",
});
});
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
const result = await showConfirm(tab, "Are you sure you want to change?")
if (result) {
// yes
} else {
// no
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment