Can you record an iframe contextmenu event, without all_frames?

78 views
Skip to first unread message

al

unread,
Oct 26, 2025, 8:41:10 AM (3 days ago) Oct 26
to Chromium Extensions
At the moment, it's my understanding that to track which element a contextMenus.onClicked occurred on, you have to have an existing contextmenu listener on every page, which you can then refer to when the former event (onClicked) is fired.

I'm able to do so with <all_urls>. But I've realised that without all_frames injection as well, I can't seem to, in any way, track an iframe's descendant being clicked on.

I know I can track an iframe itself being the target (with onClickData.frameUrl or frameId), but imagine you needed the specific click target (e.g. to modify that element), there doesn't seem to be a way, without all_frames.

For me that's an issue because it's bad enough that I have to inject the contextmenu listener on every page out of necessity and precaution, but having to do so on every frame?
That's 2x to who-knows x more injections - which could in my opinion could impact a user's device.

Is there any way how to track which of an iframe's descendants a context menu was opened  and an CM action clicked on?

woxxom

unread,
Oct 26, 2025, 9:13:35 AM (2 days ago) Oct 26
to Chromium Extensions, al
Assuming you only need the target DOM element and not the event per se, you can dynamically inject the content script code into this iframe and read document.activeElement. You might also want to check the shadow root's activeElement as well as more sites are using this tech. Here's an example: https://github.com/tophf/copy-link-text/blob/master/bg.js#L16

al

unread,
Oct 27, 2025, 1:35:12 AM (yesterday) Oct 27
to Chromium Extensions, woxxom
Thanks Woxxom, it's a nice method, but unfortunately for my case it doesn't work - where often times it might be generic elements, like imgs, svg, etc. 
I don't think those commonly get focus (at least in my testing they didn't). 

The only work-around (if you can call it that) I can think of, could be to also dynamically inject, but add a {once: true} mousemove listener, then assuming some heuristics (like position within viewport, estimating contextmenu size, etc), you might be able to guess the offset between the contextmenu click and mousemove event, which is when the contextmenu item is clicked.

I know it's absurd, but to me, so is all_frames injection for possible edge cases.

I haven't tested it with this particular case, but Firefox's getTargetElement I assume would work fine with this. 
Here's hoping Chrome add support at some point.   
Reply all
Reply to author
Forward
0 new messages