0

I'm building a Chrome extension with the Plasmo and Supabase, where I fetch data from whenever the browser's context menu is opened, using a broadcasted contextmenu event. I aim to dynamically update context menu items (child elements) with the fetched data.

The problem is that on the first right-click, the context menu shows the correct number of child items, but their titles reflect outdated data (e.g., from a previous fetch or initial state). Only on the second context menu open do the titles update correctly with the latest data.

How can I ensure that the context menu items are updated with fresh data immediately on the first open? I was trying different approaches without any luck so far.

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.type === 'refresh-context-menu') {
    chrome.contextMenus.removeAll(() => {
    fetchUserLinks().then(links => {
        chrome.contextMenus.create({
          id: "context-menu",
          title: "ExtensionName",
          contexts: ["editable"]
        }, () => {
          chrome.contextMenus.create({
            id: "my-links",
            parentId: "context-menu",
            title: "My Links",
            contexts: ["editable"]
          }, () => {
            links.forEach((link, index) => {
              chrome.contextMenus.create({
                id: `link-${index}`,
                parentId: "my-links",
                title: link.short_name,
                contexts: ["editable"]
              })
            })
          })
        })
      })
    })
  }
})
2
  • 1) You can use mousedown event instead of contextmenu, but it won't help you if fetchUserLinks() takes a long time. 2) Instead of remove+create use update(). Create the items just once in chrome.runtime.onInstalled event. Commented May 3 at 6:12
  • @woxxom update is tricky, cuz it requires keep in mind proper indexes, whereas my newly updated list will be different. Why mousedown? This will not create chrome context menu entry. Commented May 30 at 12:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.