@@ -57,7 +57,19 @@ async function switchProfile(profileName) {
5757 profileName : profileName
5858 } ) ;
5959
60- currentProfile = response . config ;
60+ // --- Added null check: If response.config is null, try to retrieve default profile ---
61+ if ( ! response || ! response . config ) {
62+ logToConsole ( `Error switching profile: received null config for profile "${ profileName } ". Attempting to retrieve default profile.` ) ;
63+ const configResponse = await chrome . runtime . sendMessage ( { type : 'getConfig' } ) ;
64+ if ( configResponse && configResponse . config ) {
65+ currentProfile = configResponse . config ;
66+ } else {
67+ logToConsole ( 'Error retrieving default profile during switchProfile.' ) ;
68+ return ;
69+ }
70+ } else {
71+ currentProfile = response . config ;
72+ }
6173 updateInterface ( ) ;
6274 logToConsole ( `Switched to profile: ${ profileName } ` ) ;
6375 updateSaveStatus ( ) ;
@@ -162,24 +174,33 @@ async function copyCurrentProfile(newProfileName) {
162174 * Deletes the current profile.
163175 */
164176async function deleteCurrentProfile ( ) {
165- if ( currentProfile . PROFILE_NAME === 'Default' ) {
177+ // --- Modified deletion logic for safety ---
178+ // Check that currentProfile is defined and has a PROFILE_NAME property.
179+ if ( ! currentProfile || ! currentProfile . PROFILE_NAME ) {
180+ showToast ( 'No profile is loaded to delete.' , 'error' ) ;
181+ return ;
182+ }
183+
184+ const profileName = currentProfile . PROFILE_NAME ;
185+
186+ if ( profileName === 'Default' ) {
166187 alert ( 'Cannot delete Default profile' ) ;
167188 return ;
168189 }
169190
170- if ( ! confirm ( `Delete profile "${ currentProfile . PROFILE_NAME } "?` ) ) return ;
191+ if ( ! confirm ( `Delete profile "${ profileName } "?` ) ) return ;
171192
172193 try {
173194 await chrome . runtime . sendMessage ( {
174195 type : 'deleteProfile' ,
175- profileName : currentProfile . PROFILE_NAME
196+ profileName : profileName
176197 } ) ;
177198
178199 await loadProfiles ( ) ;
179- logToConsole ( `Deleted profile: ${ currentProfile . PROFILE_NAME } ` ) ;
180- showToast ( `Profile "${ currentProfile . PROFILE_NAME } " deleted successfully.` , 'success' ) ;
200+ logToConsole ( `Deleted profile: ${ profileName } ` ) ;
201+ showToast ( `Profile "${ profileName } " deleted successfully.` , 'success' ) ;
181202 } catch ( error ) {
182203 showToast ( `Error deleting profile: ${ error . message } ` , 'error' ) ;
183204 logToConsole ( `Error deleting profile: ${ error . message } ` ) ;
184205 }
185- }
206+ }
0 commit comments