@@ -138,29 +138,60 @@ function permissionsChanged() {
138138/******************************************************************************/
139139
140140function gotoExtensionURL ( url ) {
141- url = chrome . extension . getURL ( url ) ;
141+
142+ var hasFragment = function ( url ) {
143+ return url . indexOf ( '#' ) >= 0 ;
144+ } ;
145+
146+ var removeFragment = function ( url ) {
147+ var pos = url . indexOf ( '#' ) ;
148+ if ( pos < 0 ) {
149+ return url ;
150+ }
151+ return url . slice ( 0 , pos ) ;
152+ } ;
153+
154+ var tabIndex = 9999 ;
155+ var targetUrl = chrome . extension . getURL ( url ) ;
156+ var urlToFind = removeFragment ( targetUrl ) ;
157+
158+ var currentWindow = function ( tabs ) {
159+ var updateProperties = { active : true } ;
160+ var i = tabs . length ;
161+ while ( i -- ) {
162+ if ( removeFragment ( tabs [ i ] . url ) !== urlToFind ) {
163+ continue ;
164+ }
165+ // If current tab in dashboard is different, force the new one, if
166+ // there is one, to be activated.
167+ if ( tabs [ i ] . url !== targetUrl ) {
168+ if ( hasFragment ( targetUrl ) ) {
169+ updateProperties . url = targetUrl ;
170+ }
171+ }
172+ // Activate found matching tab
173+ // Commented out as per:
174+ // https://github.com/gorhill/httpswitchboard/issues/150#issuecomment-32683726
175+ // chrome.tabs.move(tabs[0].id, { index: index + 1 });
176+ chrome . tabs . update ( tabs [ i ] . id , updateProperties ) ;
177+ return ;
178+ }
179+ chrome . tabs . create ( { 'url' : targetUrl , index : tabIndex + 1 } ) ;
180+ } ;
181+
182+ var currentTab = function ( tabs ) {
183+ if ( tabs . length ) {
184+ tabIndex = tabs [ 0 ] . index ;
185+ }
186+ chrome . tabs . query ( { currentWindow : true } , currentWindow ) ;
187+ } ;
188+
142189 // https://github.com/gorhill/httpswitchboard/issues/150
143190 // Logic:
144191 // - If URL is already opened in a tab, just activate tab
145192 // - Otherwise find the current active tab and open in a tab immediately
146193 // to the right of the active tab
147- chrome . tabs . query ( { active : true } , function ( tabs ) {
148- var index = tabs . length ? tabs [ 0 ] . index : 9999 ;
149- chrome . tabs . query ( { currentWindow : true } , function ( tabs ) {
150- var i = tabs . length ;
151- while ( i -- ) {
152- if ( tabs [ i ] . url === url ) {
153- // Activate found matching tab
154- // Commented out as per:
155- // https://github.com/gorhill/httpswitchboard/issues/150#issuecomment-32683726
156- // chrome.tabs.move(tabs[0].id, { index: index + 1 });
157- chrome . tabs . update ( tabs [ i ] . id , { active : true } ) ;
158- return ;
159- }
160- }
161- chrome . tabs . create ( { 'url' : url , index : index + 1 } ) ;
162- } ) ;
163- } ) ;
194+ chrome . tabs . query ( { active : true } , currentTab ) ;
164195}
165196
166197/******************************************************************************/
0 commit comments