1
+ function getNotificationOptions ( message ) {
2
+ return {
3
+ type : "basic" ,
4
+ message : message ,
5
+ title : "MediaCrush" ,
6
+ iconUrl : "icon48.png"
7
+ } ;
8
+ }
9
+
1
10
function rehostImage ( info , tab ) {
2
11
var xhr = new XMLHttpRequest ( ) ;
3
12
xhr . open ( 'POST' , 'https://mediacru.sh/api/upload/url' ) ;
13
+ var notificationId = - 1 ;
14
+ chrome . notifications . create ( "" , getNotificationOptions ( "Processing, please wait..." ) , function ( id ) {
15
+ notificationId = id ;
16
+ } ) ;
4
17
xhr . onload = function ( ) {
5
18
var result = JSON . parse ( this . responseText ) ;
6
19
if ( this . status == 409 ) {
20
+ chrome . notifications . clear ( notificationId , function ( ok ) { } ) ;
7
21
window . open ( 'https://mediacru.sh/' + result . hash , '_blank' ) ;
8
22
} else if ( this . status == 200 ) {
9
- var notification = webkitNotifications . createNotification ( 'icon48.png' , 'MediaCrush' , 'Processing, please wait...' ) ;
10
- notification . show ( ) ;
11
23
setTimeout ( function ( ) {
12
- checkStatus ( result . hash , notification ) ;
24
+ checkStatus ( result . hash , notificationId ) ;
13
25
} , 1000 ) ;
14
26
} else {
27
+ chrome . notifications . clear ( notificationId , function ( ok ) { } ) ;
15
28
alert ( 'An error occured re-hosting this image.' ) ;
16
29
}
17
30
} ;
@@ -20,19 +33,19 @@ function rehostImage(info, tab) {
20
33
xhr . send ( formData ) ;
21
34
}
22
35
23
- function checkStatus ( hash , notification ) {
36
+ function checkStatus ( hash , notificationId ) {
24
37
var xhr = new XMLHttpRequest ( ) ;
25
38
xhr . open ( 'GET' , 'https://mediacru.sh/api/' + hash + '/status' ) ;
26
39
xhr . onload = function ( ) {
27
40
var result = JSON . parse ( this . responseText ) ;
28
41
if ( result . status == "error" ) {
29
- notification . cancel ( ) ;
30
- notification = webkitNotifications . createNotification ( 'icon48.png' , 'MediaCrush' , ' There was an error while processing this file.') ;
42
+ chrome . notifications . clear ( notificationId , function ( ok ) { } ) ;
43
+ chrome . notifications . create ( "" , getNotificationOptions ( ' There was an error while processing this file.') , function ( id ) { } ) ;
31
44
} else if ( result . status == "timeout" ) {
32
- notification . cancel ( ) ;
33
- notification = webkitNotifications . createNotification ( 'icon48.png' , 'MediaCrush' , ' This file took too long to process.') ;
45
+ chrome . notifications . clear ( notificationId , function ( ok ) { } ) ;
46
+ chrome . notifications . create ( "" , getNotificationOptions ( ' This file took too long to process.') , function ( id ) { } ) ;
34
47
} else if ( result . status == "done" ) {
35
- notification . cancel ( ) ;
48
+ chrome . notifications . clear ( notificationId , function ( ok ) { } ) ;
36
49
window . open ( 'https://mediacru.sh/' + result . hash + '#fromExtension' , '_blank' ) ;
37
50
} else {
38
51
setTimeout ( function ( ) { checkStatus ( hash , notification ) ; } , 1000 ) ;
0 commit comments