Search code examples
google-chrome-extensionruntime-errorcontextmenu

cannot create item with duplicate context menu id in extension


I am creating a new extension. And I add a context menu option via an extension in webpages. But chrome developer mode throws an error, that is 'unchecked.runtime.lastError: cannot create an item with duplicate id my id '. but I gave that in unique id. how to fix that.? this is my context creation method.

chrome.contextMenus.create({
    id: "zm_mark_down_preview_beta",
    title: 'preview and edit',
    contexts: ["editable"]
});


Solution

  • In Chrome, you should create the context menu just once after install/update.

    Use onInstalled event:

    chrome.runtime.onInstalled.addListener(() => {
      chrome.contextMenus.create({
        id: "zm_mark_down_preview_beta",
        title: 'preview and edit',
        contexts: ["editable"]
      });
    });
    

    Alternatively, you can simply suppress the error by accessing lastError in the callback:

    chrome.contextMenus.create({
      id: "zm_mark_down_preview_beta",
      title: 'preview and edit',
      contexts: ["editable"]
    }, () => chrome.runtime.lastError);