4

How can I uniquely identify every separate browser window that is currently open for all major browsers using javascript? Let me explain what I need to know and let's consider the scenario below:

I have 3 browser windows [any modern browser, i.e. Chrome, Firefox etc.] that are currently open, each containing multiple tabs.

  • Window #1: 3 tabs [Tab A, Tab B, and Tab C]
  • Window #2: 2 tabs [Tab D and Tab E]
  • Window #3: 4 tabs [Tab F, Tab G, Tab H, and Tab I]

How can I uniquely identify each browser window [i.e. capture a unique ID/value for each browser window, not the tab's window]? So, at the end I will have 3 IDs [all windows], not 9 IDs [all tabs]. I am able to identify all the tabs in the windows by creating a browser add-on or extension which uses jQuery [crossrider], but could not find a way to uniquely identify each window.

Is this possible by JavaScript/JQuery? If so, how? if not, why?

I tried "window.name" in javascript, but that gives me 9 IDs, not 3 IDs.

Wally Altman
  • 3,535
  • 3
  • 25
  • 33
Ali007
  • 648
  • 1
  • 12
  • 20
  • I'm not quite clear why you would want to identify the windows, what is the use-case? Modern browsers generally treat tabs as windows (what used to be "many open windows" is now "many open tabs") and implement them as separate processes. So really, you only need the tab identities which, from your comments, I can see you have already managed to obtain. [Disclaimer] I am a Crossrider employee – Shlomo Oct 24 '13 at 08:55
  • 1
    Thanks for your help. I want to save the user's browsing session [currently opened windows and tabs] to the cloud/server on button click by user so that the user can leave/close his/her browser/computer, come back again and start browsing where he/she left off. For example, when Chrome crashes, it remembers user's last session and reopens all the windows and tabs exactly the way they were opened before the crash so that users don't have to reopen them again. In this scenario, I don't want to save/open 9 tabs in 1 window, but 3 tabs in window #1, 2 tabs in window #2, and 4 tabs in window #3. – Ali007 Oct 24 '13 at 10:22
  • 1
    There are lot of scenarios for extensions/plugins where windows tracking is very important, I think Crossrider should update its API to support browser windows identification – Ashraf Bashir Oct 25 '13 at 20:30
  • I love crossrider as it really simplifies a developer's development efforts. Is there a way I can also help creating some crossrider plugins [similarly the way people created jQuery plugins before]? What's the approach to create a plugin for crossrider? This way any developer can create a plugin to extend the crossrider APIs to satisfy his/her unique project requirements. – Ali007 Oct 27 '13 at 21:11
  • Does anyone know if you can do this just by using plain javascript or jQuery? – Ali007 Nov 04 '13 at 11:42
  • Crossrider wonderfully allows to create a cross browser solution, but should also have some mechanism that a developer can use to incorporate browser specific native code to take advantage of all the unique browser features. – Ali007 Nov 18 '13 at 23:20

1 Answers1

1

As far as I know, this cannot be done with current Crosssrider API !

You have to switch back to non cross-browser-plugins implementation,
so that you will implement separate extension for each browser you are targeting.

For example in Chrome extension, use chrome.windows.getAll function

chrome.windows.getAll(object getInfo, function callback)

to get all instances of windows where you can count and identify each. For example:

chrome.windows.getAll({populate : true}, function (window_list) {
    var count = window_list.length; // You now have their count
    for(var i=0; i<count; i++) {
        var myWindow = window_list[i];
        // Do whatever you want here !
    }
});

And don't forget tabs permissions in manifest !

{
  ...
  "permissions": ["tabs"],
  ...
}


And For Firefox plugin, use nsIWindowMediator class, calling its getEnumerator method

var windowMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                     .getService(Components.interfaces.nsIWindowMediator); 
var enumerator = windowMediator.getEnumerator(null); // returns an iterator of all windows
var count = 0;
while (enumerator.hasMoreElements()) {
    var myWindow = enumerator.getNext();
    count++; 
    // Do whatever you want here !
}
// You now have their count


And for Safari Extension, you can get an array of all open windows using safari.application.browserWindows For example

var count = safari.application.browserWindows.length; // You now have their count
for(var i=0; i<count; i++) {
    var myWindow = safari.application.browserWindows[i];
    // Do whatever you want here !
}
Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
  • Can you get/set any value to the browser window [i.e window.name] using plain javascript or jQuery? Anything really just to ID the windows or distinguish them from each other? – Ali007 Oct 26 '13 at 08:47
  • Thanks for your help. Interestingly you didn't mention IE... ;-) – Ali007 Oct 26 '13 at 09:18
  • 1
    You are limited to get/set values for fields which are available in each window API as gettable/settable fields (note that some are read only), so you have to check window object in each documentation and check first if the field (which is suitable for your application logic) is accessible and whether its gettable/settable (links of API documentation is provided in my answer above) – Ashraf Bashir Oct 27 '13 at 10:05
  • 1
    I didn't mention IE because there are many alternative to implement IE extensions, either by native code, by C++, by Win32 API, or by frameworks like Add-in-Express. So it depends on how you will implement it ... PS. Please mark solution as answer if this answers you question. – Ashraf Bashir Oct 27 '13 at 10:05