views:

132

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 open() and button2 uses save(), but var in first function doesnt exist in kuku.save()

This is my code:

function openTab() {
  //Save an address of current page
  path=content.location.href;
  //Open new Tab and select it
  var tab=gBrowser.addTab("http://www.google.com");
  var newTabBrowser = gBrowser.getBrowserForTab(tab);
  gBrowser.selectedTab=tab;
}

function write() {
  content.body.innerHTML=path;
}

Function openTab() is execute, when proper button is pressed, the same with write() function. Problem is, how to save to var this address, becouse if I will do it from first function, the function that is from button doesnt have this var. After this i try this code:

var path = null;
function openTab() {
  //Save an address of current page
  path=content.location.href;
  //Open new Tab and select it
  var tab=gBrowser.addTab("chrome://intabeditor/content/editor.html");
  var newTabBrowser = gBrowser.getBrowserForTab(tab);
  gBrowser.selectedTab=tab;
}

function write() {
    if (path!=null)
    content.body.innerHTML=path;
}

But in this code is bad too. I thought, the problem is becouse path is local var, so I try to use "more global" var:

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

In this example button1 uses kuku.open(), button2 kuku.save()

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 last value of path. For example: When i have 2 tabs open (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. I hope, i didnt make you feel dizzy ;)

A: 

There are a lot of ways to share data between windows, the easiest way is using fuel application object.

Another way that, I think, fits in your problem is using setUserData/getUserData method.

Cleiton