1

我正在尝试更改文本框的内容(使用最新版本的附加 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?我试图将 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 中过去很容易的事情这次变得非常困难(或者至少非常不直观)。

4

1 回答 1

0

如果你不能在你的内容脚本中包含整个函数(你可以把你的函数放在一个单独的文件中,如果这样更容易的话)那么你可以将一条消息发回你的内容脚本,尽管这需要你的内容脚本中的一个函数接收消息。请参阅使用内容脚本

于 2011-05-20T23:12:40.800 回答