javascript - Firefox SDK: ActionButton not working - behave like I am not click on it - just nothing -
i taked simple code documentation:
var { actionbutton } = require('sdk/ui/button/action'); var button = actionbutton({ id: 'translate-button', label: 'replace selected text translated', icon: './ico.png', onclick: function() { console.log('x'); } });
i clicking - , nothing happen - terminal clear.
i under xubuntu 14.04.2 i386, building command jpm run -b /usr/bin/firefox
. other functions of addon working fine. have no idea problem. looks bug in sdk - because if wrong need see error in console.
full code of 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 translated', icon: './ico.png', onclick: function() { console.log('x'); } }); var menuitem = contextmenu.item({ data: uuidstr, // '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 - en ru } else { // if (message.name == 'click') tabs.open(message.data); } } }); function translate(lang, input) { request({ // key not referral 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 , receive same text - translate('en', input); // translate again selected text 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); }
and 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 post jscher2000 @ http://forums.mozillazine.org/viewtopic.php?f=25&t=2268557 // supposed solve issue of firefox not getting text of selection when // in 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 { // of firefox 31.0 appears have changed, again. // type multiple methods cover bases 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 // appears did not find selected text. return ""; } } else { return doc.getselection().tostring(); } }
i read documentation
you use onclick
the documentation uses onclick
Comments
Post a Comment