-
Notifications
You must be signed in to change notification settings - Fork 16
/
context.js
106 lines (98 loc) · 3.17 KB
/
context.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
(c) Copyright 2009 iOpus Software GmbH - http://www.iopus.com
*/
// Context to store browser window-specific information
var context = {
init: function(win_id) {
this.attachListeners();
context[win_id] = new Object();
context[win_id].mplayer = new MacroPlayer(win_id);
context[win_id].recorder = new Recorder(win_id);
context[win_id].state = "idle";
},
updateState: function(win_id, state) {
// set browser action icon
switch(state) {
case "playing": case "recording":
badge.setIcon(win_id, "skin/stop.png");
break;
case "paused":
// TODO: switch to tab where replaying was paused
// after unpause
badge.setIcon(win_id, "skin/play.png");
break;
case "idle":
badge.setIcon(win_id, "skin/logo19.png");
if (Storage.getBool("show-updated-badge")) {
badge.setText(win_id, "New");
} else {
badge.clearText(win_id);
}
break;
}
// update panel
var panel = this[win_id].panelWindow;
if (panel && !panel.closed)
panel.updatePanel(state);
this[win_id].state = state;
},
onCreated: function (w) {
if (w.type != "normal")
return;
context[w.id] = new Object();
context[w.id].mplayer = new MacroPlayer(w.id);
context[w.id].recorder = new Recorder(w.id);
this.updateState(w.id, "idle");
},
onRemoved: function (id) {
if (context[id]) {
var t;
if (t = context[id].mplayer) {
t.terminate();
delete context[id].mplayer;
}
if (t = context[id].recorder) {
if (t.recording)
t.stop();
delete context[id].recorder;
}
if (context[id].dockInterval) {
clearInterval(context[id].dockInterval);
context[id].dockInterval = null;
}
delete context[id];
}
},
onTabUpdated: function(tab_id, changeInfo, tab) {
if (!context[tab.windowId])
return;
// set icon after tab is updated
switch (context[tab.windowId].state) {
case "playing": case "recording":
badge.setIcon(tab.windowId, "skin/stop.png");
break;
case "paused":
badge.setIcon(tab.windowId, "skin/play.png");
break;
case "idle":
badge.setIcon(tab.windowId, "skin/logo19.png");
if (Storage.getBool("show-updated-badge")) {
badge.setText(tab.windowId, "New");
} else {
badge.clearText(tab.windowId);
}
break;
}
},
attachListeners: function() {
chrome.windows.onCreated.addListener(
context.onCreated.bind(context)
);
chrome.windows.onRemoved.addListener(
context.onRemoved.bind(context)
);
chrome.tabs.onUpdated.addListener(
context.onTabUpdated.bind(context)
);
},
};