检索选择的单词 [英] Retrieving a word that is selected

查看:227
本文介绍了检索选择的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个插件,试图在Firefox浏览器中显示所选词的含义。如何捕捉被选中的单词/单词?

解决方案

我使用下面的函数来获取加载项中的选定文本。使用的方法取决于选择的位置和Firefox的版本。尽管可以根据这些标准来选择使用哪种方法,但是在我编写/调整它(然后在Firefox 31.0中对它进行更新时)时,只需运行多个方法直到获得有效的字符串为止就更容易了。

  / ** 
*解决Firefox的一个问题,即它不会从选项$ b中返回文本$ b *如果所选文本在INPUT /文本框中。
*
*输入:
* win当前窗口元素
* doc当前文档
*这两个项目是输入,所以这个函数可以从$ b变量窗口和文档未定义的$ b *环境。
* /
函数getSelectedText(win,doc){
//由
中的jscher2000改写而来// http://forums.mozillazine.org/viewtopic.php ?f = 25& t = 2268557
//应该解决的问题是Firefox没有得到选择文本
//在textarea / input / textbox中。
var ta;
if(win.getSelection&& doc.activeElement){
if(doc.activeElement.nodeName ==TEXTAREA||
(doc.activeElement.nodeName ==INPUT &&
doc.activeElement.getAttribute(type)。toLowerCase()==text)
){
ta = doc.activeElement;
返回ta.value.substring(ta.selectionStart,ta.selectionEnd);
} else {
//从Firefox 31.0开始,这似乎又改变了。
//尝试多种方法来覆盖不同版本的Firefox。
let returnValue =;
if(typeof win.getSelection ===function){
returnValue = win.getSelection()。toString(); (typeof returnValue ===string&& returnValue.length> 0){
return returnValue
}
}
if(typeof doc .getSelection ===function){
returnValue = win.getSelection()。toString(); (typeof returnValue ===string&& returnValue.length> 0){
return returnValue
}
}
if(typeof win .content.getSelection ===function){
returnValue = win.content.getSelection()。toString();
if(typeof returnValue ===string&& returnValue.length> 0){
return returnValue
}
}
//显示我们没有找到任何选定的文字。
return;
}
} else {
return doc.getSelection()。toString();
}
}


I am writing a plugin which tries to display the meaning of a selected word in firefox browser. How do I capture the word/words being selected?

解决方案

I use the function below to get the selected text in an add-on. The method that is used depends on where the selection is and which version of Firefox. While which method to use could be selected based on those criteria, at the time I wrote/adapted it (and then updated it upon it breaking in Firefox 31.0), it was easier to just run through multiple methods until a valid string is obtained.

/**
 * Account for an issue with Firefox that it does not return the text from a selection
 *   if the selected text is in an INPUT/textbox.
 *
 * Inputs:
 *     win    The current window element
 *     doc    The current document
 *   These two items are inputs so this function can be used from
 *     environments where the variables window and document are not defined.
 */
function getSelectedText(win,doc) {
    //Adapted from a post by jscher2000 at 
    //  http://forums.mozillazine.org/viewtopic.php?f=25&t=2268557
    //Is supposed to solve the issue of Firefox not getting the text of a selection
    //  when it is in a textarea/input/textbox.
    var ta;
    if (win.getSelection && doc.activeElement) {
        if (doc.activeElement.nodeName == "TEXTAREA" ||
                (doc.activeElement.nodeName == "INPUT" &&
                doc.activeElement.getAttribute("type").toLowerCase() == "text")
        ){
            ta = doc.activeElement;
            return ta.value.substring(ta.selectionStart, ta.selectionEnd);
        } else {
            //As of Firefox 31.0 this appears to have changed, again.
            //Try multiple methods to cover bases with different versions of Firefox.
            let returnValue = "";
            if (typeof win.getSelection === "function") {
                returnValue = win.getSelection().toString();
                if(typeof returnValue === "string" && returnValue.length >0) {
                    return returnValue
                }
            }
            if (typeof doc.getSelection === "function") {
                returnValue = win.getSelection().toString();
                if(typeof returnValue === "string" && returnValue.length >0) {
                    return returnValue
                }
            }
            if (typeof win.content.getSelection === "function") {
                returnValue = win.content.getSelection().toString();
                if(typeof returnValue === "string" && returnValue.length >0) {
                    return returnValue
                }
            }
            //It appears we did not find any selected text.
            return "";
        }
    } else {
        return doc.getSelection().toString();
    }
}

这篇关于检索选择的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆