Removed an unnecessary callback, improved readability

wip #1
This commit is contained in:
Tangent / Rose / Nebula Rosa 2024-10-20 22:15:51 +00:00
parent d828202fa7
commit 9cd33f531b

View File

@ -1,16 +1,11 @@
/**
* Randomly shuffle an array
* https://stackoverflow.com/a/2450976/1293256
* @param {Array} array The array to shuffle
* @return {String} The first item in the shuffled array
*/
var shuffle = function (array) {
var shuffle = function (array) {
var currentIndex = array.length; var currentIndex = array.length;
var temporaryValue, randomIndex; var temporaryValue, randomIndex;
// While there remain elements to shuffle... // While there remain elements to shuffle...
while (0 !== currentIndex) { while (0 !== currentIndex) {
// Pick a remaining element... // Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex); randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1; currentIndex -= 1;
@ -22,30 +17,28 @@ var shuffle = function (array) {
} }
return array; return array;
}; };
chrome.browserAction.onClicked.addListener(function() { chrome.browserAction.onClicked.addListener(function() {
chrome.tabs.query({}, function(tabs) { chrome.tabs.query({}, function(tabs) {
chrome.browserAction.getBadgeText({}, function(text) { let count = Math.floor(tabs.length / 2) + 1 // I don't remember why we need an extra +1 here, but it is necessary
ok = confirm("Are you sure you want to snap " + text + " tabs?") var ok = confirm("Are you sure you want to close " + count + " tabs?")
if (ok) { if (ok) {
shuffle(tabs) shuffle(tabs)
let count = Math.floor(tabs.length / 2) + 1 tabs = tabs.slice(1, count)
tabs = tabs.slice(1, count) let tab_identifiers = [];
let ids = []; for (tab in tabs) {
for (tab in tabs) { tab_identifiers.push(tabs[tab].id)
ids.push(tabs[tab].id)
}
chrome.tabs.remove(ids)
} }
}) chrome.tabs.remove(ids)
}
}) })
}) })
function updateCount() { function updateCount() {
chrome.tabs.query({}, function(tabs) { chrome.tabs.query({}, function(tabs) {
chrome.browserAction.setBadgeText({ text: "" + Math.floor(tabs.length / 2) }) let count = Math.floor(tabs.length / 2)
chrome.browserAction.setBadgeText({ text: "" + count })
}) })
} }