I want Chrome to detect right-click events

Asked 1 years ago, Updated 1 years ago, 58 views

I am creating a Chrome App, but I would like to do something like right-click the menu like Chrome Dev Editor.
What kind of event handling should I do to detect a right click?

javascript google-chrome google-chrome-apps

2022-09-30 20:48

2 Answers

For your information only
I'm not familiar with Chrome app, so I don't know about it, but
If it's normal js, would it be like the following?


// Let's say the id is something.
variable= document.getElementById("something");
element.onclick=function(e){
    if(e.button===2){
        // Write down the right-click process in here.
    }
}

Reference
click-Event reference|MDN


2022-09-30 20:48

Another solution I found while researching myself was that I was able to implement it by processing the contextmenu event.

varem= document.querySelector('#targetElement');
elem.addEventListener('contextmenu', function(e){

  // block browser default right-click behavior
  e.preventDefault();

  // Displaying the DOM element representing the UI of the context menu in the mouse position
  varcontextMenu= document.querySelector('#myContextMenu');
  contextMenu.classList.remove('hidden');
  contextMenu.style.left=e.x+'px';
  contextMenu.style.top=e.y+'px';

});


2022-09-30 20:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.