@@ -4,19 +4,26 @@ const ID3Writer = require('browser-id3-writer');
44
55let state = {
66 album : { } ,
7- tabId : null
7+ tabId : null ,
8+ intervalId : '' ,
9+ downloads : { }
810} ;
911
1012function onStartedDownload ( id ) {
13+ state . downloads [ id ] = true ;
1114 console . log ( `Started downloading: ${ id } ` ) ;
1215}
1316
1417function onFailed ( error ) {
15- console . log ( `Download failed: ${ error } ` ) ;
18+ state . downloads [ Math . random ( ) . toString ( ) ] = false ;
19+ clearInterval ( state . intervalId ) ;
20+ const { tabId } = state ;
21+ browser . pageAction . setIcon ( { tabId, path : `./icons/camper-died.png` } ) ;
22+ console . log ( `Download failed: ${ error . message } ` ) ;
1623}
1724
1825function sanitize ( str ) {
19- return str . replace ( / [ \\ \/ $ ' " ] / g, '' ) ;
26+ return str . replaceAll ( / [ \\ \/ $ ' " ] / g, '' ) . replaceAll ( ':' , '' ) ;
2027}
2128
2229function loadFile ( url , processResponse ) {
@@ -36,8 +43,50 @@ function loadFile(url, processResponse) {
3643 xhr . send ( ) ;
3744}
3845
46+
47+ // bugs out on second download?
48+ function animateProgressIcon ( ) {
49+ const { tabId, downloads } = state ;
50+ const frames = [ 0 , 1 , 2 , 1 ] ;
51+ let index = 0 ;
52+
53+ setActiveIcon ( index ) ;
54+ state . intervalId = setInterval ( ( ) => {
55+ index ++ ;
56+ console . log ( index ) ;
57+ setActiveIcon ( frames [ index % 4 ] ) ;
58+ } , 250 ) ;
59+ }
60+
61+ function setActiveIcon ( frame = 1 ) {
62+ const { tabId } = state ;
63+ browser . pageAction . setIcon ( { tabId, path : `./icons/camper-active-${ frame } .png` } ) ;
64+ console . log ( 'frame' , frame , Date . now ( ) ) ;
65+ }
66+
67+ function downloadOnChanged ( event ) {
68+ const { id, state : { current } = { } } = event ;
69+ state . downloads [ id ] = current === 'complete' ;
70+ if ( Object . keys ( state . downloads ) . length === state . album . tracks . length + 1 &&
71+ Object . values ( state . downloads ) . every ( download => download ) ) {
72+ onComplete ( ) ;
73+ }
74+ }
75+
76+ function onComplete ( ) {
77+ clearInterval ( state . intervalId ) ;
78+ const setActiveFrame = setTimeout ( setActiveIcon , 250 ) ;
79+ browser . downloads . onChanged . removeListener ( downloadOnChanged ) ;
80+ state . downloads = { } ;
81+ }
82+
3983function download ( message , sender ) {
4084 const { album : { tracks, cover, folder, artist, date, title : albumTitle } } = state ;
85+
86+ animateProgressIcon ( ) ;
87+
88+ browser . downloads . onChanged . addListener ( downloadOnChanged ) ;
89+
4190 loadFile ( cover , coverData => {
4291 tracks . forEach ( ( { track_num : number , title, file : { 'mp3-128' : url } } ) => {
4392 const filename = `${ sanitize ( folder ) } /${ sanitize ( title ) } .mp3` ;
@@ -57,6 +106,7 @@ function download(message, sender) {
57106 taggedUrl = writer . getURL ( ) ;
58107
59108 const downloading = browser . downloads . download ( { filename, url : taggedUrl } ) ;
109+ console . log ( filename ) ;
60110 downloading . then ( onStartedDownload , onFailed ) ;
61111 } ) ;
62112 } ) ;
@@ -72,7 +122,7 @@ function onReceiveAlbum(album) {
72122 state . album = album ;
73123 browser . pageAction . show ( state . tabId ) ;
74124 browser . pageAction . onClicked . addListener ( download ) ;
75- browser . pageAction . setIcon ( { tabId : state . tabId , path : './icons/camper-active.png' } ) ;
125+ setActiveIcon ( ) ;
76126 }
77127
78128 return true ;
0 commit comments