简体   繁体   中英

problem with variable (firefox extension)

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?

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;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM