@@ -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
4870function 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