Skip to content

Commit ee95c43

Browse files
committed
done prevent close browser on last tab
1 parent 3aed9b1 commit ee95c43

File tree

2 files changed

+84
-110
lines changed

2 files changed

+84
-110
lines changed

scripts/chongLuaDao.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export default {
2727
const { Storage } = await import("./helpers/utils.js");
2828
Storage.remove(KEYS.blackList);
2929
Storage.remove(KEYS.whiteList);
30-
3130
chrome.runtime.sendMessage({ action: KEYS.clearCache });
3231
},
3332
},
@@ -42,10 +41,8 @@ export default {
4241

4342
backgroundScript: {
4443
onDocumentStart: async (details, context) => {
45-
let cached = context.getCache("chongLuaDao");
46-
if (!cached) {
47-
cached = await saveBgCache(context);
48-
}
44+
if (!cached) await saveBgCache();
45+
if (!cached) return;
4946

5047
let whiteListed = matchOneOfPatterns(details.url, cached.whiteList);
5148
if (whiteListed) {
@@ -92,16 +89,16 @@ export default {
9289

9390
runtime: {
9491
onInStalled: (reason, context) => {
95-
saveBgCache(context);
92+
saveBgCache();
9693
},
9794
onStartup: (nil, context) => {
98-
saveBgCache(context);
95+
saveBgCache();
9996
},
10097
onMessage: ({ request, sender, sendResponse }, context) => {
10198
if (request.action === KEYS.saveCache) {
102-
saveBgCache(context);
99+
saveBgCache();
103100
} else if ((request.action = KEYS.clearCache)) {
104-
clearBgCache(context);
101+
clearBgCache();
105102
}
106103
},
107104
},
@@ -115,29 +112,32 @@ const KEYS = {
115112
clearCache: "ufs-chongLuaDao-clearCache",
116113
};
117114

118-
function getStorage(key) {
119-
return chrome.storage.local.get([key]).then((data) => data?.[key] || []);
115+
function getStorage(key, defaultValue = null) {
116+
return chrome.storage.local
117+
.get([key])
118+
.then((data) => data?.[key] ?? defaultValue);
120119
}
121120

122-
async function saveBgCache(context) {
121+
// ===================== bg script context =====================
122+
let cached = null;
123+
124+
async function saveBgCache() {
123125
try {
124-
const cached = {
125-
blackList: await context.utils.Storage.get(KEYS.blackList, []),
126-
whiteList: await context.utils.Storage.get(KEYS.whiteList, []),
126+
cached = {
127+
blackList: await getStorage(KEYS.blackList, []),
128+
whiteList: await getStorage(KEYS.whiteList, []),
127129
};
128-
console.log("cached chongLuaDao", cached);
129-
context.setCache("chongLuaDao", cached);
130-
return cached;
131130
} catch (e) {
132131
console.log("cache chongLuaDao FAIL ", e);
133132
}
134133
return null;
135134
}
136135

137-
function clearBgCache(context) {
138-
context.removeCache("chongLuaDao");
136+
function clearBgCache() {
137+
cached = null;
139138
}
140139

140+
// ===================== popup script context =====================
141141
async function onEnable() {
142142
const { t } = await import("../popup/helpers/lang.js");
143143
const { showLoading, Storage } = await import("./helpers/utils.js");
@@ -218,6 +218,7 @@ async function onEnable() {
218218
return true;
219219
}
220220

221+
// ===================== content script context =====================
221222
async function analyzeWeb(onlyShowUIIfNotSafe = false) {
222223
const href = window.location.href,
223224
hostname = window.location.hostname,

scripts/prevent_closeBrowser_lastTab.js

Lines changed: 63 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,54 @@ export default {
1818
"2024-05-22": "init",
1919
},
2020

21+
popupScript: {
22+
onEnable: () => {
23+
chrome.runtime.sendMessage({ action: enableEvent });
24+
},
25+
},
26+
2127
backgroundScript: {
28+
runtime: {
29+
onMessage: ({ request, sender, sendResponse }, context) => {
30+
if (request.action === enableEvent) {
31+
handleEvent("onEnable");
32+
}
33+
},
34+
},
2235
tabs: {
2336
onRemoved: (details, context) => {
24-
handleEvent("onRemoved", context);
37+
handleEvent("onRemoved");
2538
},
2639
onUpdated: (details, context) => {
27-
handleEvent("onUpdated", context);
40+
handleEvent("onUpdated");
2841
},
2942
onAttached: (details, context) => {
30-
handleEvent("onAttached", context);
43+
handleEvent("onAttached");
3144
},
3245
onActivated: (details, context) => {
33-
handleEvent("onActivated", context);
46+
handleEvent("onActivated");
3447
},
3548
onMoved: (details, context) => {
36-
handleEvent("onMoved", context);
49+
handleEvent("onMoved");
3750
},
3851
},
3952
},
4053
};
4154

42-
const newTabUrl = "chrome://newtab/";
43-
const rootKey = "prevent_closeBrowser_lastTab";
44-
const handlingKey = rootKey + ".handling";
45-
const lastActiveTabKey = rootKey + ".lastActiveTab";
46-
const pinnedTabKey = rootKey + ".pinnedTab";
55+
const enableEvent = "prevent_closeBrowser_lastTab_enabled";
56+
57+
// https://chromewebstore.google.com/detail/dont-close-window-with-la/dlnpfhfhmkiebpnlllpehlmklgdggbhn
58+
// ===================== bg script context =====================
59+
60+
var newTabUrl = "chrome://newtab/";
61+
var working = false;
62+
var handling = false;
63+
var first_window_id = null;
64+
var single_new_tab = false;
65+
var new_tab_last = false;
66+
var first_window = true;
67+
var every_window = false;
68+
var debug = false;
4769

4870
function isNewTabPage(url) {
4971
url = url.trim();
@@ -54,55 +76,7 @@ function isNewTabPage(url) {
5476
);
5577
}
5678

57-
async function handleEvent(type, context) {
58-
if (context.getCache(handlingKey, false)) return;
59-
context.setCache(handlingKey, true);
60-
61-
let pinnedTabId = context.getCache(pinnedTabKey);
62-
let lastActiveTabId = context.getCache(lastActiveTabKey);
63-
64-
let tabs = await chrome.tabs.query({});
65-
let pinnedTab = tabs.find((tab) => tab.id == pinnedTabId);
66-
console.log("handleEvent", type, tabs, pinnedTab);
67-
68-
// prevent activating first pinned tab
69-
let activeTab = tabs.find((tab) => tab.active);
70-
if (activeTab?.id == pinnedTab.id) {
71-
await chrome.tabs.update(lastActiveTabId, {
72-
active: true,
73-
});
74-
} else {
75-
context.setCache(lastActiveTabKey, activeTab?.id);
76-
}
77-
78-
// create the single pinned tab if there's none
79-
if (!pinnedTab && tabs.length == 1) {
80-
let pinned = await chrome.tabs.create({
81-
url: newTabUrl,
82-
pinned: true,
83-
active: false,
84-
});
85-
context.setCache(pinnedTabKey, pinned.id);
86-
}
87-
88-
// open new tab if there's only single pinned tab
89-
if (pinnedTab && tabs.length == 1) {
90-
await chrome.tabs.create({
91-
url: newTabUrl,
92-
pinned: false,
93-
active: false,
94-
});
95-
}
96-
97-
// remove pinned tab if there's enough open tabs
98-
if (pinnedTab && tabs.length > 1) {
99-
await chrome.tabs.remove(pinnedTab.id);
100-
}
101-
102-
context.setCache(handlingKey, false);
103-
}
104-
105-
async function _handleEvent(type, context) {
79+
async function handleEvent(type) {
10680
if (handling) return;
10781
handling = true;
10882

@@ -112,36 +86,35 @@ async function _handleEvent(type, context) {
11286
populate: true,
11387
windowTypes: ["normal"],
11488
});
115-
for (let i = 0; i < windows.length; i++) {
116-
let win = windows[i];
117-
let windowNewTabs = await browser.tabs.query({
89+
90+
for (let win of windows) {
91+
let newTabs = await chrome.tabs.query({
11892
windowId: win.id,
11993
url: newTabUrl,
12094
pinned: false,
12195
});
122-
let windowPinnedTabs = await chrome.tabs.query({
96+
let pinnedTabs = await chrome.tabs.query({
12397
windowId: win.id,
12498
pinned: true,
12599
});
126100
if (win.tabs === undefined || win.tabs[0] === undefined) return;
127-
let windowFirstTab = await browser.tabs.get(win.tabs[0].id);
101+
128102
if (first_window_id == null) first_window_id = win.id;
103+
let firstTab = await chrome.tabs.get(win.tabs[0].id);
129104

130105
// prevent activating first pinned tab
131106
if (win.tabs.length == 2) {
132107
if (
133108
win.tabs.length == 2 &&
134-
windowFirstTab.active &&
135-
windowFirstTab.pinned &&
136-
isNewTabPage(windowFirstTab.url) &&
109+
firstTab.active &&
110+
firstTab.pinned &&
111+
isNewTabPage(firstTab.url) &&
137112
!working
138113
) {
139114
working = true;
140115
if (debug) console.log("activate tab 1");
141116
try {
142-
await browser.tabs.update(win.tabs[1].id, {
143-
active: true,
144-
});
117+
await chrome.tabs.update(win.tabs[1].id, { active: true });
145118
wasWorking = true;
146119
} catch (error) {
147120
setTimeout(function () {
@@ -157,13 +130,13 @@ async function _handleEvent(type, context) {
157130
windows.length == 1 ||
158131
(first_window && first_window_id == win.id)) &&
159132
win.tabs.length < 2 &&
160-
windowPinnedTabs.length < 1 &&
133+
pinnedTabs.length < 1 &&
161134
!working
162135
) {
163136
working = true;
164137
if (debug) console.log("creating pinned tab");
165138
try {
166-
await browser.tabs.create({
139+
await chrome.tabs.create({
167140
windowId: win.id,
168141
index: 0,
169142
pinned: true,
@@ -185,13 +158,13 @@ async function _handleEvent(type, context) {
185158
windows.length == 1 ||
186159
(first_window && first_window_id == win.id)) &&
187160
win.tabs.length == 1 &&
188-
windowPinnedTabs.length == 1 &&
161+
pinnedTabs.length == 1 &&
189162
!working
190163
) {
191164
working = true;
192165
if (debug) console.log("creating tab");
193166
try {
194-
await browser.tabs.create({
167+
await chrome.tabs.create({
195168
windowId: win.id,
196169
url: newTabUrl,
197170
});
@@ -210,15 +183,15 @@ async function _handleEvent(type, context) {
210183
windows.length == 1 ||
211184
(first_window && first_window_id == win.id)) &&
212185
win.tabs.length > 2 &&
213-
windowPinnedTabs.length < win.tabs.length &&
214-
windowPinnedTabs.length >= 1 &&
215-
isNewTabPage(windowFirstTab.url) &&
186+
pinnedTabs.length < win.tabs.length &&
187+
pinnedTabs.length >= 1 &&
188+
isNewTabPage(firstTab.url) &&
216189
!working
217190
) {
218191
working = true;
219192
if (debug) console.log("removing pinned tab 1");
220193
try {
221-
await browser.tabs.remove(windowPinnedTabs[0].id);
194+
await chrome.tabs.remove(pinnedTabs[0].id);
222195
wasWorking = true;
223196
} catch (error) {
224197
setTimeout(function () {
@@ -233,14 +206,14 @@ async function _handleEvent(type, context) {
233206
windows.length > 1 &&
234207
!(first_window && first_window_id == win.id) &&
235208
win.tabs.length == 1 &&
236-
windowPinnedTabs.length == 1 &&
237-
isNewTabPage(windowFirstTab.url) &&
209+
pinnedTabs.length == 1 &&
210+
isNewTabPage(firstTab.url) &&
238211
!working
239212
) {
240213
working = true;
241214
if (debug) console.log("unpin pinned tab");
242215
try {
243-
await browser.tabs.update(windowPinnedTabs[0].id, {
216+
await chrome.tabs.update(pinnedTabs[0].id, {
244217
pinned: false,
245218
});
246219
wasWorking = true;
@@ -255,14 +228,14 @@ async function _handleEvent(type, context) {
255228
windows.length > 1 &&
256229
!(first_window && first_window_id == win.id) &&
257230
win.tabs.length == 2 &&
258-
windowPinnedTabs.length == 1 &&
259-
isNewTabPage(windowFirstTab.url) &&
231+
pinnedTabs.length == 1 &&
232+
isNewTabPage(firstTab.url) &&
260233
!working
261234
) {
262235
working = true;
263236
if (debug) console.log("removing pinned tab 2");
264237
try {
265-
await browser.tabs.remove(windowPinnedTabs[0].id);
238+
await chrome.tabs.remove(pinnedTabs[0].id);
266239
wasWorking = true;
267240
} catch (error) {
268241
console.log(error);
@@ -272,13 +245,13 @@ async function _handleEvent(type, context) {
272245
// allow single new tab page
273246
if (
274247
single_new_tab &&
275-
windowNewTabs.length > 1 &&
248+
newTabs.length > 1 &&
276249
/*windowPinnedTabs.length == 0 &&*/ !working
277250
) {
278251
working = true;
279252
if (debug) console.log("removing tab");
280253
try {
281-
await browser.tabs.remove(windowNewTabs[0].id);
254+
await chrome.tabs.remove(newTabs[0].id);
282255
wasWorking = true;
283256
} catch (error) {
284257
console.log(error);
@@ -288,15 +261,15 @@ async function _handleEvent(type, context) {
288261
// prevent blank new tab page(s) before actual tabs with loaded pages
289262
if (
290263
new_tab_last &&
291-
windowPinnedTabs.length == 0 &&
292-
windowNewTabs.length >= 1 &&
293-
win.tabs[win.tabs.length - 1].id != windowNewTabs[0].id &&
264+
pinnedTabs.length == 0 &&
265+
newTabs.length >= 1 &&
266+
win.tabs[win.tabs.length - 1].id != newTabs[0].id &&
294267
!working
295268
) {
296269
working = true;
297270
if (debug) console.log("removing tab 2");
298271
try {
299-
await browser.tabs.remove(windowNewTabs[0].id);
272+
await chrome.tabs.remove(newTabs[0].id);
300273
wasWorking = true;
301274
} catch (error) {
302275
setTimeout(function () {

0 commit comments

Comments
 (0)