Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to count number of tabs are opened in chrome?

I am trying to find a way to count a number of tabs that are currently open in Chrome by javascript.

I have searched and found chrome.tabs.query(). But when I opened my console and tried it I got an undefined message.

Is it not supported anymore by Chrome, or can it only be used in extension development?

like image 469
Ho Ha Avatar asked Feb 08 '17 09:02

Ho Ha


People also ask

How do I count the number of open tabs in Chrome?

To find the number of tabs that are open, you could do something like: chrome. tabs. query({windowType:'normal'}, function(tabs) { console.

How do I check how many tabs I have?

Just simply, enable Talkback mode in accessibility features. Then when you hover (if you have Bluetooth mouse enabled) or tap the tab icon (with your finger) (the :D), it will tell you how many tabs you have open. Like on mine, it said 713 tabs open.

How many total tabs can you open in Chrome?

Chrome doesn't have an encoded limit of tabs that it can have open. It is a very resource heavy browser that is notorious for memory usage.

What is the record for how many tabs open?

3333 (world record) tabs open in chrome.


3 Answers

As wscourge has implied, chrome.tabs.query() is a Chrome extension API, which is only available to extensions, not web page JavaScript. In fact, it is only available in the background context of an extension (i.e. not content scripts).

To find the number of tabs that are open, you could do something like:

chrome.tabs.query({windowType:'normal'}, function(tabs) {
    console.log('Number of open tabs in all normal browser windows:',tabs.length);
}); 

If you want to run this from a console, you will need to have an extension loaded that has a background page. You will then need to open the console for the background page. From that console, you can execute the above code.

like image 126
Makyen Avatar answered Oct 19 '22 04:10

Makyen


I found the answer to this question here: https://superuser.com/questions/967064/how-to-get-tab-count-in-chrome-desktop-without-app-extension

Go to chrome://inspect/#pages

Run the following line of code in the javascript console:

document.getElementById("pages-list").childElementCount

The tabs count will be printed to the console.

like image 7
unubar Avatar answered Oct 19 '22 03:10

unubar


Local and Session storage

First on page load (open tab) event we generate tab hash and we save it in sessionStorage (not shared between tabs) and as key in TabsOpen object in localStorage (which is shared between tabs). Then in event page unload (close tab) we remove current tab hash (saved in sesionStorage) from TabsOpen in localStorage.

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>My project</title>
    ...
    <script>

        function tabLoadEventHandler() {
            let hash = 'tab_' + +new Date();
            sessionStorage.setItem('TabHash',hash);
            let tabs = JSON.parse(localStorage.getItem('TabsOpen')||'{}');
            tabs[hash]=true;
            localStorage.setItem('TabsOpen',JSON.stringify(tabs));
        }
        function tabUnloadEventHandler() {
            let hash= sessionStorage.getItem('TabHash');
            let tabs = JSON.parse(localStorage.getItem('TabsOpen')||'{}');
            delete tabs[hash];
            localStorage.setItem('TabsOpen',JSON.stringify(tabs));
        }
    </script>
    ...
</head>

<body onunload="tabUnloadEventHandler()" onload="tabLoadEventHandler()">
...
</body>

</html>

Thanks to this in TabsOpen object in localStorage we have information about current open tabs which can be read by

let tabsCount = Object.keys( JSON.parse(localStorage.getItem('TabsOpen')||'{}') ).length
like image 7
Kamil Kiełczewski Avatar answered Oct 19 '22 03:10

Kamil Kiełczewski