views:

158

answers:

1

I try to make firefox extension, that display two buttons: 1 and 2. When you press button1 to var path get value, that is address of current open page, then new tab is opening becoming active. In this tab there is button2, that allow to put (innerHTML) value of var path (this address of first tab). And now the problem: button1 uses function kuku.open() and button2 uses kuku.save(), but var in first function doesnt exist in kuku.save()

var kuku = {

open: function () {
//put current URI addres into var
var path=content.location.href;

//Create nslFile object
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(path);

//Put file content into data variable
var data = "";
var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
                        createInstance(Components.interfaces.nsIFileInputStream);
var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
                        createInstance(Components.interfaces.nsIConverterInputStream);
fstream.init(file, -1, 0, 0);
cstream.init(fstream, "UTF-8", 0, 0); // you can use another encoding here if you wish

let (str = {}) {
  cstream.readString(-1, str); // read the whole file and put it in str.value
  data = str.value;
}
cstream.close(); // this closes fstream

//Open editor in new Tab and select it

var tab=gBrowser.addTab("http://www.google.pl");
var newTabBrowser = gBrowser.getBrowserForTab(tab);
gBrowser.selectedTab=tab;

},


save: function () {
//Write in body address from var
content.body.innerHTML=path;
}
}

I think, the problem is becouse path is local var, but I cannot use global var. It would work, only for one tab per browser. I mean, when the user press button1 on page A, and button1 on page B, then this two new opened pages will have var path of the same value. When you press button on other site it will overwrite value of path. I hope, i didnt make you feel dizzy ;)

Have someone got idea?

+1  A: 

Put the variable in the object, not in the function. If the variable is in the function, then only the function has access to it:

var kuku = {};
kuku.path = "";

kuku.open = function () {
  kuku.path = content.location.href;
  //The rest of the code here
};


kuku.save = function () {
  content.body.innerHTML=kuku.path;
};
Marius
It doesnt work :( When i have 2 tabs open (for example google.com and facebook.com) and from gogle i will press the button1, it will open new tab and value of kuku.path will be google adddress. But next, when I press button1 in facebook, value will change to facebook address and will be the same in this two new-opened pages