@@ -129,71 +129,6 @@ current_browser.runtime.onMessage.addListener(function(request, sender, sendResp
129
129
}
130
130
} ) ;
131
131
132
- // Send message to the uget-chrome-wrapper
133
- function sendMessageToHost ( message ) {
134
- current_browser . runtime . sendNativeMessage ( hostName , message , function ( response ) {
135
- ugetWrapperNotFound = ( response == null ) ;
136
- if ( ! ugetWrapperNotFound && ! ugetChromeWrapperVersion ) {
137
- ugetChromeWrapperVersion = response . version ;
138
- ugetVersion = response . uget ;
139
- }
140
- } ) ;
141
- }
142
-
143
- function getInfo ( ) {
144
- if ( ugetWrapperNotFound || ! ugetChromeWrapperVersion ) {
145
- return "Error: Unable to connect to the uget-chrome-wrapper" ;
146
- } else if ( ! ugetChromeWrapperVersion . startsWith ( "2." ) ) {
147
- return "Warning: Please update the uget-chrome-wrapper to the latest version" ;
148
- } else {
149
- return "Info: Found uGet: " + ugetVersion + " and uget-chrome-wrapper: " + ugetChromeWrapperVersion ;
150
- }
151
- }
152
-
153
- function clearMessage ( ) {
154
- message . url = '' ;
155
- message . cookies = '' ;
156
- message . filename = '' ;
157
- message . filesize = '' ;
158
- message . referrer = '' ;
159
- message . useragent = '' ;
160
- }
161
-
162
- function postParams ( source ) {
163
- var array = [ ] ;
164
- for ( var key in source ) {
165
- array . push ( encodeURIComponent ( key ) + '=' + encodeURIComponent ( source [ key ] ) ) ;
166
- }
167
- return array . join ( '&' ) ;
168
- }
169
-
170
- function extractRootURL ( url ) {
171
- var domain ;
172
-
173
- if ( url . indexOf ( "://" ) > - 1 ) {
174
- domain = url . split ( '/' ) [ 0 ] + '/' + url . split ( '/' ) [ 1 ] + '/' + url . split ( '/' ) [ 2 ] ;
175
- } else {
176
- domain = url . split ( '/' ) [ 0 ] ;
177
- }
178
-
179
- return domain ;
180
- }
181
-
182
- function parseCookies ( cookies_arr ) {
183
- cookies = '' ;
184
-
185
- for ( var i in cookies_arr ) {
186
- cookies += cookies_arr [ i ] . domain + '\t' ;
187
- cookies += ( cookies_arr [ i ] . httpOnly ? "FALSE" : "TRUE" ) + '\t' ;
188
- cookies += cookies_arr [ i ] . path + '\t' ;
189
- cookies += ( cookies_arr [ i ] . secure ? "TRUE" : "FALSE" ) + '\t' ;
190
- cookies += Math . round ( cookies_arr [ i ] . expirationDate ) + '\t' ;
191
- cookies += cookies_arr [ i ] . name + '\t' ;
192
- cookies += cookies_arr [ i ] . value ;
193
- cookies += '\n' ;
194
- }
195
- }
196
-
197
132
// Add to Chrome context menu
198
133
current_browser . contextMenus . create ( {
199
134
title : 'Download with uGet' ,
@@ -204,13 +139,9 @@ current_browser.contextMenus.create({
204
139
current_browser . contextMenus . onClicked . addListener ( function ( info , tab ) {
205
140
"use strict" ;
206
141
if ( info . menuItemId === "download_with_uget" ) {
207
- clearMessage ( ) ;
208
- chrome . cookies . getAll ( { 'url' : extractRootURL ( info . pageUrl ) } , parseCookies ) ;
209
142
message . url = info [ 'linkUrl' ] ;
210
143
message . referrer = info [ 'pageUrl' ] ;
211
- message . cookies = cookies ;
212
- sendMessageToHost ( message ) ;
213
- clearMessage ( ) ;
144
+ current_browser . cookies . getAll ( { 'url' : extractRootURL ( info . pageUrl ) } , parseCookies ) ;
214
145
}
215
146
} ) ;
216
147
@@ -236,23 +167,17 @@ current_browser.downloads.onCreated.addListener(function(downloadItem) {
236
167
return ;
237
168
}
238
169
// Cancel the download
239
- current_browser . downloads . cancel ( downloadItem . id ) . then (
240
- function ( ) {
241
- // Erase the download from list
242
- current_browser . downloads . erase ( {
243
- id : downloadItem . id
244
- } ) ; // ignore callbacks
245
- }
246
- ) ;
170
+ current_browser . downloads . cancel ( downloadItem . id ) ;
171
+ // Erase the download from list
172
+ current_browser . downloads . erase ( {
173
+ id : downloadItem . id
174
+ } ) ;
247
175
248
- clearMessage ( ) ;
249
- chrome . cookies . getAll ( { 'url' : extractRootURL ( info . pageUrl ) } , parseCookies ) ;
250
176
message . url = url ;
251
177
message . filename = downloadItem [ 'filename' ] ;
252
178
message . filesize = fileSize ;
253
179
message . referrer = downloadItem [ 'referrer' ] ;
254
- message . cookies = cookies ;
255
- sendMessageToHost ( message ) ;
180
+ current_browser . cookies . getAll ( { 'url' : extractRootURL ( url ) } , parseCookies ) ;
256
181
} ) ;
257
182
258
183
current_browser . webRequest . onBeforeRequest . addListener ( function ( details ) {
@@ -275,7 +200,6 @@ current_browser.webRequest.onBeforeRequest.addListener(function(details) {
275
200
'requestBody'
276
201
] ) ;
277
202
current_browser . webRequest . onBeforeSendHeaders . addListener ( function ( details ) {
278
- clearMessage ( ) ;
279
203
currRequest ++ ;
280
204
if ( currRequest > 2 )
281
205
currRequest = 2 ;
@@ -379,10 +303,7 @@ current_browser.webRequest.onHeadersReceived.addListener(function(details) {
379
303
if ( details . method != "POST" ) {
380
304
message . postdata = '' ;
381
305
}
382
- chrome . cookies . getAll ( { 'url' : extractRootURL ( message . url ) } , parseCookies ) ;
383
- message . cookies = cookies ;
384
- sendMessageToHost ( message ) ;
385
- message . postdata = '' ;
306
+ current_browser . cookies . getAll ( { 'url' : extractRootURL ( message . url ) } , parseCookies ) ;
386
307
var scheme = / ^ h t t p s / . test ( details . url ) ? 'https' : 'http' ;
387
308
if ( chromeVersion >= 35 || firefoxVersion >= 51 ) {
388
309
return {
@@ -411,8 +332,9 @@ current_browser.webRequest.onHeadersReceived.addListener(function(details) {
411
332
return {
412
333
cancel : true
413
334
} ;
335
+ } else {
336
+ clearMessage ( ) ;
414
337
}
415
- clearMessage ( ) ;
416
338
return {
417
339
responseHeaders : details . responseHeaders
418
340
} ;
@@ -429,26 +351,109 @@ current_browser.webRequest.onHeadersReceived.addListener(function(details) {
429
351
'blocking'
430
352
] ) ;
431
353
354
+
432
355
/**
433
- * Update the include & exclude keywords.
434
- * Is called from the popup.js.
435
- */
356
+ * Send message to the uget-chrome-wrapper
357
+ */
358
+ function sendMessageToHost ( message ) {
359
+ current_browser . runtime . sendNativeMessage ( hostName , message , function ( response ) {
360
+ clearMessage ( ) ;
361
+ ugetWrapperNotFound = ( response == null ) ;
362
+ if ( ! ugetWrapperNotFound && ! ugetChromeWrapperVersion ) {
363
+ ugetChromeWrapperVersion = response . version ;
364
+ ugetVersion = response . uget ;
365
+ }
366
+ } ) ;
367
+ }
368
+
369
+ /**
370
+ * Create a meaningful message of the internal state.
371
+ */
372
+ function getInfo ( ) {
373
+ if ( ugetWrapperNotFound || ! ugetChromeWrapperVersion ) {
374
+ return "Error: Unable to connect to the uget-chrome-wrapper" ;
375
+ } else if ( ! ugetChromeWrapperVersion . startsWith ( "2." ) ) {
376
+ return "Warning: Please update the uget-chrome-wrapper to the latest version" ;
377
+ } else {
378
+ return "Info: Found uGet: " + ugetVersion + " and uget-chrome-wrapper: " + ugetChromeWrapperVersion ;
379
+ }
380
+ }
381
+
382
+ /**
383
+ * Clear the message.
384
+ */
385
+ function clearMessage ( ) {
386
+ message . url = '' ;
387
+ message . cookies = '' ;
388
+ message . filename = '' ;
389
+ message . filesize = '' ;
390
+ message . referrer = '' ;
391
+ message . useragent = '' ;
392
+ }
393
+
394
+ /**
395
+ * Extract the POST parameters from a form data.
396
+ */
397
+ function postParams ( source ) {
398
+ var array = [ ] ;
399
+ for ( var key in source ) {
400
+ array . push ( encodeURIComponent ( key ) + '=' + encodeURIComponent ( source [ key ] ) ) ;
401
+ }
402
+ return array . join ( '&' ) ;
403
+ }
404
+
405
+ /**
406
+ * Extract the root of a URL.
407
+ */
408
+ function extractRootURL ( url ) {
409
+ var domain ;
410
+ if ( url . indexOf ( "://" ) > - 1 ) {
411
+ domain = url . split ( '/' ) [ 0 ] + '/' + url . split ( '/' ) [ 1 ] + '/' + url . split ( '/' ) [ 2 ] ;
412
+ } else {
413
+ domain = url . split ( '/' ) [ 0 ] ;
414
+ }
415
+ return domain ;
416
+ }
417
+
418
+ /**
419
+ * Parse the cookies and send the message to the native host.
420
+ */
421
+ function parseCookies ( cookies_arr ) {
422
+ cookies = '' ;
423
+ for ( var i in cookies_arr ) {
424
+ cookies += cookies_arr [ i ] . domain + '\t' ;
425
+ cookies += ( cookies_arr [ i ] . httpOnly ? "FALSE" : "TRUE" ) + '\t' ;
426
+ cookies += cookies_arr [ i ] . path + '\t' ;
427
+ cookies += ( cookies_arr [ i ] . secure ? "TRUE" : "FALSE" ) + '\t' ;
428
+ cookies += Math . round ( cookies_arr [ i ] . expirationDate ) + '\t' ;
429
+ cookies += cookies_arr [ i ] . name + '\t' ;
430
+ cookies += cookies_arr [ i ] . value ;
431
+ cookies += '\n' ;
432
+ }
433
+ message . cookies = cookies ;
434
+ sendMessageToHost ( message ) ;
435
+ }
436
+
437
+ /**
438
+ * Update the include & exclude keywords.
439
+ * Is called from the popup.js.
440
+ */
436
441
function updateKeywords ( include , exclude ) {
437
442
keywordsToInclude = include . split ( / [ \s , ] + / ) ;
438
443
keywordsToExclude = exclude . split ( / [ \s , ] + / ) ;
439
444
}
440
445
441
446
/**
442
- * Update the minimum file size to interrupt.
443
- * Is called from the popup.js.
444
- */
447
+ * Update the minimum file size to interrupt.
448
+ * Is called from the popup.js.
449
+ */
445
450
function updateMinFileSize ( size ) {
446
451
minFileSizeToInterrupt = size ;
447
452
}
448
453
449
454
/**
450
- * Check whether not to interrupt the given url.
451
- */
455
+ * Check whether not to interrupt the given url.
456
+ */
452
457
function isBlackListed ( url ) {
453
458
if ( ! url ) {
454
459
return ;
@@ -465,11 +470,10 @@ function isBlackListed(url) {
465
470
}
466
471
467
472
/**
468
- * Check whether to interrupt the given url or not.
469
- */
473
+ * Check whether to interrupt the given url or not.
474
+ */
470
475
function isWhiteListed ( url ) {
471
476
for ( var keyword of keywordsToInclude ) {
472
- console . log ( keyword + " " + url ) ;
473
477
if ( url . includes ( keyword ) ) {
474
478
return true ;
475
479
}
@@ -478,8 +482,8 @@ function isWhiteListed(url) {
478
482
}
479
483
480
484
/**
481
- * Enable/Disable the plugin and update the plugin icon based on the state.
482
- */
485
+ * Enable/Disable the plugin and update the plugin icon based on the state.
486
+ */
483
487
function setInterruptDownload ( interrupt , writeToStorage ) {
484
488
interruptDownloads = interrupt ;
485
489
if ( interrupt ) {
0 commit comments