66// Track which tabs have content scripts loaded
77const tabsWithContentScript = new Set < number > ( ) ;
88
9+ // Track active state of the extension
10+ let isActive = false ;
11+
12+ // Colors based on the purple book icon
13+ const BADGE_BACKGROUND_COLOR : [ number , number , number , number ] = [ 177 , 156 , 217 , 255 ] ; // #B19CD9 - Lavender purple (original icon color)
14+
915// Listen for content script ready messages
1016chrome . runtime . onMessage . addListener ( ( message , sender , sendResponse ) => {
1117
1218 // Mark tab as having content script ready
1319 if ( message . type === "CONTENT_SCRIPT_READY" && sender . tab ?. id ) {
1420 tabsWithContentScript . add ( sender . tab . id ) ;
15- sendResponse ( { received : true } ) ;
21+ sendResponse ( { received : true } ) ;
1622 return true ;
1723 }
1824
@@ -72,22 +78,55 @@ async function toggleReaderMode(tabId: number) {
7278 }
7379}
7480
81+ /**
82+ * Update the extension icon state
83+ * @param active - Whether the extension is active
84+ */
85+ function updateIconState ( active : boolean ) {
86+ if ( active ) {
87+ // For active state: use "on" text with a compact badge
88+ chrome . action . setBadgeText ( { text : "ON" } ) ;
89+
90+ // Use the lavender purple color that matches the icon
91+ chrome . action . setBadgeBackgroundColor ( {
92+ color : BADGE_BACKGROUND_COLOR
93+ } ) ;
94+
95+ // Make badge text smaller and more compact
96+ chrome . action . setBadgeTextColor ( { color : "#FFFFFF" } ) ;
97+ } else {
98+ // For inactive state: clear the badge completely
99+ chrome . action . setBadgeText ( { text : "" } ) ;
100+ }
101+
102+ // Store the current state
103+ isActive = active ;
104+ }
105+
75106// Listen for extension icon clicks
76107chrome . action . onClicked . addListener ( async ( tab ) => {
77108 if ( ! tab . id ) return ;
78109
79-
110+ // Toggle the active state
111+ isActive = ! isActive ;
112+
113+ // Update the icon to reflect the new state
114+ updateIconState ( isActive ) ;
115+
80116 try {
81117 // Execute script to dispatch the custom event directly
82118 await chrome . scripting . executeScript ( {
83119 target : { tabId : tab . id } ,
84120 func : ( ) => {
85- // Create and dispatch the event directly
121+ // Create and dispatch the event directly
86122 document . dispatchEvent ( new CustomEvent ( 'READLITE_TOGGLE_INTERNAL' ) ) ;
87123 }
88124 } ) ;
89125
90- } catch ( error ) {
126+ } catch ( error ) {
91127 console . error ( "Error executing script:" , error ) ;
92128 }
93- } ) ;
129+ } ) ;
130+
131+ // Initialize icon state on extension load
132+ updateIconState ( isActive ) ;
0 commit comments