Firefox SDK: ActionButton不工作-表现得像我没有点击它-什么都没有
Firefox SDK: ActionButton not working - behave like I am not click on it - just nothing
我从文档中取了这个简单的代码:
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();
}
}
我刚看了文档
使用onclick
文档在C上使用单击
相关文章:
- 这是什么 ==- javascript 运算符
- 我的单元测试选项是什么
- 全局变量和全局对象的属性之间有什么区别吗
- 打破承诺链的好方法是什么
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- Javascript中的空白是什么
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- 在ember/handlers中使用value和valueBinding有什么区别
- 什么'在webpack外部设置中,reactDom和reactDom不同
- 在JavaScript中拆分日期字符串的更好方法是什么
- 为什么$.brower被弃用?还有什么更好的替代方案
- 什么是&&在没有if的行中的变量之间
- 将jQuery.ech()方法转换为本地JavaScript抽象的最佳方法是什么
- 处理浮点错误的最佳方法是什么
- DOM元素和angular元素之间的主要区别是什么
- javascript导入的最佳实践是什么
- 这两个关于 JavaScript 作用域链的例子有什么区别?
- 什么's本地node.js服务器和python简单http服务器之间的区别
- 基于窗口宽度jquery的函数的替代方法是什么
- Firefox SDK: ActionButton不工作-表现得像我没有点击它-什么都没有