Skip to content

Instantly share code, notes, and snippets.

@KhodeN
Created November 13, 2014 00:24
Show Gist options
  • Save KhodeN/bdb5e5125836bea3404b to your computer and use it in GitHub Desktop.
Save KhodeN/bdb5e5125836bea3404b to your computer and use it in GitHub Desktop.
closeDubTabs
var getWindows = new Promise(function(resolve) {
chrome.windows.getAll(function(windows) {
resolve(windows);
});
});
var getTabs = function(window) {
var p = new Promise(function(resolve) {
chrome.tabs.getAllInWindow(window.id, function(tabs) {
resolve(tabs);
});
});
return p;
};
var closeTab = function(tab){
var p = new Promise(function(resolve){
chrome.tabs.remove(tab.id, function(){
resolve();
});
});
return p;
}
getWindows
.then(function(windows) {
return Promise.all(windows.map(getTabs));
})
.then(function(tabGroups) {
return Array.prototype.concat.apply([], tabGroups);
})
.then(function(allTabs) {
var urls = {};
var toClose = [];
allTabs.forEach(function(tab) {
if (urls.hasOwnProperty(tab.url)) {
toClose.push(tab);
} else {
urls[tab.url] = true;
}
});
return toClose;
})
.then(function (toCloseTabs){
return Promise.all(toCloseTabs.map(closeTab));
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment