2013-04-24 112 views
10

這是可能的擴展鉻使用擴展名獲取所有選項卡中的所有URL?使用Chrome擴展獲取所有窗口中所有選項卡的網址

我有當前選項卡的使用此代碼

chrome.tabs.getSelected(null, function(tab) { 
    tabUrl = tab.url; 
    alert(tabUrl); 
}); 

的URL,我們需要在manifest.json檔案以下權限

"permissions": [ 
    "tabs" 
] 

我的問題是要找出所有選項卡中的URL ?

回答

18

你可以做這樣的事情:

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); 
    }); 
    }); 
}); 
+0

我是否需要額外的許可,這裏的窗戶? – sachinjain024 2013-04-24 07:55:22

+0

@blunderboy你需要'tabs'權限。 – BeardFist 2013-04-24 08:11:05

+0

@BeardFist是'windows.forEach(...)'異步? – MikeG 2016-12-04 05:30:30

12

隨着chrome.tabs.query方法,你也可以簡單地做,

chrome.tabs.query({},function(tabs){  
    console.log("\n/////////////////////\n"); 
    tabs.forEach(function(tab){ 
     console.log(tab.url); 
    }); 
}); 
相關問題