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