-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
50 lines (42 loc) · 1.21 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
// Global accessor that the popup uses.
var eventsOfTabs = {};
var selectedEvents = null;
var selectedId = null;
function updateEvents(tabId) {
if(! eventsOfTabs[tabId])
eventsOfTabs[tabId] = [];
}
function updateSelected(tabId) {
selectedEvents = eventsOfTabs[tabId];
if (selectedEvents != null) {
chrome.pageAction.show(tabId);
var newTitle = selectedEvents.length + " events on MessageBus";
chrome.pageAction.setTitle({tabId:tabId, title: newTitle});
}
}
chrome.tabs.onUpdated.addListener(function(tabId, change, tab) {
if (change.status == "complete") {
updateEvents(tabId);
}
});
chrome.tabs.onSelectionChanged.addListener(function(tabId, info) {
selectedId = tabId;
updateSelected(tabId);
});
// Ensure the current selected tab is set up.
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
updateEvents(tabs[0].id);
});
chrome.runtime.onConnect.addListener(function(port) {
console.assert(port.name == "events");
var tabId = port.sender.tab.id;
port.onMessage.addListener(function(msg) {
if(! eventsOfTabs[tabId])
eventsOfTabs[tabId] = [];
msg.date = new Date();
eventsOfTabs[tabId].unshift(msg);
if(selectedId == tabId) {
updateSelected(tabId);
}
});
});