如何更改 Firefox Add-on SDK (Jetpack) 中文本框的值?
我正在尝试更改文本框的内容(使用最新版本的附加 SDK 1.05b)。我能够获取其内容,但我不知道如何更改它。这是我的代码的相关部分:
var deasciifyItem = contextMenu.Item({
label: "Label",
context: contextMenu.SelectorContext("input[type=text], textarea"),
contentScript: 'self.on("click", function (node) {' +
'var text = node.value;' +
'self.postMessage(text);' +
'});',
onMessage: function(text) {
if (text.length == 0) {
throw ("Text to convert must not be empty!");
}
console.log(text);
console.log(someMyFunction(text));
text = "A computed new value to replace the old value in text box!";
}
});
我可以读取任何文本框的内容并将其记录到控制台,但如何更改其内容,例如通过将 node.value 传递给我定义的函数来更改其内容?我尝试将 node.value 作为参数传递给 self.postMessage 函数,但它不起作用。我想要实现的目标是:
node.value = someMyFunction(node.value);
我也尝试在内部执行此
' node.value = someMyFunction(node.value); ' + ...
操作,但随后它说 someMyFunction 在此上下文中未定义(我知道它已定义,因为我测试了它的
console.log(someMyFunction(text));
工作原理)。
我被困在这一点上。有什么建议吗?我既不能强制 someMyFunction 进入 contentScript 的范围,也不能在“onMessage”中获取“节点”。在以前版本的 Add-on SDK 中曾经非常容易的事情这次变得非常困难(或者至少非常不直观)。
I'm trying to change the contents of a text box (using the latest version of Add-on SDK, 1.05b). I'm able to get its contents but I could not find out how I can change it. Here's the relevant part of my code:
var deasciifyItem = contextMenu.Item({
label: "Label",
context: contextMenu.SelectorContext("input[type=text], textarea"),
contentScript: 'self.on("click", function (node) {' +
'var text = node.value;' +
'self.postMessage(text);' +
'});',
onMessage: function(text) {
if (text.length == 0) {
throw ("Text to convert must not be empty!");
}
console.log(text);
console.log(someMyFunction(text));
text = "A computed new value to replace the old value in text box!";
}
});
I can read the contents of any text box and log it to the console but how can I change its contents, e.g. the node.value by passing node.value to a function that I defined? I tried to pass node.value as a parameter to self.postMessage function but it does not work. What I'm trying to achieve is something like:
node.value = someMyFunction(node.value);
I also tried to do that inside
' node.value = someMyFunction(node.value); ' + ...
part but then it says that someMyFunction is not defined in this context (I know that it is defined because I tested that
console.log(someMyFunction(text));
works).
I'm stuck at this point. Any tips? I can neither force someMyFunction into the scope of contentScript nor can I get the 'node' within 'onMessage'. What used to be very easy in previous versions of Add-on SDK turned out to be very difficult (or very unintuitive, at least) this time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您无法将整个函数包含在内容脚本中(您可以将函数放在单独的文件中,如果这样更容易),那么您可以将消息发布回内容脚本,尽管这需要内容脚本中的函数接收消息。请参阅使用内容脚本。
If you can't include the entire function inside your content script (you can put your function in a separate file if it makes this easier) then you can post a message back to your content script, although this needs a function inside your content script to receive the message. See Working with Content Scripts.