Skip to content

Commit 3aed9b1

Browse files
author
hoang.tran12
committed
prevent close browser last tab
1 parent 0b37089 commit 3aed9b1

File tree

6 files changed

+396
-17
lines changed

6 files changed

+396
-17
lines changed

popup/tabs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ const tabs = [
484484
s.magnify_image,
485485
s.auto_redirectLargestImageSrc,
486486
s.remove_tracking_in_url,
487+
s.prevent_closeBrowser_lastTab,
487488
s.chongLuaDao,
488489
s.shortenURL,
489490
s.unshorten,

scripts/_test.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,17 @@ export default {
270270
document.head.appendChild(favicon);
271271

272272
function updateFavicon() {
273+
requestAnimationFrame(updateFavicon);
273274
let img = canvas.toDataURL();
274275
favicon.setAttribute("href", img);
275276
}
276277

277278
setInterval(() => {
278279
context.clearRect(0, 0, canvas.width, canvas.height);
279280
context.drawImage(video, 0, 0, canvas.width, canvas.height);
280-
updateFavicon();
281-
}, 500);
281+
}, 1000 / 30);
282+
283+
updateFavicon();
282284
},
283285
},
284286

@@ -292,6 +294,45 @@ export default {
292294
console.log(res);
293295
},
294296
},
297+
298+
backgroundScript: {
299+
tabs: {
300+
onRemoved: (details, context) => {
301+
const [tabId, removeInfo] = details;
302+
303+
// check if there is any tab left
304+
chrome.tabs.query({}, (tabs) => {
305+
let tabCached = context.getCache("tabs");
306+
307+
// case tabs was opened before extension installed/reloaded
308+
if (!tabCached) {
309+
tabCached = {};
310+
tabs.forEach((tab) => {
311+
tabCached[tab.id] = tab;
312+
});
313+
context.setCache("tabs", tabCached);
314+
}
315+
316+
console.log("tabCached", tabCached);
317+
if (
318+
!tabCached[tabId] ||
319+
tabCached[tabId]?.url?.endsWith?.("://newtab/")
320+
)
321+
return;
322+
323+
// create a new tab
324+
if (tabs.length == 0) {
325+
chrome.tabs.create({ url: "chrome://newtab/" });
326+
}
327+
});
328+
},
329+
onUpdated: (details, context) => {
330+
console.log("onUpdated", details);
331+
const [tabId, changeInfo, tab] = details;
332+
context.setCache("tabs." + tabId, tab);
333+
},
334+
},
335+
},
295336
};
296337

297338
const backup = () => {

scripts/background-scripts/background_script.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,42 @@ const GLOBAL = {
2626
runScriptsTab,
2727
runScriptsBackground,
2828
checkWillRun,
29-
getAllCache,
3029
getCache,
31-
addCache,
30+
setCache,
3231
removeCache,
3332
};
3433

35-
function getAllCache() {
36-
return CACHED;
37-
}
38-
39-
function getCache(key) {
40-
return CACHED[key];
34+
// keyChain = "" => get all cached
35+
function getCache(keyChain, defaultValue = null) {
36+
let value = CACHED;
37+
keyChain?.split(".")?.forEach((key) => {
38+
value = value?.[key] ?? null;
39+
});
40+
return value ?? defaultValue;
4141
}
4242

43-
function addCache(key, value) {
44-
CACHED[key] = value;
43+
function setCache(keyChain, value) {
44+
let keys = keyChain?.split(".");
45+
let obj = CACHED;
46+
keys?.forEach((key, i) => {
47+
if (i === keys.length - 1) {
48+
obj[key] = value;
49+
} else {
50+
obj = obj[key] ?? (obj[key] = {});
51+
}
52+
});
4553
}
4654

47-
function removeCache(key) {
48-
delete CACHED[key];
55+
function removeCache(keyChain) {
56+
let keys = keyChain?.split(".");
57+
let obj = CACHED;
58+
keys?.forEach((key, i) => {
59+
if (i === keys.length - 1) {
60+
delete obj[key];
61+
} else {
62+
obj = obj[key];
63+
}
64+
});
4965
}
5066

5167
function cacheActiveScriptIds() {
@@ -342,8 +358,9 @@ function listenTabs() {
342358
"onUpdated",
343359
// "onZoomChange",
344360
].forEach((event) => {
345-
chrome.tabs[event].addListener((details) => {
346-
runScriptsBackground("tabs." + event, details);
361+
chrome.tabs[event].addListener((...details) => {
362+
// these events will fired in all scripts active scripts (no need to call checkWillRun)
363+
runScriptsBackground("tabs." + event, null, details);
347364
});
348365
});
349366
}

scripts/chongLuaDao.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ async function saveBgCache(context) {
126126
whiteList: await context.utils.Storage.get(KEYS.whiteList, []),
127127
};
128128
console.log("cached chongLuaDao", cached);
129-
context.addCache("chongLuaDao", cached);
129+
context.setCache("chongLuaDao", cached);
130130
return cached;
131131
} catch (e) {
132132
console.log("cache chongLuaDao FAIL ", e);

scripts/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ import remove_tracking_in_url from "./remove_tracking_in_url.js";
164164
import auto_lockWebsite from "./auto_lockWebsite.js";
165165
import showFps_v2 from "./showFps_v2.js";
166166
import chongLuaDao from "./chongLuaDao.js";
167+
import prevent_closeBrowser_lastTab from "./prevent_closeBrowser_lastTab.js";
167168

168169
// inject badges
169170
const allScripts = {
@@ -344,6 +345,10 @@ const allScripts = {
344345
auto_lockWebsite: addBadge(auto_lockWebsite, BADGES.new),
345346
showFps_v2: addBadge(showFps_v2, BADGES.new),
346347
chongLuaDao: addBadge(chongLuaDao, BADGES.new),
348+
prevent_closeBrowser_lastTab: addBadge(
349+
prevent_closeBrowser_lastTab,
350+
BADGES.new
351+
),
347352
};
348353

349354
// alert(Object.keys(allScripts).length);

0 commit comments

Comments
 (0)