-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
42 lines (34 loc) · 1.09 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const percentElement = document.getElementById('percent');
const tidyElement = document.getElementById('tidy');
let percent;
tidyElement.onclick = () => {
percent = percentElement.value;
setPercent(percent);
chrome.tabs.query({currentWindow: true}, tidyUp);
}
tidyUp = (tabs) => {
const numTabsToClose = Math.floor(percent / 100 * tabs.length);
const shuffledTabIds = shuffleTabIds(tabs);
const tabsToClose = takeFirstN(shuffledTabIds, numTabsToClose);
closeTabs(tabsToClose);
}
shuffleTabIds = (tabs) => {
return tabs
.map(tab => { return { id: tab.id, random: Math.random() }; } )
.sort((id1, id2) => id1.random - id2.random)
.map(tabIds => tabIds.id);
}
takeFirstN = (array, n) => array.slice(0, n);
closeTabs = (ids) => chrome.tabs.remove(ids);
getPercent = () => {
chrome.storage.local.get('percent', (data) => {
percent = data.percent;
percentElement.value = percent;
});
}
setPercent = (percent) => {
chrome.storage.local.set({percent}, function() {
console.log('Percent has been set.');
});
}
getPercent();