Behavior change of chrome.contextMenus.create when service work / event page start

512 views
Skip to first unread message

Jackie Han

unread,
Dec 1, 2022, 7:39:32 AM12/1/22
to Chromium Extensions
Last year, I clearly remember(because I only call contextMenus.create in one place) that the following code will throw "runtime.lastError: Cannot create item with duplicate id xx" if you click the "reload" button in chrome://extensions/. 

// in service worker or event page
function createMenus() {
  console.log('create menu');
  const pageContext = {
    id: "menu-id",
    title: "menu-title",
    contexts: ["page"]
  };
  chrome.contextMenus.create(pageContext);
}
chrome.runtime.onInstalled.addListener(createMenus);


But now, it doesn't throw this runtime error when service worker start, and the context menu is updated (for example, you update the menu title to "menu-title-2". You can even put "contextMenus.create" at top-level, no "duplicate id" error.

This exemption is only valid when service worker start (first event loop). If you click "Inspect views
service worker", and call createMenus() again or run it at any other extension pages, you will still meet the "duplicate id" error.

I think the internal implementation may have changed, or I remember incorrectly.

Jackie Han
Reply all
Reply to author
Forward
0 new messages