@@ -17,6 +17,12 @@ export default defineBackground(() => {
1717 browser . runtime . onMessage . addListener ( async ( event , sender ) => {
1818 if ( event . type === "SCREENSHOT" && sender . tab ) {
1919 captureHandler ( sender . tab ) ;
20+ } else if ( event . type === "EXTENSION_TOGGLE" ) {
21+ const { domain, isActive } = event . payload ;
22+ browser . storage ?. local . set ( {
23+ [ domain ] : isActive ,
24+ } ) ;
25+ setIcon ( isActive ) ;
2026 }
2127 } ) ;
2228
@@ -30,11 +36,11 @@ export default defineBackground(() => {
3036
3137 browser . runtime . onStartup . addListener ( async ( ) => {
3238 console . log ( "Extension started" ) ;
33- await updateIconForActiveTab ( ) ;
39+ updateIconForActiveTab ( ) ;
3440 } ) ;
3541
3642 // Update icon when switching tabs
37- browser . tabs . onActivated . addListener ( async ( ) => {
43+ browser . tabs . onActivated . addListener ( async ( _activeInfo ) => {
3844 updateIconForActiveTab ( ) ;
3945 } ) ;
4046
@@ -45,7 +51,7 @@ export default defineBackground(() => {
4551 }
4652 } ) ;
4753
48- // Listen for storage changes to update icon
54+ // // Listen for storage changes to update icon
4955 browser . storage . onChanged . addListener ( async ( changes , areaName ) => {
5056 if ( areaName === "local" ) {
5157 updateIconForActiveTab ( ) ;
@@ -56,14 +62,19 @@ export default defineBackground(() => {
5662/**
5763 * Get the current active tab's domain
5864 */
59- async function getCurrentDomain ( ) : Promise < string | null > {
65+ async function getCurrentDomain ( tabId ?: number ) : Promise < string | null > {
6066 try {
61- const [ tab ] = await browser . tabs . query ( {
62- active : true ,
63- currentWindow : true ,
64- } ) ;
67+ let tab : globalThis . Browser . tabs . Tab ;
68+ if ( tabId ) {
69+ tab = await browser . tabs . get ( tabId ) ;
70+ } else {
71+ [ tab ] = await browser . tabs . query ( {
72+ active : true ,
73+ currentWindow : true ,
74+ } ) ;
75+ }
6576
66- if ( ! tab . url ) return null ;
77+ if ( ! tab ? .url ) return null ;
6778
6879 const url = new URL ( tab . url ) ;
6980 return url . host ;
@@ -86,29 +97,31 @@ async function isDomainActive(domain: string): Promise<boolean> {
8697 }
8798}
8899
100+ async function setIcon ( isActive : boolean ) {
101+ await ( browser . action ?? browser . browserAction ) . setIcon ( {
102+ path : isActive ? IconOn : IconOff ,
103+ } ) ;
104+ }
105+
89106/**
90107 * Update extension icon based on current active tab's domain state
91108 */
92- async function updateIconForActiveTab ( ) {
109+ async function updateIconForActiveTab ( tabId ?: number ) {
93110 try {
94- const domain = await getCurrentDomain ( ) ;
111+ const domain = await getCurrentDomain ( tabId ) ;
95112
96113 if ( ! domain ) {
97114 // No valid domain, show disabled state
98- await ( browser . action ?? browser . browserAction ) . setIcon ( {
99- path : IconOff ,
100- } ) ;
115+ setIcon ( false ) ;
101116 console . log ( "No valid domain, icon set to disabled" ) ;
102117 return ;
103118 }
104119
105120 const isActive = await isDomainActive ( domain ) ;
106121
107- await ( browser . action ?? browser . browserAction ) . setIcon ( {
108- path : isActive ? IconOn : IconOff ,
109- } ) ;
122+ setIcon ( isActive ) ;
110123
111- console . log (
124+ console . trace (
112125 `Icon updated for domain "${ domain } ": ${
113126 isActive ? "active" : "inactive"
114127 } `,
0 commit comments