-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathprevent_closeBrowser_lastTab.js
307 lines (293 loc) · 7.88 KB
/
prevent_closeBrowser_lastTab.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
export default {
icon: '<i class="fa-regular fa-window-restore fa-lg"></i>',
name: {
en: "Don't close browser with last tab",
vi: "Không tắt trình duyệt khi tắt tab cuối",
},
description: {
en: `Prevent closing of Browser after close the last tab<br/>
Auto create new empty tab if no tab left`,
vi: `Không tắt trình duyệt khi tắt tab cuối cùng<br/>
Tự động mở tab mới khi bạn tắt tab cuối cùng`,
img: "",
},
changeLogs: {
"2024-05-22": "init",
},
popupScript: {
onEnable: () => {
chrome.runtime.sendMessage({ action: enableEvent });
},
onDisable: () => {
chrome.runtime.sendMessage({ action: disableEvent });
},
},
backgroundScript: {
runtime: {
onMessage: async ({ request, sender, sendResponse }, context) => {
if (request.action === enableEvent) {
onEnable();
}
if (request.action === disableEvent) {
onDisable();
}
},
},
tabs: {
onRemoved: (details, context) => {
handleEvent("onRemoved");
},
onUpdated: (details, context) => {
handleEvent("onUpdated");
},
onAttached: (details, context) => {
handleEvent("onAttached");
},
onActivated: (details, context) => {
handleEvent("onActivated");
},
onMoved: (details, context) => {
handleEvent("onMoved");
},
},
},
};
const enableEvent = "prevent_closeBrowser_lastTab_enabled";
const disableEvent = "prevent_closeBrowser_lastTab_disabled";
// https://chromewebstore.google.com/detail/dont-close-window-with-la/dlnpfhfhmkiebpnlllpehlmklgdggbhn
// ===================== bg script context =====================
let newTabUrl = "chrome://newtab/";
let working = false;
let handling = false;
let first_window_id = null;
let pinned_tab_id = null;
// config
let single_new_tab = false;
let new_tab_last = false;
let first_window = true;
let every_window = false;
let debug = false;
function isNewTabPage(url) {
url = url.trim();
return (
url == newTabUrl ||
url == "edge://newtab/" ||
(url.indexOf("/_/chrome/newtab?") !== -1 && url.indexOf("google.") !== -1)
);
}
async function onEnable() {
first_window_id = null;
pinned_tab_id = null;
await handleEvent("onEnable");
}
async function onDisable() {
if (pinned_tab_id) await chrome.tabs.remove(pinned_tab_id);
}
async function handleEvent(type) {
if (handling) return;
handling = true;
if (debug) console.log("handleEvent " + type);
let wasWorking = false;
let windows = await chrome.windows.getAll({
populate: true,
windowTypes: ["normal"],
});
for (let win of windows) {
let newTabs = await chrome.tabs.query({
windowId: win.id,
url: newTabUrl,
pinned: false,
});
let pinnedTabs = await chrome.tabs.query({
windowId: win.id,
pinned: true,
});
if (win.tabs === undefined || win.tabs[0] === undefined) return;
if (first_window_id == null) first_window_id = win.id;
let firstTab = await chrome.tabs.get(win.tabs[0].id);
// prevent activating first pinned tab
if (win.tabs.length == 2) {
if (
win.tabs.length == 2 &&
firstTab.active &&
firstTab.pinned &&
isNewTabPage(firstTab.url) &&
!working
) {
working = true;
if (debug) console.log("activate tab 1");
try {
await chrome.tabs.update(win.tabs[1].id, { active: true });
wasWorking = true;
} catch (error) {
setTimeout(function () {
handleEvent("redo");
}, 50);
}
working = false;
}
}
// create the single pinned tab if there's none
if (
(every_window ||
windows.length == 1 ||
(first_window && first_window_id == win.id)) &&
win.tabs.length < 2 &&
pinnedTabs.length < 1 &&
!working
) {
working = true;
if (debug) console.log("creating pinned tab");
try {
let tab = await chrome.tabs.create({
windowId: win.id,
index: 0,
pinned: true,
active: false,
url: newTabUrl,
});
pinned_tab_id = tab.id;
first_window_id = win.id;
wasWorking = true;
} catch (error) {
setTimeout(function () {
handleEvent("redo");
}, 700);
}
working = false;
}
// open new tab if there's only single pinned tab
if (
(every_window ||
windows.length == 1 ||
(first_window && first_window_id == win.id)) &&
win.tabs.length == 1 &&
pinnedTabs.length == 1 &&
!working
) {
working = true;
if (debug) console.log("creating tab");
try {
await chrome.tabs.create({
windowId: win.id,
url: newTabUrl,
});
first_window_id = win.id;
wasWorking = true;
} catch (error) {
setTimeout(function () {
handleEvent("redo");
}, 700);
}
working = false;
}
// remove pinned tab if there's enough open tabs
if (
(every_window ||
windows.length == 1 ||
(first_window && first_window_id == win.id)) &&
win.tabs.length > 2 &&
pinnedTabs.length < win.tabs.length &&
pinnedTabs.length >= 1 &&
isNewTabPage(firstTab.url) &&
!working
) {
working = true;
if (debug) console.log("removing pinned tab 1");
try {
await chrome.tabs.remove(pinnedTabs[0].id);
pinned_tab_id = null;
wasWorking = true;
} catch (error) {
setTimeout(function () {
handleEvent("redo");
}, 700);
}
working = false;
}
// unpin single pinned tab if there's another window
if (
!every_window &&
windows.length > 1 &&
!(first_window && first_window_id == win.id) &&
win.tabs.length == 1 &&
pinnedTabs.length == 1 &&
isNewTabPage(firstTab.url) &&
!working
) {
working = true;
if (debug) console.log("unpin pinned tab");
try {
await chrome.tabs.update(pinnedTabs[0].id, {
pinned: false,
});
wasWorking = true;
} catch (error) {
console.log(error);
}
working = false;
}
// remove pinned tab if 1st window has at least one regular page and new window is openning (ctrl+n)
if (
!every_window &&
windows.length > 1 &&
!(first_window && first_window_id == win.id) &&
win.tabs.length == 2 &&
pinnedTabs.length == 1 &&
isNewTabPage(firstTab.url) &&
!working
) {
working = true;
if (debug) console.log("removing pinned tab 2");
try {
await chrome.tabs.remove(pinnedTabs[0].id);
wasWorking = true;
} catch (error) {
console.log(error);
}
working = false;
}
// allow single new tab page
if (
single_new_tab &&
newTabs.length > 1 &&
/*windowPinnedTabs.length == 0 &&*/ !working
) {
working = true;
if (debug) console.log("removing tab");
try {
await chrome.tabs.remove(newTabs[0].id);
wasWorking = true;
} catch (error) {
console.log(error);
}
working = false;
}
// prevent blank new tab page(s) before actual tabs with loaded pages
if (
new_tab_last &&
pinnedTabs.length == 0 &&
newTabs.length >= 1 &&
win.tabs[win.tabs.length - 1].id != newTabs[0].id &&
!working
) {
working = true;
if (debug) console.log("removing tab 2");
try {
await chrome.tabs.remove(newTabs[0].id);
wasWorking = true;
} catch (error) {
setTimeout(function () {
handleEvent("redo");
}, 700);
}
working = false;
}
}
if (debug) console.log("end handleEvent");
handling = false;
if (wasWorking)
setTimeout(function () {
handleEvent("redo");
}, 50);
}