|
@@ -0,0 +1,65 @@
|
|
1
|
+/**
|
|
2
|
+ * Randomly shuffle an array
|
|
3
|
+ * https://stackoverflow.com/a/2450976/1293256
|
|
4
|
+ * @param {Array} array The array to shuffle
|
|
5
|
+ * @return {String} The first item in the shuffled array
|
|
6
|
+ */
|
|
7
|
+var shuffle = function (array) {
|
|
8
|
+
|
|
9
|
+ var currentIndex = array.length;
|
|
10
|
+ var temporaryValue, randomIndex;
|
|
11
|
+
|
|
12
|
+ // While there remain elements to shuffle...
|
|
13
|
+ while (0 !== currentIndex) {
|
|
14
|
+ // Pick a remaining element...
|
|
15
|
+ randomIndex = Math.floor(Math.random() * currentIndex);
|
|
16
|
+ currentIndex -= 1;
|
|
17
|
+
|
|
18
|
+ // And swap it with the current element.
|
|
19
|
+ temporaryValue = array[currentIndex];
|
|
20
|
+ array[currentIndex] = array[randomIndex];
|
|
21
|
+ array[randomIndex] = temporaryValue;
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ return array;
|
|
25
|
+
|
|
26
|
+};
|
|
27
|
+
|
|
28
|
+chrome.browserAction.onClicked.addListener(function() {
|
|
29
|
+ chrome.tabs.query({}, function(tabs) {
|
|
30
|
+ chrome.browserAction.getBadgeText({}, function(text) {
|
|
31
|
+ ok = confirm("Are you sure you want to snap " + text + " tabs?")
|
|
32
|
+ if (ok) {
|
|
33
|
+ shuffle(tabs)
|
|
34
|
+ let count = Math.floor(tabs.length / 2) + 1
|
|
35
|
+ tabs = tabs.slice(1, count)
|
|
36
|
+ let ids = [];
|
|
37
|
+ for (tab in tabs) {
|
|
38
|
+ ids.push(tabs[tab].id)
|
|
39
|
+ }
|
|
40
|
+ chrome.tabs.remove(ids)
|
|
41
|
+ }
|
|
42
|
+ })
|
|
43
|
+ })
|
|
44
|
+})
|
|
45
|
+
|
|
46
|
+function updateCount() {
|
|
47
|
+ chrome.tabs.query({}, function(tabs) {
|
|
48
|
+ chrome.browserAction.setBadgeText({ text: "" + Math.floor(tabs.length / 2) })
|
|
49
|
+ })
|
|
50
|
+}
|
|
51
|
+
|
|
52
|
+chrome.tabs.onCreated.addListener(function() {
|
|
53
|
+ updateCount()
|
|
54
|
+})
|
|
55
|
+chrome.tabs.onRemoved.addListener(function() {
|
|
56
|
+ updateCount()
|
|
57
|
+})
|
|
58
|
+chrome.windows.onCreated.addListener(function() {
|
|
59
|
+ updateCount()
|
|
60
|
+})
|
|
61
|
+chrome.windows.onRemoved.addListener(function() {
|
|
62
|
+ updateCount()
|
|
63
|
+})
|
|
64
|
+
|
|
65
|
+updateCount()
|