开发者

Count the number of windows open

开发者 https://www.devze.com 2023-01-24 09:30 出处:网络
I am trying to count the number of Tabs that are open in my google chrome browswer with javascript. Does anyone know how to do this?

I am trying to count the number of Tabs that are open in my google chrome browswer with javascript. Does anyone know how to do this?

I wrote some javascript that I want repeated 10 times and then stop. Upon the completion of 1 iteration, I open a new window using:

window.open("http://www.test.com");

I want to do this 10 times than stop. Mayb开发者_开发知识库e there is a better way than what I am thinking...


It's a good thing that webpages are sandboxed so that other websites can't access them. If they're windows that you've opened using window.open you can save the reference you receive to the window:

var win = window.open(url);

of course you could push this to an array if you're opening a large number of windows.

var wins = [];
//looping stuff here
wins.push(window.open(url[i]);


I think the loop is just fine, but if you want to keep track,

var winList = new Array();
var count = 10;

for(var i=0; i < count; i++){
    winList[i] = window.open("http://www.test.com");
}

This way, you can keep references to your windows.

hth


So I couldn't figure out a way doing what I wanted so I took a different approach. I said, lets see if google has the option in chrome to limit the number of tabs opened and I found someone wrote an extension to do exactly that. I don't know how he did but it definitely works. Controlled multi-tab browsing


Look at the Google Chrome Extensions Developer Guide , in particular the Tabs page and the getAllInWindow function

chrome.tabs.getAllInWindow(integer windowId, function callback)

Where the callback function receives an array of tabs. Meaning you can get its length.

And if the tabs you want to keep track of are possibly in different windows, then you need to look at the Windows page and the getAll function

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

Use this to iterate over all windows and call getAllInWindow. And you're all set.


If all you want to do is open ten tabs:

var i;
for (i = 0; i < 10; i++) {
    window.open("http://www.test.com");
}

But no, I don't believe you can count the number of open tabs since that could disclose information to websites that you may not want them knowing. (Do you want random websites you visit to know how many tabs you have open?)

0

精彩评论

暂无评论...
验证码 换一张
取 消