使用chrome扩展程序获取所有窗口中所有选项卡的URL

sac*_*024 15 url tabs google-chrome google-chrome-extension

Chrome扩展程序是否可以使用chrome扩展程序获取所有标签中的所有网址?

我使用此代码获得了当前Tab的url

chrome.tabs.getSelected(null, function(tab) {
    tabUrl = tab.url;
    alert(tabUrl);
});
Run Code Online (Sandbox Code Playgroud)

我们需要manifest.json文件中的以下权限

"permissions": [
    "tabs"
]
Run Code Online (Sandbox Code Playgroud)

我的问题是找出所有标签中的网址?

Bea*_*ist 26

你可以这样做:

chrome.windows.getAll({populate:true},function(windows){
  windows.forEach(function(window){
    window.tabs.forEach(function(tab){
      //collect all of the urls here, I will just log them instead
      console.log(tab.url);
    });
  });
});
Run Code Online (Sandbox Code Playgroud)


aan*_*dis 16

使用chrome.tabs.query方法,你也可以简单地做到,

 chrome.tabs.query({},function(tabs){     
    console.log("\n/////////////////////\n");
    tabs.forEach(function(tab){
      console.log(tab.url);
    });
 });
Run Code Online (Sandbox Code Playgroud)