How to get mouse position in programmatically injected content script just at first right-click-event.

538 views
Skip to first unread message

Roberto Oneto

unread,
Apr 3, 2021, 5:00:11 PM4/3/21
to Chromium Extensions
Hi developers,
I'm looking for some opinions and / or suggestions.
Is there a way to get mouse coordinates when user activate the contextMenu on a selection?
I'd like to create a small div near the position where the user clicks to open the context menu and then clicks on an item within the same menu.

The contextMenu is not activated on an extension page, but instead on a content page.

I'm talking more specifically about manifest V3 and programmatically injected scripts (with chrome.scripting).
When the script is injected (only if necessary) I have not the possibility to read the event object which contains the mouse position.
Or rather, by injecting a mouseup handler into the page together with the script, this is (rightly) activated from the second event onwards.
In practice, the first event is lost, which is the one that triggers the contextMenu and injects the script into the page.
I hope I was clear.
Obviously this happens if I decide to inject the script programmatically and not declaratively (which I would like to avoid)

I don't know if there is a way to do this, but I was thinking that it would be useful to have the "event" object in response to the "click" event of the contextMenu which it would seem is not expected.
The event handler returns only two objects "info" and "tab".
If there was also "event" I would have solved my problems because at this point
I would pass the mouse coordinates contained in event object to my content script as parameters (feature not yet implemented)
or I would store these coordinates in a chrome.storage var to be read by the other side.

chrome.contextMenus.onClicked.addListener((info, tab,  ?_event_?) => {
let tp = info.menuItemId;
let ist = info.selectionText;
...
});

This instead is essentially the manifest
{
"manifest_version": 3,
"minimum_chrome_version": "89.0.0.0",
"background": {
"service_worker": "worker_wrapper.js"
},
"web_accessible_resources": [
{
"resources": [
"img/pinNote.png"
],
"matches": ["https://*/*", "http://*/*", "file://*/*"]
}
],
"permissions": [
"contextMenus",
"activeTab",
"scripting"
]
...
}

Thank you.

Jackie Han

unread,
Apr 4, 2021, 2:02:57 AM4/4/21
to Roberto Oneto, Chromium Extensions
chrome.contextMenus event doesn't supply mouse coordinates.
But for your case, you can get Selection and Range object, then put a div alongside the user's selection.

--
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 on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/2e49bcca-0e5a-4cc7-923d-d41f8e82f174n%40chromium.org.

Roberto Oneto

unread,
Apr 5, 2021, 10:29:16 AM4/5/21
to Chromium Extensions, Jackie Han, Chromium Extensions, Roberto Oneto
Ok, I completely forgot about the range.getClientRects () method.
With a small change, everything now works as I wanted.
I report a stackoverflow thread (which I found too late) with almost ready2use code:

Thanks
Reply all
Reply to author
Forward
0 new messages