Firefox 上下文:contextMenu.SelectionContext()不适用于输入字段中的选定文本

Firefox 上下文:contextMenu.SelectionContext()不适用于输入字段中的选定文本,firefox,firefox-addon,firefox-addon-sdk,Firefox,Firefox Addon,Firefox Addon Sdk,我的小项目-Firefox的文本翻译扩展。用户在页面上选择文本,右键单击,然后在上下文菜单或弹出窗口中看到翻译。在我的contextMenu.Item中,我使用context:contextMenu.SelectionContext()来确定上下文(例如,用户单击图像或所选文本) 但如果在输入字段中选择了文本,则此项不起作用,未提及它。我需要做什么来处理输入字段的选择上下文,而不仅仅是页面上的常规文本?在这种情况下,我看到上下文菜单,但在调试中,我看到所选文本没有发送到扩展代码 我什么也没试过

我的小项目-Firefox的文本翻译扩展。用户在页面上选择文本,右键单击,然后在上下文菜单或弹出窗口中看到翻译。在我的
contextMenu.Item
中,我使用
context:contextMenu.SelectionContext()
来确定上下文(例如,用户单击图像或所选文本)

但如果在输入字段中选择了文本,则此项不起作用,未提及它。我需要做什么来处理输入字段的选择上下文,而不仅仅是页面上的常规文本?在这种情况下,我看到上下文菜单,但在调试中,我看到所选文本没有发送到扩展代码

我什么也没试过

我目前的代码是:

const { getMostRecentBrowserWindow } = require('sdk/window/utils');
var uuid = require('sdk/util/uuid').uuid();
var uuidstr = uuid.number.substring(1, 37);
var notifications = require("sdk/notifications");
var contextMenu = require("sdk/context-menu");
var Request = require("sdk/request").Request;
var self = require('sdk/self');
var tabs = require('sdk/tabs');
var prefs = require('sdk/simple-prefs').prefs;
var cmitems = null;

var wasTranslatedSecondTime = false;
var inProgress = '...';
var translated = '';

var menuItem = contextMenu.Item({
    data: uuidstr, // for 'binding' tooltop's 'id' + text
    label: inProgress, // ...
    image: self.data.url('ico.png'),
    context: contextMenu.SelectionContext(),
    contentScript: 'self.on("context", function() {' +
                        'var selectionText = window.getSelection().toString();' +
                        'self.postMessage({name:"context", data:selectionText});' +
                        'return true;' +
                    '});' +
                    'self.on("click", function() {' +
                        'var selectionText = window.getSelection().toString();' +
                        'self.postMessage({name:"click", data:"https://translate.yandex.ru?text=" + selectionText.replace("&", "%26")});' +
                    '})',
    onMessage: function(message) {
        if (message.name == 'context') {
            menuItem.label = inProgress; // ...
            if (cmitems != undefined) cmitems[0].tooltipText = '';
            var input = message.data.replace('&', '%26');
            translate('ru', input); // default direction - from EN to RU
        } else { // if (message.name == 'click')
            tabs.open(message.data);
        }
    }
});

function translate(lang, input) {
    Request({ // key is not referral but API-key: https://api.yandex.com/translate/doc/dg/concepts/api-overview.xml
        url: 'https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20150627T071448Z.117dacaac1e63b79.6b1b4bb84635161fcd400dace9fb2220d6f344ef&lang=' +
                                                                                    lang + '&text=' + input,
        onComplete: function (response) {
            translated = response.json.text[0];
            if (input == translated && wasTranslatedSecondTime == false) {  // if input on Russian and we receive the same text -
                translate('en', input);                                     // translate again selected text into English
                wasTranslatedSecondTime = true;
            } else { // show results
                if (prefs.popup) popup(translated);
                menuItem.label = translated;
                wasTranslatedSecondTime = false;
                if (prefs.tooltip) tooltip(translated);
            }
        }
    }).get();
}

function popup(text) {
    if (text.length > 0)
        notifications.notify({
            title: 'translate.yandex.ru',
            text: text,
            time: 5000
        })
}

function tooltip(translated) {
    menuItem.data = uuidstr + translated;
    cmitems = getMostRecentBrowserWindow().document.querySelectorAll(".addon-context-menu-item[value^='"+uuidstr+"']");
    cmitems[0].tooltipText = cmitems[0].value.substring(36);
}

您的问题似乎可以归结为:即使在输入字段中,如何获取所选文本

您当前使用的是
var selectionText=window.getSelection().toString()当所选文本位于输入字段中时失败

在我的一个扩展中,我使用以下命令获取所选文本。即使选定的文本位于输入字段中,它也可以工作:

/**
 * Fix an issue with Firefox that it does not return the text from a selection if 
 * the selected text is in an INPUT/textbox.
 */
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
                }
            } //else
            if (typeof doc.getSelection === "function"){
                returnValue = doc.getSelection().toString();
                if(typeof returnValue === "string" && returnValue.length >0) {
                    return returnValue
                }
            } //else
            if (typeof win.content.getSelection === "function"){
                returnValue = win.content.getSelection().toString();
                if(typeof returnValue === "string" && returnValue.length >0) {
                    return returnValue
                }
            } //else
            //It appears we did not find any selected text.
            return "";
        }
    } else {
        return doc.getSelection().toString();
    }
}

您的问题似乎可以归结为:即使在输入字段中,如何获取所选文本

您当前使用的是
var selectionText=window.getSelection().toString()当所选文本位于输入字段中时失败

在我的一个扩展中,我使用以下命令获取所选文本。即使选定的文本位于输入字段中,它也可以工作:

/**
 * Fix an issue with Firefox that it does not return the text from a selection if 
 * the selected text is in an INPUT/textbox.
 */
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
                }
            } //else
            if (typeof doc.getSelection === "function"){
                returnValue = doc.getSelection().toString();
                if(typeof returnValue === "string" && returnValue.length >0) {
                    return returnValue
                }
            } //else
            if (typeof win.content.getSelection === "function"){
                returnValue = win.content.getSelection().toString();
                if(typeof returnValue === "string" && returnValue.length >0) {
                    return returnValue
                }
            } //else
            //It appears we did not find any selected text.
            return "";
        }
    } else {
        return doc.getSelection().toString();
    }
}

这是sdk上下文菜单的一个限制,如果在可编辑字段中单击鼠标右键,它将不会显示。这就是我在几年前玩它时所记得的,事情可能已经改变了。解决这个问题的方法是不使用sdk。但我建议您等待更新的sdk开发人员回复/评论/这是sdk上下文菜单的一个限制,因为如果您右键单击可编辑字段,它将不会显示。这就是我在几年前玩它时所记得的,事情可能已经改变了。解决这个问题的方法是不使用sdk。但我建议您等待更新的sdk开发人员回复/评论/谢谢,我添加了您的函数,但现在控制台中出现错误,
getSelectionText未定义
。我的代码在这里@VitalyZdanevich,啊,好的。你的意思是说
getSelectedText
没有定义(不是getSelectionText)。这是因为函数需要成为内容脚本的一部分。因为它当前在代码中,所以在主代码中定义了它,而不是在内容脚本中定义。内容脚本在完全独立的上下文中运行,无法看到主脚本中的任何内容。@VitalyZdanevich,注意:复制代码时出错:第二个内部
if
语句/块应该包含
doc.getSelection
,而不是
win.getSelection
。这已在上面更正。谢谢,下一个错误是:
doc.activeElement.getAttribute(…)为null
,这是关于行
doc.activeElement.getAttribute(“type”).toLowerCase()=“text”)
。你对此有什么想法吗?更新的代码仍在+。哦,我明白了,这是因为我在ya.ru上选择了
input
而不选择
type=text
。@VitalyZdanevich,有趣的是,我在使用代码时没有看到错误,但我的代码在不同的环境中(引导加载主代码,而不是内容脚本)。您可以给我一个指向您测试的网页的指针,以及您正在使用的输入字段,以便我可以在这里复制问题吗?谢谢,我添加了您的函数,但现在控制台中出现错误,
getSelectionText未定义
。我的代码在这里@VitalyZdanevich,啊,好的。你的意思是说
getSelectedText
没有定义(不是getSelectionText)。这是因为函数需要成为内容脚本的一部分。因为它当前在代码中,所以在主代码中定义了它,而不是在内容脚本中定义。内容脚本在完全独立的上下文中运行,无法看到主脚本中的任何内容。@VitalyZdanevich,注意:复制代码时出错:第二个内部
if
语句/块应该包含
doc.getSelection
,而不是
win.getSelection
。这已在上面更正。谢谢,下一个错误是:
doc.activeElement.getAttribute(…)为null
,这是关于行
doc.activeElement.getAttribute(“type”).toLowerCase()=“text”)
。你对此有什么想法吗?更新的代码仍在+。哦,我明白了,这是因为我在ya.ru上选择了
input
而不选择
type=text
。@VitalyZdanevich,有趣的是,我在使用代码时没有看到错误,但我的代码在不同的环境中(引导加载主代码,而不是内容脚本)。您能给我一个指向您测试的网页的指针,以及您正在使用的输入字段,以便我在这里复制问题吗?