有什么方法可以获取 Firefox 扩展中上下文菜单下的单词吗?
我正在编写一个 Firefox 扩展来添加上下文菜单元素,我可以获取 DOM 元素和选定的文本,但无法获取触发右键单击事件的未选定单词。我认为这一定是可能的,因为拼写检查器会这样做,或者这是一个特殊的内置函数?
有什么想法吗?这是获取另外两个的代码:
oncommand="if(gContextMenu.isTextSelected) rightClick(document.commandDispatcher.focusedWindow.getSelection().toString()); else rightClick(gContextMenu.target.innerHTML);"
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);"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道有什么超级简单的方法可以做到这一点。默认情况下,拼写检查器仅在文本区域元素中工作(尽管也可以修改它以在输入字段中工作)。因此,该功能很可能封装在这些控件的内部代码中。
但是,我发现了一小段代码,声称可以在另一个问题的答案中获取鼠标光标下的单词在 StackOverflow 上。它只是利用 onmousemove 事件来跟踪您所在的位置。
从可用性的角度来看,我个人认为最好强制用户选择他们感兴趣的文本,然后对其进行操作。用于处理选定文本的内置功能已经存在(正如您所指出的),然后用户就毫无疑问他们在谈论什么(尽管我想这一切都取决于您的最终目标是什么)。
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).