This repository was archived by the owner on Mar 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbackground.js
72 lines (64 loc) · 1.76 KB
/
background.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"use strict";
function Counter(name, listener) {
this.count = 0;
this.name = name;
this.listener = listener;
}
Counter.prototype.setCount = function(count) {
this.count = count;
this.listener(this);
};
Counter.prototype.increment = function() {
this.setCount(this.count+1);
};
Counter.prototype.decrement = function() {
this.setCount(this.count-1);
};
function TabCounter(listener) {
Counter.call(this,"Tabs",listener);
var self = this;
chrome.tabs.query({}, function(tabs) {
self.setCount(tabs.length);
});
chrome.tabs.onCreated.addListener(self.increment.bind(self));
chrome.tabs.onRemoved.addListener(self.decrement.bind(self));
}
TabCounter.prototype = new Counter();
function WindowCounter(listener) {
Counter.call(this,"Windows",listener);
var self = this;
chrome.windows.getAll(function(windows) {
self.setCount(windows.length);
});
chrome.windows.onCreated.addListener(self.increment.bind(self));
chrome.windows.onRemoved.addListener(self.decrement.bind(self));
}
WindowCounter.prototype = new Counter();
function onUpdate() {
var text = "",
title = mode;
if(mode !== "Windows") {
text += counters.Tabs.count;
}
if(mode === "Both") {
text += "/";
title = "Tabs over Windows";
}
if(mode !== "Tabs") {
text += counters.Windows.count;
}
chrome.browserAction.setBadgeText({text: text});
chrome.browserAction.setTitle({title: title});
}
const counters = {
"Tabs": new TabCounter(onUpdate),
"Windows": new WindowCounter(onUpdate)
}
const modes = Object.keys(counters).concat(["Both"]);
var mode = modes[0];
chrome.browserAction.onClicked.addListener(function() {
const nextIndex = (modes.indexOf(mode)+1)%modes.length;
mode = modes[nextIndex];
onUpdate();
});
onUpdate();