开发者

How to change the value of a text box in Firefox Add-on SDK (Jetpack)?

开发者 https://www.devze.com 2023-03-07 15:54 出处:网络
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

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开发者_如何学Python

     ' 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消