@@ -50,6 +50,10 @@ class BrowserTabs {
50
50
throw new Error ( 'getActive is not implemented' ) ;
51
51
}
52
52
53
+ getActiveScreenshot ( onSuccess ) {
54
+ throw new Error ( 'getActiveScreenshot is not implemented' ) ;
55
+ }
56
+
53
57
runScript ( tab_id , script , payload , onSuccess , onError ) {
54
58
throw new Error ( 'runScript is not implemented' ) ;
55
59
}
@@ -68,10 +72,30 @@ class FirefoxTabs extends BrowserTabs {
68
72
}
69
73
70
74
query ( queryInfo , onSuccess ) {
71
- this . _browser . tabs . query ( queryInfo ) . then (
72
- onSuccess ,
73
- ( error ) => console . log ( `Error executing queryTabs: ${ error } ` )
74
- ) ;
75
+ if ( queryInfo . hasOwnProperty ( 'windowFocused' ) ) {
76
+ let keepFocused = queryInfo [ 'windowFocused' ]
77
+ delete queryInfo . windowFocused ;
78
+ this . _browser . tabs . query ( queryInfo ) . then (
79
+ tabs => {
80
+ Promise . all ( tabs . map ( tab => {
81
+ return new Promise ( resolve => {
82
+ this . _browser . windows . get ( tab . windowId , { populate : false } , window => {
83
+ resolve ( window . focused === keepFocused ? tab : null ) ;
84
+ } ) ;
85
+ } ) ;
86
+ } ) ) . then ( result => {
87
+ tabs = result . filter ( tab => tab !== null ) ;
88
+ onSuccess ( tabs ) ;
89
+ } ) ;
90
+ } ,
91
+ ( error ) => console . log ( `Error executing queryTabs: ${ error } ` )
92
+ ) ;
93
+ } else {
94
+ this . _browser . tabs . query ( queryInfo ) . then (
95
+ onSuccess ,
96
+ ( error ) => console . log ( `Error executing queryTabs: ${ error } ` )
97
+ ) ;
98
+ }
75
99
}
76
100
77
101
close ( tab_ids , onSuccess ) {
@@ -100,10 +124,17 @@ class FirefoxTabs extends BrowserTabs {
100
124
}
101
125
102
126
create ( createOptions , onSuccess ) {
103
- this . _browser . tabs . create ( createOptions ) . then (
104
- onSuccess ,
105
- ( error ) => console . log ( `Error: ${ error } ` )
106
- ) ;
127
+ if ( createOptions . windowId === 0 ) {
128
+ this . _browser . windows . create ( { url : createOptions . url } ) . then (
129
+ onSuccess ,
130
+ ( error ) => console . log ( `Error: ${ error } ` )
131
+ ) ;
132
+ } else {
133
+ this . _browser . tabs . create ( createOptions ) . then (
134
+ onSuccess ,
135
+ ( error ) => console . log ( `Error: ${ error } ` )
136
+ ) ;
137
+ }
107
138
}
108
139
109
140
getActive ( onSuccess ) {
@@ -113,6 +144,29 @@ class FirefoxTabs extends BrowserTabs {
113
144
) ;
114
145
}
115
146
147
+ getActiveScreenshot ( onSuccess ) {
148
+ let queryOptions = { active : true , lastFocusedWindow : true } ;
149
+ this . _browser . tabs . query ( queryOptions ) . then (
150
+ ( tabs ) => {
151
+ let tab = tabs [ 0 ] ;
152
+ let windowId = tab . windowId ;
153
+ let tabId = tab . id ;
154
+ this . _browser . tabs . captureVisibleTab ( windowId , { format : 'png' } ) . then (
155
+ function ( data ) {
156
+ const message = {
157
+ tab : tabId ,
158
+ window : windowId ,
159
+ data : data
160
+ } ;
161
+ onSuccess ( message ) ;
162
+ } ,
163
+ ( error ) => console . log ( `Error: ${ error } ` )
164
+ ) ;
165
+ } ,
166
+ ( error ) => console . log ( `Error: ${ error } ` )
167
+ ) ;
168
+ }
169
+
116
170
runScript ( tab_id , script , payload , onSuccess , onError ) {
117
171
this . _browser . tabs . executeScript ( tab_id , { code : script } ) . then (
118
172
( result ) => onSuccess ( result , payload ) ,
@@ -145,7 +199,24 @@ class ChromeTabs extends BrowserTabs {
145
199
}
146
200
147
201
query ( queryInfo , onSuccess ) {
148
- this . _browser . tabs . query ( queryInfo , onSuccess ) ;
202
+ if ( queryInfo . hasOwnProperty ( 'windowFocused' ) ) {
203
+ let keepFocused = queryInfo [ 'windowFocused' ]
204
+ delete queryInfo . windowFocused ;
205
+ this . _browser . tabs . query ( queryInfo , tabs => {
206
+ Promise . all ( tabs . map ( tab => {
207
+ return new Promise ( resolve => {
208
+ this . _browser . windows . get ( tab . windowId , { populate : false } , window => {
209
+ resolve ( window . focused === keepFocused ? tab : null ) ;
210
+ } ) ;
211
+ } ) ;
212
+ } ) ) . then ( result => {
213
+ tabs = result . filter ( tab => tab !== null ) ;
214
+ onSuccess ( tabs ) ;
215
+ } ) ;
216
+ } ) ;
217
+ } else {
218
+ this . _browser . tabs . query ( queryInfo , onSuccess ) ;
219
+ }
149
220
}
150
221
151
222
close ( tab_ids , onSuccess ) {
@@ -169,13 +240,35 @@ class ChromeTabs extends BrowserTabs {
169
240
}
170
241
171
242
create ( createOptions , onSuccess ) {
172
- this . _browser . tabs . create ( createOptions , onSuccess ) ;
243
+ if ( createOptions . windowId === 0 ) {
244
+ this . _browser . windows . create ( { url : createOptions . url } , onSuccess ) ;
245
+ } else {
246
+ this . _browser . tabs . create ( createOptions , onSuccess ) ;
247
+ }
173
248
}
174
249
175
250
getActive ( onSuccess ) {
176
251
this . _browser . tabs . query ( { active : true } , onSuccess ) ;
177
252
}
178
253
254
+ getActiveScreenshot ( onSuccess ) {
255
+ // this._browser.tabs.captureVisibleTab(null, { format: 'png' }, onSuccess);
256
+ let queryOptions = { active : true , lastFocusedWindow : true } ;
257
+ this . _browser . tabs . query ( queryOptions , ( tabs ) => {
258
+ let tab = tabs [ 0 ] ;
259
+ let windowId = tab . windowId ;
260
+ let tabId = tab . id ;
261
+ this . _browser . tabs . captureVisibleTab ( windowId , { format : 'png' } , function ( data ) {
262
+ const message = {
263
+ tab : tabId ,
264
+ window : windowId ,
265
+ data : data
266
+ } ;
267
+ onSuccess ( message ) ;
268
+ } ) ;
269
+ } ) ;
270
+ }
271
+
179
272
runScript ( tab_id , script , payload , onSuccess , onError ) {
180
273
this . _browser . tabs . executeScript (
181
274
tab_id , { code : script } ,
@@ -282,7 +375,7 @@ function queryTabs(query_info) {
282
375
283
376
integerKeys = { 'windowId' : null , 'index' : null } ;
284
377
booleanKeys = { 'active' : null , 'pinned' : null , 'audible' : null , 'muted' : null , 'highlighted' : null ,
285
- 'discarded' : null , 'autoDiscardable' : null , 'currentWindow' : null , 'lastFocusedWindow' : null } ;
378
+ 'discarded' : null , 'autoDiscardable' : null , 'currentWindow' : null , 'lastFocusedWindow' : null , 'windowFocused' : null } ;
286
379
287
380
query = Object . entries ( query ) . reduce ( ( o , [ k , v ] ) => {
288
381
if ( booleanKeys . hasOwnProperty ( k ) && typeof v != 'boolean' ) {
@@ -335,13 +428,23 @@ function closeTabs(tab_ids) {
335
428
browserTabs . close ( tab_ids , ( ) => port . postMessage ( 'OK' ) ) ;
336
429
}
337
430
338
- function openUrls ( urls , window_id ) {
431
+ function openUrls ( urls , window_id , first_result = "" ) {
339
432
if ( urls . length == 0 ) {
340
433
console . log ( 'Opening urls done' ) ;
341
434
port . postMessage ( [ ] ) ;
342
435
return ;
343
436
}
344
437
438
+ if ( window_id === 0 ) {
439
+ browserTabs . create ( { 'url' : urls [ 0 ] , windowId : 0 } , ( window ) => {
440
+ result = `${ window . id } .${ window . tabs [ 0 ] . id } ` ;
441
+ console . log ( `Opened first window: ${ result } ` ) ;
442
+ urls = urls . slice ( 1 ) ;
443
+ openUrls ( urls , window . id , result ) ;
444
+ } ) ;
445
+ return ;
446
+ }
447
+
345
448
var promises = [ ] ;
346
449
for ( let url of urls ) {
347
450
console . log ( `Opening another one url ${ url } ` ) ;
@@ -352,6 +455,9 @@ function openUrls(urls, window_id) {
352
455
} ) )
353
456
} ;
354
457
Promise . all ( promises ) . then ( result => {
458
+ if ( first_result !== "" ) {
459
+ result . unshift ( first_result ) ;
460
+ }
355
461
const data = Array . prototype . concat ( ...result )
356
462
console . log ( `Sending ids back: ${ JSON . stringify ( data ) } ` ) ;
357
463
port . postMessage ( data )
@@ -405,6 +511,12 @@ function getActiveTabs() {
405
511
} ) ;
406
512
}
407
513
514
+ function getActiveScreenshot ( ) {
515
+ browserTabs . getActiveScreenshot ( data => {
516
+ port . postMessage ( data ) ;
517
+ } ) ;
518
+ }
519
+
408
520
function getWordsScript ( match_regex , join_with ) {
409
521
return GET_WORDS_SCRIPT
410
522
. replace ( '#match_regex#' , match_regex )
@@ -609,6 +721,11 @@ port.onMessage.addListener((command) => {
609
721
getActiveTabs ( ) ;
610
722
}
611
723
724
+ else if ( command [ 'name' ] == 'get_screenshot' ) {
725
+ console . log ( 'Getting visible screenshot' ) ;
726
+ getActiveScreenshot ( ) ;
727
+ }
728
+
612
729
else if ( command [ 'name' ] == 'get_words' ) {
613
730
console . log ( 'Getting words from tab:' , command [ 'tab_id' ] ) ;
614
731
getWords ( command [ 'tab_id' ] , command [ 'match_regex' ] , command [ 'join_with' ] ) ;
0 commit comments