javascript 检索 Chrome 中打开的标签页?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11915370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:44:25  来源:igfitidea点击:

Retrieving which tabs are open in Chrome?

javascripttabsgoogle-chrome-extension

提问by Ray

Is there a way to retrieve all the tabs open and sort them in to an array in Chrome? So if Gmail and YouTube were open, there would be two entries in the array entitled "gmail.com" and "youtube.com".

有没有办法检索所有打开的选项卡并将它们排序到 Chrome 中的数组中?因此,如果 Gmail 和 YouTube 是开放的,则数组中将有两个条目,名为“gmail.com”和“youtube.com”。

采纳答案by bpatel

Yes, here is how you can do this:

是的,您可以这样做:

Note:this requires permission "tabs" to be specified in your manifest file.

注意:这需要在清单文件中指定权限“tabs”。

chrome.windows.getAll({populate:true}, getAllOpenWindows);

function getAllOpenWindows(winData) {

  var tabs = [];
  for (var i in winData) {
    if (winData[i].focused === true) {
        var winTabs = winData[i].tabs;
        var totTabs = winTabs.length;
        for (var j=0; j<totTabs;j++) {
          tabs.push(winTabs[j].url);
        }
    }
  }
  console.log(tabs);
}

In this example I am just adding tab url as you asked in an array but each "tab" object contains a lot more information. Url will be the full URL you can apply some regular expression to extract the domain names from the URL.

在此示例中,我只是按照您在数组中的要求添加选项卡 url,但每个“选项卡”对象都包含更多信息。Url 将是完整的 URL,您可以应用一些正则表达式从 URL 中提取域名。

回答by Oliver Spryn

Unless you are building a plugin, there isn't a way that I know of to retrieve all of the names of the open tabs, especially if the tabs contain content from separate domains. If you were able to do such a thing, it could be quite a security issue!

除非您正在构建插件,否则我知道要检索打开选项卡的所有名称,尤其是如果选项卡包含单独的域中的内容。如果你能够做这样的事情,这可能是一个相当大的安全问题!

You can check the Chrome documentation here: http://developer.chrome.com/extensions/devguide.html

您可以在此处查看 Chrome 文档:http: //developer.chrome.com/extensions/devguide.html