How to identify every separate browser window that is currently open?

南笙酒味 提交于 2019-12-04 09:49:01

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 !
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!