My Firefox add-on is adding a context menu to its browser action (browser toolbar button) using browser.menus.create() and specifying the “browser_action” context. This context menu item will open an extension page in its own popup window.
Is there a way to get the x and y screen coordinates of the toolbar button from where the right-click originated from? I want to be able to set the position of the popup window so that it is adjacent to the toolbar button from which the context menu item invocation happened.
Juan Pablo Isaza
Listen for the contextmenu
event and get the event's pageX
and pageY
properties:
document.addEventListener('contextmenu', function(e){
const xCoordinate = e.pageX;
const yCoordinate = e.pageY;
console.log(`x: ${xCoordinate}, y: ${yCoordinate}`)
})
You can add a listener to window/document
window.oncontextmenu = function(event){
//get rhe x/y from the event
}
Or
document.on('contextmenu', (event)=>{
//get the x/y from the event
})
On of them should do the trick
In VanillaJS:
document.addEventListener('contextmenu', function(event){
var bounds = event.target.getBoundingClientRect();
var x = event.clientX - bounds.left;
var y = event.clientY - bounds.top;
console.log( {x: x, y: y} );
})