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

后端 未结 4 689
心在旅途
心在旅途 2021-01-13 04:18

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

4条回答
  •  隐瞒了意图╮
    2021-01-13 05:12

    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.

提交回复
热议问题