@@ -129,71 +129,6 @@ current_browser.runtime.onMessage.addListener(function(request, sender, sendResp
129129 }
130130} ) ;
131131
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-
197132// Add to Chrome context menu
198133current_browser . contextMenus . create ( {
199134 title : 'Download with uGet' ,
@@ -204,13 +139,9 @@ current_browser.contextMenus.create({
204139current_browser . contextMenus . onClicked . addListener ( function ( info , tab ) {
205140 "use strict" ;
206141 if ( info . menuItemId === "download_with_uget" ) {
207- clearMessage ( ) ;
208- chrome . cookies . getAll ( { 'url' : extractRootURL ( info . pageUrl ) } , parseCookies ) ;
209142 message . url = info [ 'linkUrl' ] ;
210143 message . referrer = info [ 'pageUrl' ] ;
211- message . cookies = cookies ;
212- sendMessageToHost ( message ) ;
213- clearMessage ( ) ;
144+ current_browser . cookies . getAll ( { 'url' : extractRootURL ( info . pageUrl ) } , parseCookies ) ;
214145 }
215146} ) ;
216147
@@ -236,23 +167,17 @@ current_browser.downloads.onCreated.addListener(function(downloadItem) {
236167 return ;
237168 }
238169 // 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+ } ) ;
247175
248- clearMessage ( ) ;
249- chrome . cookies . getAll ( { 'url' : extractRootURL ( info . pageUrl ) } , parseCookies ) ;
250176 message . url = url ;
251177 message . filename = downloadItem [ 'filename' ] ;
252178 message . filesize = fileSize ;
253179 message . referrer = downloadItem [ 'referrer' ] ;
254- message . cookies = cookies ;
255- sendMessageToHost ( message ) ;
180+ current_browser . cookies . getAll ( { 'url' : extractRootURL ( url ) } , parseCookies ) ;
256181} ) ;
257182
258183current_browser . webRequest . onBeforeRequest . addListener ( function ( details ) {
@@ -275,7 +200,6 @@ current_browser.webRequest.onBeforeRequest.addListener(function(details) {
275200 'requestBody'
276201] ) ;
277202current_browser . webRequest . onBeforeSendHeaders . addListener ( function ( details ) {
278- clearMessage ( ) ;
279203 currRequest ++ ;
280204 if ( currRequest > 2 )
281205 currRequest = 2 ;
@@ -379,10 +303,7 @@ current_browser.webRequest.onHeadersReceived.addListener(function(details) {
379303 if ( details . method != "POST" ) {
380304 message . postdata = '' ;
381305 }
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 ) ;
386307 var scheme = / ^ h t t p s / . test ( details . url ) ? 'https' : 'http' ;
387308 if ( chromeVersion >= 35 || firefoxVersion >= 51 ) {
388309 return {
@@ -411,8 +332,9 @@ current_browser.webRequest.onHeadersReceived.addListener(function(details) {
411332 return {
412333 cancel : true
413334 } ;
335+ } else {
336+ clearMessage ( ) ;
414337 }
415- clearMessage ( ) ;
416338 return {
417339 responseHeaders : details . responseHeaders
418340 } ;
@@ -429,26 +351,109 @@ current_browser.webRequest.onHeadersReceived.addListener(function(details) {
429351 'blocking'
430352] ) ;
431353
354+
432355/**
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+ */
436441function updateKeywords ( include , exclude ) {
437442 keywordsToInclude = include . split ( / [ \s , ] + / ) ;
438443 keywordsToExclude = exclude . split ( / [ \s , ] + / ) ;
439444}
440445
441446/**
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+ */
445450function updateMinFileSize ( size ) {
446451 minFileSizeToInterrupt = size ;
447452}
448453
449454/**
450- * Check whether not to interrupt the given url.
451- */
455+ * Check whether not to interrupt the given url.
456+ */
452457function isBlackListed ( url ) {
453458 if ( ! url ) {
454459 return ;
@@ -465,11 +470,10 @@ function isBlackListed(url) {
465470}
466471
467472/**
468- * Check whether to interrupt the given url or not.
469- */
473+ * Check whether to interrupt the given url or not.
474+ */
470475function isWhiteListed ( url ) {
471476 for ( var keyword of keywordsToInclude ) {
472- console . log ( keyword + " " + url ) ;
473477 if ( url . includes ( keyword ) ) {
474478 return true ;
475479 }
@@ -478,8 +482,8 @@ function isWhiteListed(url) {
478482}
479483
480484/**
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+ */
483487function setInterruptDownload ( interrupt , writeToStorage ) {
484488 interruptDownloads = interrupt ;
485489 if ( interrupt ) {
0 commit comments