本文介绍了有没有办法计算Chrome中打开的标签数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图找到一种方法来计算当前在Chrome中通过javascript打开的多个标签。

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.

Chrome不再支持它,还是只能用于扩展程序开发?

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

推荐答案

由于,是Chrome扩展程序API,仅限于此可用于扩展,而不是网页JavaScript。实际上,它仅在扩展的后台上下文中可用(即不是内容脚本)。

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.

这篇关于有没有办法计算Chrome中打开的标签数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:18