Let's assume I'm using default browser's contextmenu.
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.
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:
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With