user1157584
user1157584

Reputation: 41

How do I store and retrieve a variable from an array using the Firefox Add-on SDK?

I am attempting to develop a Firefox extension.

I have a simple need. At this time in testing, I simply wish to define an array. When the button is pushed, the Current Window goes to the first URL in the array. When the button is pushed again, it goes to the next URL in the array.

I am of the impression that I need to store the present sequence in the array using simple-storage (ss.storage) yet I cannot make it work. I thought perhaps I could store the sequence information using a Cookie but the Add-on SDK appears to be very stringent. Perhaps there is a far simpler method but I cannot seem to identify what that would be.

Current Status - when the button is pushed, the code opens three separate windows for each URL in the array. If you could please help me to modify this code so that opens one URL from the array each time the button is pushed - as described above.

require("widget").Widget({

  id: "view-source-widget",
  label: "View Source",
  contentURL: "http://www.mozilla.org/favicon.ico",
  onClick: function() {

    var arr = new Array("one.com","two.com","three.com");

    for(var i=0; i<arr.length; i++) {

      var value = arr[i];   
      var ss = require("simple-storage");
      ss.storage.myURL= value;

      var windows = require("windows").browserWindows;
      windows.open(ss.storage.myURL);    

    }
  }

});

Upvotes: 1

Views: 227

Answers (1)

Wladimir Palant
Wladimir Palant

Reputation: 57651

If I understand correctly what you are trying to do - you don't need persistent storage, a normal global variable will do. The global variable indicates the array index at which you are right now. Something like this:

var urls = ["one.com","two.com","three.com"];
var urlIndex = 0;

...

onClick: function() {
  var windows = require("windows").browserWindows;
  windows.open(urls[urlIndex]);

  // Increase index for the next click
  urlIndex++;
  if (urlIndex >= urls.length)
    urlIndex = 0;
}

...

Upvotes: 1

Related Questions