Browse Source

Upload files to ''

Tangent 5 years ago
parent
commit
e33c40bc2c
3 changed files with 83 additions and 0 deletions
  1. 65
    0
      background.js
  2. BIN
      gauntlet.png
  3. 18
    0
      manifest.json

+ 65
- 0
background.js View File

@@ -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()

BIN
gauntlet.png View File


+ 18
- 0
manifest.json View File

@@ -0,0 +1,18 @@
1
+{
2
+  "manifest_version": 2,
3
+  "name": "The Snap",
4
+  "description": "Too many tabs open? Halve your workload.",
5
+  "version": "1.0",
6
+  "icons": {
7
+    "16": "images/gauntlet16.png",
8
+    "32": "images/gauntlet32.png",
9
+    "48": "images/gauntlet48.png",
10
+    "128": "images/gauntlet128.png"
11
+  },
12
+  "browser_action": {},
13
+  "permissions": [ "tabs" ],
14
+  "background": {
15
+    "scripts": [ "background.js" ],
16
+    "persistent": false
17
+  }
18
+}