11import browser from 'webextension-polyfill' ;
22
3- import type { Profile , RequestHeader } from '#entities/request-profile/types' ;
4-
53import { BrowserStorageKey , ServiceWorkerEvent } from './shared/constants' ;
64import { browserAction } from './shared/utils/browserAPI' ;
75import { logger , LogLevel } from './shared/utils/logger' ;
86import { setBrowserHeaders } from './shared/utils/setBrowserHeaders' ;
9- import { setIconBadge } from './shared/utils/setIconBadge' ;
107import { enableExtensionReload } from './utils/extension-reload' ;
118
129logger . configure ( {
@@ -21,6 +18,15 @@ logger.info('🎯 Background script loaded successfully!');
2118logger . debug ( '🎯 Background script loaded successfully! (debug)' ) ;
2219logger . info ( '🔍 About to check storage contents...' ) ;
2320
21+ async function getCurrentTabUrl ( ) : Promise < string | undefined > {
22+ try {
23+ const tabs = await browser . tabs . query ( { active : true , currentWindow : true } ) ;
24+ return tabs [ 0 ] ?. url ;
25+ } catch {
26+ return undefined ;
27+ }
28+ }
29+
2430// Check storage immediately on background script load
2531( async ( ) => {
2632 try {
@@ -34,35 +40,13 @@ logger.info('🔍 About to check storage contents...');
3440 logger . info ( ' - Profiles:' , result [ BrowserStorageKey . Profiles ] ? 'Present' : 'Missing' ) ;
3541 logger . info ( ' - Selected Profile:' , result [ BrowserStorageKey . SelectedProfile ] || 'None' ) ;
3642 logger . info ( ' - Is Paused:' , result [ BrowserStorageKey . IsPaused ] || false ) ;
37-
38- // Log profile count if present
39- let activeHeadersCount = 0 ;
40- if ( result [ BrowserStorageKey . Profiles ] ) {
41- try {
42- const profiles = JSON . parse ( result [ BrowserStorageKey . Profiles ] as string ) ;
43- logger . info ( ` - Profiles count: ${ profiles . length } ` ) ;
44- if ( profiles . length > 0 ) {
45- logger . info ( ' - Profile names:' , profiles . map ( ( p : Profile ) => p . name || p . id ) . join ( ', ' ) ) ;
46-
47- // Count active headers for the badge
48- const selectedProfile = profiles . find ( ( p : Profile ) => p . id === result [ BrowserStorageKey . SelectedProfile ] ) ;
49- if ( selectedProfile ) {
50- activeHeadersCount = selectedProfile . requestHeaders ?. filter ( ( h : RequestHeader ) => ! h . disabled ) . length || 0 ;
51- logger . info ( ` - Active headers count: ${ activeHeadersCount } ` ) ;
52- }
53- }
54- } catch ( error ) {
55- logger . warn ( ' - Failed to parse profiles:' , error ) ;
56- }
57- }
43+ logger . groupEnd ( ) ;
5844
5945 logger . debug ( 'Background script load storage data:' , JSON . stringify ( result , null , 2 ) ) ;
60- logger . groupEnd ( ) ;
6146
62- // Set the badge based on storage data
63- const isPaused = ( result [ BrowserStorageKey . IsPaused ] as boolean ) || false ;
64- await setIconBadge ( { isPaused, activeRulesCount : activeHeadersCount } ) ;
65- logger . info ( `🏷️ Badge set: paused=${ isPaused } , activeRules=${ activeHeadersCount } ` ) ;
47+ const currentTabUrl = await getCurrentTabUrl ( ) ;
48+ await setBrowserHeaders ( result , currentTabUrl ) ;
49+ logger . info ( `🏷️ Initial badge set for URL: ${ currentTabUrl } ` ) ;
6650 } catch ( error ) {
6751 logger . error ( 'Failed to check storage on background script load:' , error ) ;
6852 }
@@ -89,7 +73,7 @@ async function notify(message: ServiceWorkerEvent) {
8973 ] ) ;
9074
9175 logger . info ( '📦 Storage data for reload:' , result ) ;
92- await setBrowserHeaders ( result ) ;
76+ await setBrowserHeaders ( result , await getCurrentTabUrl ( ) ) ;
9377 }
9478 return undefined ;
9579}
@@ -110,25 +94,12 @@ browser.runtime.onStartup.addListener(async function () {
11094 logger . info ( ' - Is Paused:' , result [ BrowserStorageKey . IsPaused ] || false ) ;
11195 logger . debug ( 'Startup storage data:' , JSON . stringify ( result , null , 2 ) ) ;
11296
113- // Log profile count if present
114- if ( result [ BrowserStorageKey . Profiles ] ) {
115- try {
116- const profiles = JSON . parse ( result [ BrowserStorageKey . Profiles ] as string ) ;
117- logger . info ( ` - Profiles count: ${ profiles . length } ` ) ;
118- if ( profiles . length > 0 ) {
119- logger . info ( ' - Profile names:' , profiles . map ( ( p : Profile ) => p . name || p . id ) . join ( ', ' ) ) ;
120- }
121- } catch ( error ) {
122- logger . warn ( ' - Failed to parse profiles:' , error ) ;
123- }
124- }
125-
12697 logger . debug ( 'Startup storage data:' , result ) ;
12798
12899 if ( Object . keys ( result ) . length ) {
129100 logger . info ( '🚀 Storage data found, setting browser headers on startup' ) ;
130101 try {
131- await setBrowserHeaders ( result ) ;
102+ await setBrowserHeaders ( result , await getCurrentTabUrl ( ) ) ;
132103 } catch ( error ) {
133104 logger . error ( 'Failed to set browser headers on startup:' , error ) ;
134105 }
@@ -156,7 +127,7 @@ browser.storage.onChanged.addListener(async (changes, areaName) => {
156127 ] ) ;
157128 logger . debug ( 'Storage changes data:' , result ) ;
158129 try {
159- await setBrowserHeaders ( result ) ;
130+ await setBrowserHeaders ( result , await getCurrentTabUrl ( ) ) ;
160131 } catch ( error ) {
161132 logger . error ( 'Failed to set browser headers on storage change:' , error ) ;
162133 }
@@ -181,25 +152,12 @@ browser.runtime.onInstalled.addListener(async details => {
181152 logger . debug ( 'Install/update storage data:' , JSON . stringify ( result , null , 2 ) ) ;
182153 logger . groupEnd ( ) ;
183154
184- // Log profile count if present
185- if ( result [ BrowserStorageKey . Profiles ] ) {
186- try {
187- const profiles = JSON . parse ( result [ BrowserStorageKey . Profiles ] as string ) ;
188- logger . info ( ` - Profiles count: ${ profiles . length } ` ) ;
189- if ( profiles . length > 0 ) {
190- logger . info ( ' - Profile names:' , profiles . map ( ( p : Profile ) => p . name || p . id ) . join ( ', ' ) ) ;
191- }
192- } catch ( error ) {
193- logger . warn ( ' - Failed to parse profiles:' , error ) ;
194- }
195- }
196-
197155 logger . debug ( 'Install/update storage data:' , result ) ;
198156
199157 if ( Object . keys ( result ) . length ) {
200158 logger . info ( '🔧 Storage data found, initializing browser headers on install/update' ) ;
201159 try {
202- await setBrowserHeaders ( result ) ;
160+ await setBrowserHeaders ( result , await getCurrentTabUrl ( ) ) ;
203161 } catch ( error ) {
204162 logger . error ( 'Failed to set browser headers on install/update:' , error ) ;
205163 }
@@ -222,7 +180,8 @@ browser.tabs.onActivated.addListener(async activeInfo => {
222180 if ( Object . keys ( result ) . length ) {
223181 logger . info ( '📱 Tab activated, updating headers' ) ;
224182 try {
225- await setBrowserHeaders ( result ) ;
183+ const tab = await browser . tabs . get ( activeInfo . tabId ) ;
184+ await setBrowserHeaders ( result , tab . url ) ;
226185 } catch ( error ) {
227186 logger . error ( 'Failed to set browser headers on tab activation:' , error ) ;
228187 }
@@ -231,6 +190,29 @@ browser.tabs.onActivated.addListener(async activeInfo => {
231190 }
232191} ) ;
233192
193+ browser . tabs . onUpdated . addListener ( async ( tabId , changeInfo , tab ) => {
194+ if ( changeInfo . status !== 'complete' ) return ;
195+
196+ const activeTabs = await browser . tabs . query ( { active : true , currentWindow : true } ) ;
197+ if ( activeTabs [ 0 ] ?. id !== tabId ) return ;
198+
199+ logger . debug ( 'Active tab URL updated:' , tab . url ) ;
200+
201+ const result = await browser . storage . local . get ( [
202+ BrowserStorageKey . Profiles ,
203+ BrowserStorageKey . SelectedProfile ,
204+ BrowserStorageKey . IsPaused ,
205+ ] ) ;
206+
207+ if ( Object . keys ( result ) . length ) {
208+ try {
209+ await setBrowserHeaders ( result , tab . url ) ;
210+ } catch ( error ) {
211+ logger . error ( 'Failed to set browser headers on tab URL update:' , error ) ;
212+ }
213+ }
214+ } ) ;
215+
234216browserAction . setBadgeBackgroundColor ( { color : BADGE_COLOR } ) ;
235217
236218browser . runtime . onMessage . addListener ( ( message : unknown ) => {
0 commit comments