Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect which option from hyperlink's right-clicked contextmenu was clicked?

Let's assume I'm using default browser's contextmenu.

  • How can I find out that user for example has chosen "Copy link Location" from right-click contextmenu?
  • How can I overwrite default behaviour after "Copy link Location" action? (e.g. replace the link, but if user choose "Open in a new tab", it'll still be the original link).

Edited: replacing link in the clipboard RIGHT AFTER closing the link's contextmenu (even if user had chosen different option than "Copy link location") is also acceptable solution.

like image 876
Michal_Szulc Avatar asked Jan 30 '19 00:01

Michal_Szulc


Video Answer


2 Answers

Unfortunately it is not possible to detect something what was choosen on default browser's contextmenu because of security reasons.

It is also not possible to overwrite override default functions / actions on default browser's contextmenu because of security reasons.

What you can do:

  1. With browser extensions (end user should install your extension at first) you can add your own custom options with your icon and functions to default browser's contextmenu.

  2. You can override default behaviour on mouse right click. For example after right click you can show your own custom contextmenu with your own functions.


EDIT: My answer to the first comment from OP (original poster):

The link replacement after right click and on oncotextmenu event has nothing to do what you want to achieve in your question! I wrote already above that

you can override default behaviour on mouse right click

And this means also that you can replace the link after right click, but in your question you want to replace the link after click on specific menu option from default context menu. And this is not possible because of security reasons.

You have to read about the oncontextmenu event:

The contextmenu event typically fires when the right mouse button is clicked on the window. Unless the default behavior is prevented, the browser context menu will activate.

like image 52
Bharata Avatar answered Oct 12 '22 22:10

Bharata


Edited: replacing link in the clipboard RIGHT AFTER closing the link's contextmenu (even if user had chosen different option than "Copy link location") is also acceptable solution.

If the user grants appropriate permissions you can use contextmenu event, focusout attached to window within contextmenu event handler and Async Clipboard API

<body>
  <a id="a" href="#">click</a>
  <script>
    a.addEventListener('contextmenu', e => {
      console.log(e);
      window.addEventListener("focusout", e => {
        console.log(e);
        navigator.clipboard.writeText(a.href)
          .then(() => {
            console.log('Text copied to clipboard');
          })
          .catch(err => {
            // This can happen if the user denies clipboard permissions:
            console.error('Could not copy text: ', err);
          });
        navigator.clipboard.readText()
          .then(text => {
            console.log('Pasted content: ', text);
          })
          .catch(err => {
            console.error('Failed to read clipboard contents: ', err);
          });
      }, {
        once: true
      });
    });
  </script>
</body>

plnkr

like image 43
guest271314 Avatar answered Oct 12 '22 22:10

guest271314