Created
November 13, 2014 00:24
-
-
Save KhodeN/bdb5e5125836bea3404b to your computer and use it in GitHub Desktop.
closeDubTabs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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