@@ -7,7 +7,6 @@ const { version } =
77
88const CACHED = {
99 userID : null ,
10- lastWindowIds : [ ] ,
1110} ;
1211
1312export function checkBlackWhiteList ( script , url = location ?. href ) {
@@ -113,16 +112,14 @@ export function runFunc(fnPath = "", params = [], global = {}) {
113112 return p ;
114113 } ) ;
115114
116- if ( ! ( typeof fn === "function" ) ) return resolve ( null ) ;
115+ if ( typeof fn !== "function" ) return resolve ( null ) ;
117116
118117 try {
119118 let res = fn ( ..._params ) ;
120119
121120 if ( ! hasCallback ) {
122121 if ( typeof res ?. then === "function" ) {
123- res . then ?. ( ( _res ) => {
124- resolve ( _res ) ;
125- } ) ;
122+ res . then ?. ( resolve ) ;
126123 } else {
127124 resolve ( res ) ;
128125 }
@@ -189,7 +186,6 @@ export async function setActiveScript(scriptId, isActive = true) {
189186 if ( isActive ) list . push ( scriptId ) ;
190187 else list = list . filter ( ( _ ) => _ != scriptId ) ;
191188 list = list . filter ( ( _ ) => _ ) ;
192- // localStorage.setItem(listActiveScriptsKey, JSON.stringify(list));
193189 Storage . set ( listActiveScriptsKey , list ) ; // save to storage => content script can access
194190 return list ;
195191}
@@ -225,10 +221,6 @@ export async function toggleActiveScript(scriptId) {
225221// });
226222// };
227223
228- // const CACHED = {
229- // lastWindowId: 0,
230- // };
231-
232224// https://developer.chrome.com/docs/extensions/reference/windows/#event-onFocusChanged
233225// chrome.windows.onFocusChanged.addListener(
234226// (windowId) => {
@@ -253,47 +245,50 @@ export const getAllTabs = async () => {
253245 return tabs ;
254246} ;
255247
256- // Lấy ra tab hiện tại, trong window sử dung gần nhất
257- if ( typeof chrome ?. windows ?. onFocusChanged ?. addListener === "function" ) {
258- chrome . windows . onFocusChanged . addListener ( ( windowId ) => {
259- if ( windowId !== chrome . windows . WINDOW_ID_NONE ) {
260- CACHED . lastWindowIds . unshift ( windowId ) ;
261-
262- // remove duplicate
263- for ( let i = 1 ; i < CACHED . lastWindowIds . length ; i ++ ) {
264- if ( windowId === CACHED . lastWindowIds [ i ] ) {
265- CACHED . lastWindowIds . splice ( i , 1 ) ;
266- break ;
267- }
268- }
269- }
270- } ) ;
248+ export function getPopupURL ( ) {
249+ return chrome . runtime . getURL ( "/popup/popup.html" ) ;
271250}
272251
273- export const getCurrentTab = async ( ) => {
274- let windows = await chrome . windows . getAll ( { populate : true } ) ;
275- let lastFocusedWindow = windows
252+ export const getLastFocusedWindowIds = ( ) => {
253+ return chrome . runtime . sendMessage ( {
254+ action : "ufs-runInBackground" ,
255+ data : {
256+ fnPath : "getCache" ,
257+ params : [ "lastFocusedWindowIds" ] ,
258+ } ,
259+ } ) ;
260+ } ;
261+
262+ export const getLastFocusedTab = async ( ) => {
263+ const lastFocusedWindowIds = await getLastFocusedWindowIds ( ) ;
264+ const windows = await chrome . windows . getAll ( { populate : true } ) ;
265+ const lastFocusedWindow = windows
276266 // sort windows by lastFocused
277267 . sort (
278268 ( a , b ) =>
279- CACHED . lastWindowIds . indexOf ( a . id ) - CACHED . lastWindowIds . indexOf ( b . id )
269+ lastFocusedWindowIds . indexOf ( a . id ) - lastFocusedWindowIds . indexOf ( b . id )
280270 )
281271 // get windows that not popup extension
282- . filter (
283- ( w ) =>
284- w . type !== "popup" &&
285- w . tabs [ 0 ] . url !== chrome . runtime . getURL ( "/popup/popup.html" )
286- ) ?. [ 0 ] ;
287-
288- console . log ( "lastFocusedWindow" , lastFocusedWindow ) ;
272+ . filter ( ( w ) => w . type !== "popup" && w . tabs [ 0 ] . url !== getPopupURL ( ) ) ?. [ 0 ] ;
289273
290274 if ( ! lastFocusedWindow ) return null ;
291-
292- let tab = lastFocusedWindow . tabs . find ( ( tab ) => tab . active ) ;
293- console . log ( "tabs" , tab ) ;
275+ const tab = lastFocusedWindow . tabs . find (
276+ ( tab ) => tab . active && tab . url !== getPopupURL ( )
277+ ) ;
294278 return tab ;
295279} ;
296280
281+ export const getCurrentTab = async ( ) => {
282+ // case normal popup
283+ let tabs = await chrome . tabs . query ( {
284+ active : true ,
285+ currentWindow : true ,
286+ } ) ;
287+ if ( tabs ?. [ 0 ] ?. url !== getPopupURL ( ) ) return tabs [ 0 ] ;
288+
289+ return await getLastFocusedTab ( ) ;
290+ } ;
291+
297292export const getCurrentTabId = async ( ) => {
298293 return ( await getCurrentTab ( ) ) ?. id ;
299294} ;
0 commit comments