For my application, I want to show a custom context menu whenever someone right clicks. And if the right click was on any word, I want to show some extra option for it. I looked up on the net and I found solution on how to get the word when it is left clicked, I tried to modify it for the right-click but somehow couldn't it to work.
The code below, on doing a left click it shows an alert with the word and on doing a right click it's expected to do the same. But sometimes it doesn't show any pop-up and when it shows the word in the pop-up the previous right-clicked word.
$('.text123').click(function(e){
s = window.getSelection();
var range = s.getRangeAt(0);
var node = s.anchorNode;
while(range.toString().indexOf(' ') != 0) {
range.setStart(node,(range.startOffset -1));
}
range.setStart(node, range.startOffset +1);
do{
range.setEnd(node,range.endOffset + 1);
}while(range.toString().indexOf(' ') == -1 && range.toString().trim() != '');
var str = range.toString().trim();
alert(str);
});
$(".text123").mouseup(function(){
var e = window.event;
if (e.button == 2)
{
jQuery(document.elementFromPoint(e.pageX, e.pageY)).click();
}
});
I am testing on Edge browser as I want to use the code in UWP app.