سؤال

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. Maybe there is a better way than what I am thinking...

هل كانت مفيدة؟

المحلول 3

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

نصائح أخرى

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

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]);

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?)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top