问题
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.
回答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 !
}
来源:https://stackoverflow.com/questions/19557199/how-to-identify-every-separate-browser-window-that-is-currently-open