Get clicked element under contextMenus

Hi guys,

I’m trying to build my first extension and would like to get help on how to get the DOM element we clicked before the context menu show up, SPAN, DIV, …
I have in manifest.json:

     "permissions": [
        "contextMenus",
        "activeTab"
    ]

And do not anything to exploit inside (background.js)

browser.contextMenus.onClicked.addListener(async (info, tab)

This may be tricky…
There is this special API for getting clicked node, but it’s not called before the menu is shown:

If you would like to react to right-click BEFORE the menu is displayed, you need to inject content script into the page and have event handler on one of the mouse events.
Maybe mouseup or contextmenu. But if you intend to update context menu, you may end up with race conditions.
But to inject content script into page, you need host permissions, which you don’t have (unless user click toolbar icon which would activate “activeTab” permission).

In my case browser.menus is undefined i use only browser.contextMenus.onClicked.addListener…
Anyway that link give a good example.
Problem solved. Thanks!

1 Like