-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
65 lines (55 loc) · 1.54 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
const CREATE_ATTRIBUTES = [
"focused",
"height",
"incognito",
"left",
"state",
"top",
"type",
"width",
];
let lastKnownWindowState = {};
function maybeRememberWindowState() {
chrome.windows.getAll({windowTypes: ["normal"]}, __maybeRememberWindowState);
}
function __maybeRememberWindowState(windows) {
for (const window of windows) {
if (window.focused) {
lastKnownWindowState = {};
for (const attribute of CREATE_ATTRIBUTES) {
lastKnownWindowState[attribute] = window[attribute];
}
}
}
}
function __isSpecialState(windowState) {
return [
"fullscreen",
"maximized",
"minimized",
].indexOf(windowState) != -1;
}
function handleRemove(_tabId, removeInfo) {
if (removeInfo.isWindowClosing) {
return;
}
chrome.tabs.query({}, __maybeRestoreWindow);
}
function __maybeRestoreWindow(tabs) {
if (tabs.length > 0) {
return;
}
const targetState = lastKnownWindowState.state;
if (__isSpecialState(targetState)) {
lastKnownWindowState.state = "normal";
chrome.windows.create(lastKnownWindowState, __applyState(targetState));
} else {
chrome.windows.create(lastKnownWindowState);
}
}
function __applyState(state) {
return (window) => chrome.windows.update(window.id, {state});
}
chrome.windows.onFocusChanged.addListener(maybeRememberWindowState);
chrome.tabs.onActivated.addListener(maybeRememberWindowState);
chrome.tabs.onRemoved.addListener(handleRemove);