简体   繁体   中英

How can I get instances of different tabs such that I can store variables / perform operations specific to respective tabs

Note: I am using GWT to build a firefox extension

Say I have a class which operates on the different TABs of the browser:

class TabRoutine{
  Element tab;
  static int tabData;

  public TabRoutine(Element tab){
    this.tab=tab;
  }

  public void tabMethod(){
    //Operation on tab
  }      
}

For each tab I must create an instance of this class which I want to destroy once the tab closes. Therefore each tab has an int tabData related to it. Here is how I approached it:

I have the document element pertaining to each tab available with me (I also have the window object if that helps). Therefore, I decided to use

gBrowser.getBrowserForDocument(doc);

My question is does this Browser element I get unique for tabs? What I imply is that say I have 2 tabs with documents doc1 and doc2. If I map these Browser elements to objects of TabRoutine, would each individual tab always return the same (and correct) browser so that i can find the object related to it?

Please tell if some aspect of the question is unclear and I'll elaborate. Thanks a lot!

Yes, each <browser> element is associated with a particular tab. It is not bound to a particular content document however - a different site can be loaded into the same tab. You probably want to go through the gBrowser.browsers collection when your extension starts. Then you would listen to TabOpen and TabClose events to get notified whenever a tab is opened or closed (use gBrowser.getBrowserForTab method to get the <browser> element that these events are associated with).

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