forked from nickbaum/simple-window-saver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventHandlers.js
113 lines (91 loc) · 3.11 KB
/
eventHandlers.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
107
108
109
110
111
112
113
/* TAB EVENTS */
// For most tab events, we simply resave the entire window.
// While more wasteful, this makes the code much more robust.
function onTabAttached(tabId, info) {
onTabChanged(tabId, info.newWindowId);
// TODO: handle the case where this was the last tab of a saved window
}
chrome.tabs.onAttached.addListener(onTabAttached);
function onTabCreated(tab) {
onTabChanged(tab.id, tab.windowId);
}
chrome.tabs.onCreated.addListener(onTabCreated);
function onTabDetached(tabId, info) {
if (tabIdToSavedWindowId[tabId]) {
delete tabIdToSavedWindowId[tabId];
}
onTabChanged(tabId, info.oldWindowId);
}
chrome.tabs.onDetached.addListener(onTabDetached);
function onTabMoved(tabId, info) {
onTabChanged(tabId, info.windowId);
}
chrome.tabs.onMoved.addListener(onTabMoved);
function onTabRemoved(tabId, removeInfo) {
var windowId = tabIdToSavedWindowId[tabId];
delete tabIdToSavedWindowId[tabId];
isWindowClosing[windowId] = removeInfo.isWindowClosing;
onTabChanged(tabId, windowId);
}
chrome.tabs.onRemoved.addListener(onTabRemoved);
function onTabSelectionChanged(tabId, info) {
var windowId = info.windowId;
updateBadgeForTab({id: tabId, windowId: windowId});
onTabChanged(tabId, windowId);
}
chrome.tabs.onSelectionChanged.addListener(onTabSelectionChanged);
function onTabUpdated(tabId, info, tab) {
onTabChanged(tabId, tab.windowId);
}
chrome.tabs.onUpdated.addListener(onTabUpdated);
// updates a window in response to a tab event
function onTabChanged(tabId, windowId) {
if (isWindowClosing[windowId])
return;
getPopulatedWindow(windowId, function(browserWindow) {
// if the window is saved, we update it
if (windowIdToName[windowId]) {
tabIdToSavedWindowId[tabId] = windowId;
var name = windowIdToName[windowId];
var displayName = savedWindows[name].displayName;
storeWindow(browserWindow, name, displayName);
} else {
// otherwise we double check that it's not saved
for (i in closedWindows) {
var savedWindow = closedWindows[i];
if (windowsAreEqual(browserWindow, savedWindow)) {
var name = savedWindow.name;
var displayName = savedWindow.displayName;
storeWindow(browserWindow, name, displayName);
markWindowAsOpen(browserWindow);
}
}
}
if (tabId) {
updateBadgeForTab({id:tabId, windowId:windowId});
}
});
}
// given a window id, fetches the corresponding window object
// and tabs, and calls callback with the window as argument
function getPopulatedWindow(windowId, callback) {
if (!windowId) {return;}
chrome.windows.get(windowId, function(browserWindow) {
if (!browserWindow) {return;}
chrome.tabs.getAllInWindow(windowId, function(tabs) {
if (!tabs) {return;}
browserWindow.tabs = tabs;
callback(browserWindow);
});
});
}
/* WINDOW EVENTS */
function onWindowRemoved(windowId) {
var windowName = windowIdToName[windowId];
if (windowName) {
var savedWindow = savedWindows[windowName];
markWindowAsClosed(savedWindow);
}
delete isWindowClosing[windowId];
}
chrome.windows.onRemoved.addListener(onWindowRemoved);