Firefox SDK:ActionButton不工作 - 表现得像我没有点击它 - 什么都没有

时间:2015-08-19 13:05:41

标签: javascript firefox firefox-addon firefox-addon-sdk

我从documentation获取了这个简单的代码:

var { ActionButton } = require('sdk/ui/button/action');

var button = ActionButton({
    id: 'translate-button',
    label: 'Replace selected text with translated',
    icon: './ico.png',
    onclick: function() {
        console.log('x');
    }
});

我点击了 - 没有任何反应 - 终端很清楚。

我在Xubuntu 14.04.2 i386下,使用命令jpm run -b /usr/bin/firefox构建。我的插件的其他功能工作正常。我不知道问题出在哪里。看起来像SDK中的错误 - 因为如果出现问题,我需要在控制台中看到错误。

我的index.js

的完整代码
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 data = require('sdk/self').data;
var tabs = require('sdk/tabs');
var prefs = require('sdk/simple-prefs').prefs;
var cmitems = null;
var { ActionButton } = require('sdk/ui/button/action');

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

var button = ActionButton({
    id: 'translate-button',
    label: 'Replace selected text with translated',
    icon: './ico.png',
    onclick: function() {
        console.log('x');
    }
});

var menuItem = contextMenu.Item({
    data: uuidstr, // for 'binding' tooltop's 'id' + text
    label: inProgress, // ...
    image: self.data.url('ico.png'),
    context: contextMenu.SelectionContext(),
    contentScriptFile: data.url('script.js'),
    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);
                getMostRecentBrowserWindow().document.querySelectorAll('#text').value = 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);
}

我的script.js

self.on("context", function() {
    selectionText = getSelectedText(window, document);
    self.postMessage({name:"context", data:selectionText});
    return true;
});
self.on("click", function() {
    self.postMessage({name:"click", data:"https://translate.yandex.ru?text=" + selectionText.replace("&", "%26")});
});
self.on('translated', function(translatedText) {
    console.log(translatedText);
});

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.getAttribute("id") == 'text' ||
            (doc.activeElement.nodeName == 'INPUT' &&
            doc.activeElement.getAttribute("type").toLowerCase() == 'text')
        ){
            ta = doc.activeElement;
            translate();
            return ta.value.substring(ta.selectionStart, ta.selectionEnd);
        } else {
            // As of Firefox 31.0 this appears to have changed, again.
            // Type 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();
    }
}

1 个答案:

答案 0 :(得分:3)

我刚刚阅读了文档

你使用onclick

文档用于 C lick