@@ -175,93 +175,111 @@ const isDebug = true;
175175
176176// load
177177chrome . storage . local . get ( 'config' , ( c ) => {
178- const config = c . config !== undefined ? c . config : { } ;
179- if ( isDebug ) console . log ( `config: ${ JSON . stringify ( config , null , 2 ) } ` ) ;
178+ try {
179+ const config = c . config !== undefined ? c . config : { } ;
180+ if ( isDebug ) console . log ( `config: ${ JSON . stringify ( config , null , 2 ) } ` ) ;
180181
181- // city
182- let city = document . querySelector ( '[data-testid="awsc-nav-regions-menu-button"]>span' ) . innerText ;
183-
184- // lang
185- const lang = document . documentElement . lang ;
186- if ( langs . hasOwnProperty ( lang ) && langs [ lang ] . hasOwnProperty ( city ) ) {
187- city = langs [ lang ] [ city ] ;
188- if ( isDebug ) console . log ( `city: ${ city } ` ) ;
189- }
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 ;
190189
191- // aws service
192- let region = undefined ;
193- let svc = undefined ;
194- 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 ] + (? = \/ ) ) ? .* / ;
195- const m = re . exec ( window . location . href ) ;
196- if ( m !== undefined && m . length > 2 ) {
197- if ( city === 'Global' ) {
198- region = 'global' ;
199- } else {
200- region = m [ 1 ] ;
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 } ` ) ;
201195 }
202- svc = m [ 2 ] ;
203- if ( svc === 'codesuite' && m . length > 3 ) {
204- svc = m [ 3 ] ;
196+
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 ] ;
207+ }
208+ svc = m [ 2 ] ;
209+ if ( svc === 'codesuite' && m . length > 3 ) {
210+ svc = m [ 3 ] ;
211+ }
205212 }
206- }
207213
208- if ( isDebug ) console . log ( `region: ${ region } ` ) ;
209- if ( isDebug ) console . log ( `service: ${ svc } ` ) ;
214+ if ( isDebug ) console . log ( `region: ${ region } ` ) ;
215+ if ( isDebug ) console . log ( `service: ${ svc } ` ) ;
210216
211- if ( colors . hasOwnProperty ( region ) ) {
212- // region header background
213- if ( config [ 'background' ] !== 'disabled' ) {
214- document . querySelector ( "#awsc-navigation-container>div>header>nav" ) . style . background = colors [ region ] [ 'background' ] ;
215- }
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+ }
224+ }
216225
217- // region flag
218- if ( config [ 'flag' ] !== 'disabled' ) {
219- const flag = chrome . runtime . getURL ( `flags/${ colors [ region ] [ 'country' ] } .png` ) ;
220- document . querySelector ( '[data-testid="awsc-nav-regions-menu-button"]' ) . insertAdjacentHTML ( "beforeBegin" , `<span style="line-height:0;margin-right:0.5em;"><img src="${ flag } " style="width:20px;height:20px;"></span>` ) ;
221- // document.querySelector('[data-testid="awsc-nav-regions-menu-button"]').insertAdjacentHTML("beforeBegin", `<span style="font-size:1.8em;margin-right:0.2em;">${colors[region]['emoji']}</span>`);
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+ }
233+ }
222234 }
223- }
224235
225- // account_id
226- const account_menu = document . querySelector ( 'div[data-testid=account-detail-menu]>div>div' ) ;
227- const account_id = account_menu . children [ 1 ] . innerText . replaceAll ( '-' , '' ) ;
228- if ( isDebug ) console . log ( `account_id: ${ account_id } ` ) ;
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 } ` ) ;
229241
230- // account info
231- if ( config [ 'info' ] !== undefined && config [ 'info' ] [ account_id ] !== undefined ) {
232- document . querySelector ( '[data-testid="awsc-nav-account-menu-button"]' ) . insertAdjacentHTML ( "beforeBegin" , `<span style="font-size:1.8em;margin-right:0.2em;">${ config [ 'info' ] [ account_id ] } </span>` ) ;
233- }
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>` ) ;
246+ }
247+ }
248+ }
234249
235- // favicon
236- if ( config [ 'favicon' ] !== 'disabled' && svc !== undefined ) {
237- const x1 = document . querySelector ( "link[rel*='shortcut icon']" ) ;
238- if ( x1 ) document . head . removeChild ( x1 ) ;
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 ) ;
239254
240- const x2 = document . querySelector ( "link[rel*='icon']" ) ;
241- if ( x2 ) document . head . removeChild ( x2 ) ;
255+ const x2 = document . querySelector ( "link[rel*='icon']" ) ;
256+ if ( x2 ) document . head . removeChild ( x2 ) ;
242257
243- const link_icon = document . createElement ( 'link' ) ;
244- const link_shortcut_icon = document . createElement ( 'link' ) ;
258+ const link_icon = document . createElement ( 'link' ) ;
259+ const link_shortcut_icon = document . createElement ( 'link' ) ;
245260
246- link_icon . rel = 'icon' ;
247- link_shortcut_icon . rel = 'shortcut icon' ;
261+ link_icon . rel = 'icon' ;
262+ link_shortcut_icon . rel = 'shortcut icon' ;
248263
249- if ( svc === 'console' || svc === 'settings' || svc === 'servicequotas' || svc === 'billing' ) {
250- link_icon . href = chrome . runtime . getURL ( `svcs/favicon.ico` ) ;
251- link_icon . id = 'icon' ;
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' ;
252267
253- link_shortcut_icon . href = chrome . runtime . getURL ( `svcs/favicon.ico` ) ;
254- link_shortcut_icon . id = 'icon' ;
255- } else {
256- link_icon . type = 'image/svg+xml' ;
257- link_icon . href = chrome . runtime . getURL ( `svcs/${ svc } .svg` ) ;
258- link_icon . id = 'icon' ;
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' ;
259274
260- link_shortcut_icon . type = 'image/svg+xml' ;
261- link_shortcut_icon . href = chrome . runtime . getURL ( `svcs/${ svc } .svg` ) ;
262- link_shortcut_icon . id = 'shortcutIcon' ;
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' ;
278+ }
279+ document . head . appendChild ( link_icon ) ;
280+ document . head . appendChild ( link_shortcut_icon ) ;
263281 }
264- document . head . appendChild ( link_icon ) ;
265- document . head . appendChild ( link_shortcut_icon ) ;
282+ } catch ( error ) {
283+ console . error ( 'Error:' , error ) ;
266284 }
267285} ) ;
0 commit comments