Last active
          August 22, 2024 14:52 
        
      - 
      
 - 
        
Save tackme31/124a4c3a0c08f971e812a56dbd6dfd82 to your computer and use it in GitHub Desktop.  
    Chrome extension: display confirm dialog from background (manifest v3)
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const showConfirm = async (tab, message) => { | |
| return new Promise(resolve => { | |
| chrome.tabs.sendMessage( | |
| tab.id, | |
| { id: "confirm", message }, | |
| async (confirmResult) => resolve(confirmResult) | |
| ); | |
| }); | |
| }; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | chrome.runtime.onMessage.addListener((msg, _, sendResponse) => { | |
| if (msg.id === "confirm") { | |
| const result = confirm(msg.message); | |
| sendResponse(result); | |
| return; | |
| } | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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