55*/
66'use strict' ;
77
8- gLogContext = 'BG' ;
8+ import { configs } from '../common/commonConfigs.js' ;
9+ import { log , setLogContext } from '../common/common.js' ;
10+ import { URIMatcher } from './uriMatcher.js' ;
11+ import * as Constants from '../common/constants.js' ;
12+
13+ setLogContext ( 'BG' ) ;
14+
15+ let mLastContextTab = 0 ;
916
1017browser . runtime . onMessage . addListener ( ( message , sender ) => {
1118 if ( ! message ||
@@ -14,10 +21,10 @@ browser.runtime.onMessage.addListener((message, sender) => {
1421 return ;
1522
1623 switch ( message . type ) {
17- case kCOMMAND_TRY_ACTION : return ( async ( ) => {
24+ case Constants . kCOMMAND_TRY_ACTION : return ( async ( ) => {
1825 const action = detectActionFromEvent ( message . event ) ;
1926 log ( 'action: ' , action ) ;
20- if ( action == kACTION_DISABLED )
27+ if ( action == Constants . kACTION_DISABLED )
2128 return null ;
2229
2330 message . cursor . framePos = sender . frameId ;
@@ -33,19 +40,20 @@ browser.runtime.onMessage.addListener((message, sender) => {
3340
3441 result . action = action ;
3542 if ( result . uri ) {
36- if ( action & kACTION_OPEN_IN_CURRENT ) {
43+ if ( action & Constants . kACTION_OPEN_IN_CURRENT ) {
3744 browser . tabs . update ( sender . tab . id , {
3845 url : result . uri
3946 } ) ;
4047 }
41- else if ( action & kACTION_OPEN_IN_WINDOW ) {
48+ else if ( action & Constants . kACTION_OPEN_IN_WINDOW ) {
4249 browser . windows . create ( {
4350 url : result . uri
4451 } ) ;
4552 }
46- else if ( action & kACTION_OPEN_IN_TAB || action & kACTION_OPEN_IN_BACKGROUND_TAB ) {
53+ else if ( action & Constants . kACTION_OPEN_IN_TAB ||
54+ action & Constants . kACTION_OPEN_IN_BACKGROUND_TAB ) {
4755 browser . tabs . create ( {
48- active : ! ! ( action & kACTION_OPEN_IN_TAB ) ,
56+ active : ! ! ( action & Constants . kACTION_OPEN_IN_TAB ) ,
4957 url : result . uri ,
5058 windowId : sender . tab . windowId ,
5159 openerTabId : sender . tab . id
@@ -55,27 +63,27 @@ browser.runtime.onMessage.addListener((message, sender) => {
5563 return result ;
5664 } ) ( ) ;
5765
58- case kCOMMAND_ACTION_FOR_URIS :
59- if ( message . action & kACTION_OPEN_IN_CURRENT ) {
66+ case Constants . kCOMMAND_ACTION_FOR_URIS :
67+ if ( message . action & Constants . kACTION_OPEN_IN_CURRENT ) {
6068 browser . tabs . update ( sender . tab . id , {
6169 url : message . uris [ 0 ]
6270 } ) ;
63- message . uris . slice ( 1 ) . forEach ( ( uri , index ) => {
71+ message . uris . slice ( 1 ) . forEach ( ( uri , _index ) => {
6472 browser . tabs . create ( {
6573 url : uri ,
6674 windowId : sender . tab . windowId ,
6775 openerTabId : sender . tab . id
6876 } ) ;
6977 } ) ;
7078 }
71- else if ( message . action & kACTION_OPEN_IN_WINDOW ) {
72- message . uris . forEach ( ( uri , index ) => {
79+ else if ( message . action & Constants . kACTION_OPEN_IN_WINDOW ) {
80+ message . uris . forEach ( ( uri , _index ) => {
7381 browser . windows . create ( {
7482 url : uri
7583 } ) ;
7684 } ) ;
7785 }
78- else if ( message . action & kACTION_OPEN_IN_TAB ) {
86+ else if ( message . action & Constants . kACTION_OPEN_IN_TAB ) {
7987 message . uris . forEach ( ( uri , index ) => {
8088 browser . tabs . create ( {
8189 active : index == 0 ,
@@ -87,13 +95,13 @@ browser.runtime.onMessage.addListener((message, sender) => {
8795 }
8896 break ;
8997
90- case kNOTIFY_READY_TO_FIND_URI_RANGES :
98+ case Constants . kNOTIFY_READY_TO_FIND_URI_RANGES :
9199 initContextMenuForWaiting ( sender . tab . id ) ;
92100 break ;
93101
94- case kCOMMAND_FIND_URI_RANGES : return ( async ( ) => {
102+ case Constants . kCOMMAND_FIND_URI_RANGES : return ( async ( ) => {
95103 browser . tabs . sendMessage ( sender . tab . id , {
96- type : kNOTIFY_MATCH_ALL_PROGRESS ,
104+ type : Constants . kNOTIFY_MATCH_ALL_PROGRESS ,
97105 progress : 0
98106 } ) ;
99107 await initContextMenuForWaiting ( sender . tab . id ) ;
@@ -102,23 +110,23 @@ browser.runtime.onMessage.addListener((message, sender) => {
102110 range . framePos = sender . frameId ;
103111 }
104112 const results = await URIMatcher . matchAll ( {
105- tabId : sender . tab . id ,
106- ranges : message . ranges ,
107- baseURI : message . base ,
113+ tabId : sender . tab . id ,
114+ ranges : message . ranges ,
115+ baseURI : message . base ,
108116 onProgress : ( aProgress ) => {
109117 try {
110118 const progress = Math . round ( aProgress * 100 ) ;
111119 browser . tabs . sendMessage ( sender . tab . id , {
112- type : kNOTIFY_MATCH_ALL_PROGRESS ,
113- progress : progress ,
120+ type : Constants . kNOTIFY_MATCH_ALL_PROGRESS ,
121+ progress : progress ,
114122 showInContent : configs . showProgress
115123 } ) ;
116- if ( gLastContextTab == sender . tab . id )
124+ if ( mLastContextTab == sender . tab . id )
117125 browser . menus . update ( 'waiting' , {
118126 title : browser . i18n . getMessage ( `menu_waiting_label` , [ progress ] )
119127 } ) ;
120128 }
121- catch ( e ) {
129+ catch ( _error ) {
122130 }
123131 }
124132 } ) ;
@@ -133,17 +141,17 @@ browser.runtime.onMessage.addListener((message, sender) => {
133141
134142function detectActionFromEvent ( event ) {
135143 const baseType = event . inEditable ? 'actionInEditable' : 'action' ;
136- for ( const name of Object . keys ( kACTION_NAME_TO_ID ) ) {
144+ for ( const name of Object . keys ( Constants . kACTION_NAME_TO_ID ) ) {
137145 const base = `${ baseType } _${ name } _${ event . type } ` ;
138146 if ( ! configs [ base ] ||
139147 configs [ `${ base } _alt` ] != event . altKey ||
140148 configs [ `${ base } _ctrl` ] != event . ctrlKey ||
141149 configs [ `${ base } _meta` ] != event . metaKey ||
142150 configs [ `${ base } _shift` ] != event . shiftKey )
143151 continue ;
144- return kACTION_NAME_TO_ID [ name ] ;
152+ return Constants . kACTION_NAME_TO_ID [ name ] ;
145153 }
146- return kACTION_DISABLED ;
154+ return Constants . kACTION_DISABLED ;
147155}
148156
149157const MENU_ITEMS = [
@@ -153,8 +161,6 @@ const MENU_ITEMS = [
153161 'copy'
154162] ;
155163
156- let gLastContextTab = 0 ;
157-
158164browser . menus . create ( {
159165 id : 'waiting' ,
160166 title : browser . i18n . getMessage ( `menu_waiting_label` , [ 0 ] ) ,
@@ -170,7 +176,7 @@ browser.menus.create({
170176for ( const id of MENU_ITEMS ) {
171177 browser . menus . create ( {
172178 id,
173- title : id ,
179+ title : id ,
174180 visible : false ,
175181 contexts : [ 'selection' ]
176182 } ) ;
@@ -207,9 +213,9 @@ async function initContextMenuForWaiting(tabId) {
207213 return ;
208214 }
209215
210- gLastContextTab = tabId ;
216+ mLastContextTab = tabId ;
211217 const progress = await browser . tabs . sendMessage ( tabId , {
212- type : kCOMMAND_FETCH_MATCH_ALL_PROGRESS
218+ type : Constants . kCOMMAND_FETCH_MATCH_ALL_PROGRESS
213219 } ) ;
214220 browser . menus . update ( 'waiting' , {
215221 title : browser . i18n . getMessage ( `menu_waiting_label` , [ progress || 0 ] ) ,
@@ -228,8 +234,8 @@ function initContextMenuForURIs(uris) {
228234 }
229235
230236 const first = getShortURIString ( uris [ 0 ] ) ;
231- const last = getShortURIString ( uris [ uris . length - 1 ] ) ;
232- const type = uris . length == 1 ? 'single' : 'multiple' ;
237+ const last = getShortURIString ( uris [ uris . length - 1 ] ) ;
238+ const type = uris . length == 1 ? 'single' : 'multiple' ;
233239
234240 let visibleCount = 0 ;
235241 const visibility = { } ;
@@ -282,29 +288,29 @@ browser.menus.onClicked.addListener((info, tab) => {
282288 switch ( info . menuItemId . replace ( / ^ g r o u p e d : / , '' ) ) {
283289 case 'openCurrent' :
284290 browser . tabs . sendMessage ( tab . id , {
285- type : kCOMMAND_ACTION_FOR_URIS ,
286- action : kACTION_OPEN_IN_CURRENT
291+ type : Constants . kCOMMAND_ACTION_FOR_URIS ,
292+ action : Constants . kACTION_OPEN_IN_CURRENT
287293 } ) ;
288294 break ;
289295
290296 case 'openTab' :
291297 browser . tabs . sendMessage ( tab . id , {
292- type : kCOMMAND_ACTION_FOR_URIS ,
293- action : kACTION_OPEN_IN_TAB
298+ type : Constants . kCOMMAND_ACTION_FOR_URIS ,
299+ action : Constants . kACTION_OPEN_IN_TAB
294300 } ) ;
295301 break ;
296302
297303 case 'openWindow' :
298304 browser . tabs . sendMessage ( tab . id , {
299- type : kCOMMAND_ACTION_FOR_URIS ,
300- action : kACTION_OPEN_IN_WINDOW
305+ type : Constants . kCOMMAND_ACTION_FOR_URIS ,
306+ action : Constants . kACTION_OPEN_IN_WINDOW
301307 } ) ;
302308 break ;
303309
304310 case 'copy' :
305311 browser . tabs . sendMessage ( tab . id , {
306- type : kCOMMAND_ACTION_FOR_URIS ,
307- action : kACTION_COPY
312+ type : Constants . kCOMMAND_ACTION_FOR_URIS ,
313+ action : Constants . kACTION_COPY
308314 } ) ;
309315 break ;
310316 }
@@ -313,7 +319,7 @@ browser.menus.onClicked.addListener((info, tab) => {
313319
314320browser . tabs . onActivated . addListener ( async ( activeInfo ) => {
315321 const ranges = await browser . tabs . sendMessage ( activeInfo . tabId , {
316- type : kCOMMAND_FETCH_URI_RANGES
322+ type : Constants . kCOMMAND_FETCH_URI_RANGES
317323 } ) ;
318324 initContextMenuForURIs ( ranges . map ( result => result . uri ) ) ;
319325} ) ;
@@ -325,7 +331,7 @@ browser.windows.onFocusChanged.addListener(async (windowId) => {
325331
326332 const activeTab = window . tabs . filter ( tab => tab . active ) [ 0 ] ;
327333 const ranges = await browser . tabs . sendMessage ( activeTab . id , {
328- type : kCOMMAND_FETCH_URI_RANGES
334+ type : Constants . kCOMMAND_FETCH_URI_RANGES
329335 } ) ;
330336 initContextMenuForURIs ( ranges . map ( result => result . uri ) ) ;
331337} ) ;
0 commit comments