Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to get the word under the context menu in a firefox extension?

I'm writing a Firefox extension to add a context menu element and I can get both the DOM element and the selected text, but can't manage to get the unselected word on which the right click event was fired. I assume it must be possible, since the spell-checker does it, or is that a special inbuilt function?

Any ideas? Here is the code to get the other two:

oncommand="if(gContextMenu.isTextSelected) rightClick(document.commandDispatcher.focusedWindow.getSelection().toString()); else rightClick(gContextMenu.target.innerHTML);"
like image 984
yuttadhammo Avatar asked Nov 04 '22 08:11

yuttadhammo


1 Answers

I'm not aware of a super-easy way to do this. The spell checker only works in textarea elements by default (though it can be modified to work within input fields also). As such, that functionality is most likely encapsulated within the internal code for those controls.

However, I found a short snippet of code that claims to get the word under the mouse cursor in an answer for another question here on StackOverflow. It simply makes use of the onmousemove event to keep track of where you are.

From a usability perspective, I personally think it's best to force the user to select the text they are interested in, then operate on that. Built-in functionality for working with selected text already exists (as you pointed out), and then there's no question to the user what they're talking about (though I guess this all depends on what your end goal is).

like image 189
Jonah Bishop Avatar answered Nov 09 '22 12:11

Jonah Bishop