• Home
  • Jobs
  • Courses
  • Questions
  • Teachers
  • For business
  • ES/EN

0

27
Views
How to get the x and y coordinates of mouse click on browser action context menu item

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.

about 1 month ago ·

Juan Pablo Isaza

3 answers
Answer question

0

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}`)
})

about 1 month ago · Juan Pablo Isaza Report

0

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

about 1 month ago · Juan Pablo Isaza Report

0

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} );
})
about 1 month ago · Juan Pablo Isaza Report
Answer question
Find remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.