@@ -179,106 +179,117 @@ chrome.storage.local.get('config', (c) => {
179179 const config = c . config !== undefined ? c . config : { } ;
180180 if ( isDebug ) console . log ( `config: ${ JSON . stringify ( config , null , 2 ) } ` ) ;
181181
182- // city
183- const cityElement = document . querySelector ( '[data-testid="awsc-nav-regions-menu-button"]>span' ) ;
184- if ( ! cityElement ) {
185- console . error ( 'Region selector not found' ) ;
186- return ;
187- }
188- let city = cityElement . innerText ;
182+ const initializeAWS = ( retryCount = 0 ) => {
183+ const cityElement = document . querySelector ( '[data-testid="awsc-nav-regions-menu-button"]>span' ) ;
184+ if ( ! cityElement ) {
185+ if ( retryCount < 3 ) {
186+ console . log ( `Region selector not found, retrying... (${ retryCount + 1 } /3)` ) ;
187+ setTimeout ( ( ) => initializeAWS ( retryCount + 1 ) , 1000 ) ;
188+ return ;
189+ }
190+ console . error ( 'Region selector not found after 3 retries' ) ;
191+ return ;
192+ }
189193
190- // lang
191- const lang = document . documentElement . lang ;
192- if ( langs . hasOwnProperty ( lang ) && langs [ lang ] . hasOwnProperty ( city ) ) {
193- city = langs [ lang ] [ city ] ;
194- if ( isDebug ) console . log ( `city: ${ city } ` ) ;
195- }
194+ let city = cityElement . innerText ;
196195
197- // aws service
198- let region = undefined ;
199- let svc = undefined ;
200- const re = / ^ h t t p s : \/ \/ ( [ a - z 0 - 9 - ] + ) ? (?: \. ) ? c o n s o l e \. a w s \. a m a z o n \. c o m \/ ( [ a - z 0 - 9 - ] + ) \/ ( [ a - z 0 - 9 ] + (? = \/ ) ) ? .* / ;
201- const m = re . exec ( window . location . href ) ;
202- if ( m !== undefined && m . length > 2 ) {
203- if ( city === 'Global' ) {
204- region = 'global' ;
205- } else {
206- region = m [ 1 ] ;
196+ // lang
197+ const lang = document . documentElement . lang ;
198+ if ( langs . hasOwnProperty ( lang ) && langs [ lang ] . hasOwnProperty ( city ) ) {
199+ city = langs [ lang ] [ city ] ;
200+ if ( isDebug ) console . log ( `city: ${ city } ` ) ;
207201 }
208- svc = m [ 2 ] ;
209- if ( svc === 'codesuite' && m . length > 3 ) {
210- svc = m [ 3 ] ;
202+
203+ // aws service
204+ let region = undefined ;
205+ let svc = undefined ;
206+ const re = / ^ h t t p s : \/ \/ ( [ a - z 0 - 9 - ] + ) ? (?: \. ) ? c o n s o l e \. a w s \. a m a z o n \. c o m \/ ( [ a - z 0 - 9 - ] + ) \/ ( [ a - z 0 - 9 ] + (? = \/ ) ) ? .* / ;
207+ const m = re . exec ( window . location . href ) ;
208+ if ( m !== undefined && m . length > 2 ) {
209+ if ( city === 'Global' ) {
210+ region = 'global' ;
211+ } else {
212+ region = m [ 1 ] ;
213+ }
214+ svc = m [ 2 ] ;
215+ if ( svc === 'codesuite' && m . length > 3 ) {
216+ svc = m [ 3 ] ;
217+ }
211218 }
212- }
213219
214- if ( isDebug ) console . log ( `region: ${ region } ` ) ;
215- if ( isDebug ) console . log ( `service: ${ svc } ` ) ;
220+ if ( isDebug ) console . log ( `region: ${ region } ` ) ;
221+ if ( isDebug ) console . log ( `service: ${ svc } ` ) ;
216222
217- if ( colors . hasOwnProperty ( region ) ) {
218- // region header background
219- if ( config [ 'background' ] !== 'disabled' ) {
220- const navElement = document . querySelector ( "#awsc-navigation-container>div>header>nav" ) ;
221- if ( navElement ) {
222- navElement . style . background = colors [ region ] [ 'background' ] ;
223+ if ( colors . hasOwnProperty ( region ) ) {
224+ // region header background
225+ if ( config [ 'background' ] !== 'disabled' ) {
226+ const navElement = document . querySelector ( "#awsc-navigation-container>div>header>nav" ) ;
227+ if ( navElement ) {
228+ navElement . style . background = colors [ region ] [ 'background' ] ;
229+ }
223230 }
224- }
225231
226- // region flag
227- if ( config [ 'flag' ] !== 'disabled' ) {
228- const regionButton = document . querySelector ( '[data-testid="awsc-nav-regions-menu-button"]' ) ;
229- if ( regionButton ) {
230- const flag = chrome . runtime . getURL ( `flags/${ colors [ region ] [ 'country' ] } .png` ) ;
231- regionButton . insertAdjacentHTML ( "beforeBegin" , `<span style="line-height:0;margin-right:0.5em;"><img src="${ flag } " style="width:20px;height:20px;"></span>` ) ;
232+ // region flag
233+ if ( config [ 'flag' ] !== 'disabled' ) {
234+ const regionButton = document . querySelector ( '[data-testid="awsc-nav-regions-menu-button"]' ) ;
235+ if ( regionButton ) {
236+ const flag = chrome . runtime . getURL ( `flags/${ colors [ region ] [ 'country' ] } .png` ) ;
237+ regionButton . insertAdjacentHTML ( "beforeBegin" , `<span style="line-height:0;margin-right:0.5em;"><img src="${ flag } " style="width:20px;height:20px;"></span>` ) ;
238+ }
232239 }
233240 }
234- }
235241
236- // account info
237- const accountMenu = document . querySelector ( 'div[data-testid=account-detail-menu]>div>div' ) ;
238- if ( accountMenu && accountMenu . children . length > 1 ) {
239- const account_id = accountMenu . children [ 1 ] . innerText . replaceAll ( '-' , '' ) ;
240- if ( isDebug ) console . log ( `account_id: ${ account_id } ` ) ;
242+ // account info
243+ const accountMenu = document . querySelector ( 'div[data-testid=account-detail-menu]>div>div' ) ;
244+ if ( accountMenu && accountMenu . children . length > 1 ) {
245+ const account_id = accountMenu . children [ 1 ] . innerText . replaceAll ( '-' , '' ) ;
246+ if ( isDebug ) console . log ( `account_id: ${ account_id } ` ) ;
241247
242- if ( config [ 'info' ] !== undefined && config [ 'info' ] [ account_id ] !== undefined ) {
243- const accountButton = document . querySelector ( '[data-testid="awsc-nav-account-menu-button"]' ) ;
244- if ( accountButton ) {
245- accountButton . insertAdjacentHTML ( "beforeBegin" , `<span style="font-size:1.8em;margin-right:0.2em;">${ config [ 'info' ] [ account_id ] } </span>` ) ;
248+ if ( config [ 'info' ] !== undefined && config [ 'info' ] [ account_id ] !== undefined ) {
249+ const accountButton = document . querySelector ( '[data-testid="awsc-nav-account-menu-button"]' ) ;
250+ if ( accountButton ) {
251+ accountButton . insertAdjacentHTML ( "beforeBegin" , `<span style="font-size:1.8em;margin-right:0.2em;">${ config [ 'info' ] [ account_id ] } </span>` ) ;
252+ }
246253 }
247254 }
248- }
249255
250- // favicon
251- if ( config [ 'favicon' ] !== 'disabled' && svc !== undefined ) {
252- const x1 = document . querySelector ( "link[rel*='shortcut icon']" ) ;
253- if ( x1 ) document . head . removeChild ( x1 ) ;
256+ // favicon
257+ if ( config [ 'favicon' ] !== 'disabled' && svc !== undefined ) {
258+ const x1 = document . querySelector ( "link[rel*='shortcut icon']" ) ;
259+ if ( x1 ) document . head . removeChild ( x1 ) ;
254260
255- const x2 = document . querySelector ( "link[rel*='icon']" ) ;
256- if ( x2 ) document . head . removeChild ( x2 ) ;
261+ const x2 = document . querySelector ( "link[rel*='icon']" ) ;
262+ if ( x2 ) document . head . removeChild ( x2 ) ;
257263
258- const link_icon = document . createElement ( 'link' ) ;
259- const link_shortcut_icon = document . createElement ( 'link' ) ;
264+ const link_icon = document . createElement ( 'link' ) ;
265+ const link_shortcut_icon = document . createElement ( 'link' ) ;
260266
261- link_icon . rel = 'icon' ;
262- link_shortcut_icon . rel = 'shortcut icon' ;
267+ link_icon . rel = 'icon' ;
268+ link_shortcut_icon . rel = 'shortcut icon' ;
263269
264- if ( svc === 'console' || svc === 'settings' || svc === 'servicequotas' || svc === 'billing' ) {
265- link_icon . href = chrome . runtime . getURL ( `svcs/favicon.ico` ) ;
266- link_icon . id = 'aws-icon' ;
270+ if ( svc === 'console' || svc === 'settings' || svc === 'servicequotas' || svc === 'billing' ) {
271+ link_icon . href = chrome . runtime . getURL ( `svcs/favicon.ico` ) ;
272+ link_icon . id = 'aws-icon' ;
267273
268- link_shortcut_icon . href = chrome . runtime . getURL ( `svcs/favicon.ico` ) ;
269- link_shortcut_icon . id = 'aws-shortcut-icon' ;
270- } else {
271- link_icon . type = 'image/svg+xml' ;
272- link_icon . href = chrome . runtime . getURL ( `svcs/${ svc } .svg` ) ;
273- link_icon . id = 'aws-icon' ;
274+ link_shortcut_icon . href = chrome . runtime . getURL ( `svcs/favicon.ico` ) ;
275+ link_shortcut_icon . id = 'aws-shortcut-icon' ;
276+ } else {
277+ link_icon . type = 'image/svg+xml' ;
278+ link_icon . href = chrome . runtime . getURL ( `svcs/${ svc } .svg` ) ;
279+ link_icon . id = 'aws-icon' ;
274280
275- link_shortcut_icon . type = 'image/svg+xml' ;
276- link_shortcut_icon . href = chrome . runtime . getURL ( `svcs/${ svc } .svg` ) ;
277- link_shortcut_icon . id = 'aws-shortcut-icon' ;
281+ link_shortcut_icon . type = 'image/svg+xml' ;
282+ link_shortcut_icon . href = chrome . runtime . getURL ( `svcs/${ svc } .svg` ) ;
283+ link_shortcut_icon . id = 'aws-shortcut-icon' ;
284+ }
285+ document . head . appendChild ( link_icon ) ;
286+ document . head . appendChild ( link_shortcut_icon ) ;
278287 }
279- document . head . appendChild ( link_icon ) ;
280- document . head . appendChild ( link_shortcut_icon ) ;
281- }
288+ } ;
289+
290+ // Start initialization
291+ initializeAWS ( ) ;
292+
282293 } catch ( error ) {
283294 console . error ( 'Error:' , error ) ;
284295 }
0 commit comments