ContextMenus: getTargetElement and Canvas ContextType

132 views
Skip to first unread message

al

unread,
Oct 29, 2024, 8:43:49 PMOct 29
to Chromium Extensions
Hey there,

I apologize as these have been indirectly asked before (but have gone unanswered).

I was hoping to get some definitive info (or indicators) on the following, relating to the ContextMenus implementation:
  • Will Chrome implement [context]menus.getElementTarget() akin to Firefox's implementation or similar?
  • Will there be a ContextType for canvas elements?
In my case, for the former, I currently implement a content script contextMenu listener as a workaround - 
let target;
function contextMenuListener(evt) {
  target = evt.target
}
Which requires injection on every page. 

For the latter, I have to use the "all" ContextType, which unfortunately adds the menu item to places it isn't needed (e.g. [popup] action), which can cause confusion. 

I believe these are the best solutions (at least since I last checked) for the listed questions, but they're a bit inconvenient. 
(Especially as I look toward dynamic script injection, based on user action, which a lack of TargetElement prevents)

N.B. Totally understand that these aren't mission critical, so just generally asking. 

Any info would be appreciated!

woxxom

unread,
Oct 30, 2024, 3:34:20 AMOct 30
to Chromium Extensions, al
Re getElementTarget, try checking document.activeElement afterwards instead, see this example.

al

unread,
Oct 30, 2024, 5:44:41 AMOct 30
to Chromium Extensions, woxxom, al

Thanks for the idea (my current approach was actually an SO answer of yours!)

I've just tested using that example, as well as just seeing the differences between the two methods - 
a la `document.addEventListener("contextmenu", (e) => {console.log(e.target, document.activeElement)})`

But unfortunately for my case I need non-focusable elements as well (e.g. divs).

Oliver Dunk

unread,
Oct 30, 2024, 5:53:00 AMOct 30
to al, Chromium Extensions, woxxom
Hi,

I'm not aware of any discussions around this in the past. You might want to consider opening an issue in the WebExtensions Community Group: https://github.com/w3c/webextensions/issues. We often have discussions there around inconsistencies between implementations and it's the right place to make a decision about what we want to do going forward.

I can see the appeal of a ContextType for canvas elements so that may also be worth bringing up (although likely in a different issue).

Thanks,
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/852655bf-e49e-4897-bf37-69aeba926c90n%40chromium.org.

al

unread,
Oct 30, 2024, 8:52:45 AMOct 30
to Chromium Extensions, Oliver Dunk, Chromium Extensions, woxxom, al
Thanks Oliver, have now done that

For anyone interested:
Reply all
Reply to author
Forward
0 new messages