@@ -148,7 +148,6 @@ function add(down) {
148
148
}
149
149
150
150
function postaria2obj ( addobj , callback = null ) {
151
- var httppost = new XMLHttpRequest ( ) ;
152
151
var aria2jsonrpcpath = getStorage ( 'path' ) ;
153
152
154
153
var result = parse_url ( aria2jsonrpcpath ) ;
@@ -161,23 +160,27 @@ function postaria2obj(addobj, callback = null) {
161
160
}
162
161
}
163
162
164
- httppost . open ( 'POST' , result [ 0 ] + '?tm=' + ( new Date ( ) ) . getTime ( ) . toString ( ) , true ) ;
165
- httppost . setRequestHeader ( 'Authorization' , auth ) ;
166
-
167
- httppost . onerror = function ( ) {
168
- console . log ( 'Error aria2 configuration!' ) ;
169
- if ( addobj [ 0 ] && addobj [ 0 ] . method === 'aria2.addUri' ) {
170
- notice ( 'Error adding tasks to aria2,please check the configuration!' , 'Error' ) ;
163
+ fetch ( result [ 0 ] + '?tm=' + ( new Date ( ) ) . getTime ( ) . toString ( ) , {
164
+ method : 'POST' ,
165
+ body : JSON . stringify ( addobj ) ,
166
+ headers : {
167
+ 'Authorization' : auth ,
168
+ 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
171
169
}
172
- } ;
173
- httppost . addEventListener ( 'load' , function ( ) {
174
- let rt = JSON . parse ( this . responseText )
170
+ } ) . then ( ( response ) => {
171
+ return response . json ( )
172
+ } ) . then ( ( data ) => {
175
173
if ( callback ) {
176
- callback ( rt )
174
+ callback ( data )
175
+ }
176
+ } ) . catch ( ( error ) => {
177
+ console . error ( 'Error:' , error )
178
+ console . log ( 'Error aria2 configuration!' ) ;
179
+ if ( addobj && addobj . method === 'aria2.addUri' ) {
180
+ notice ( 'Error adding tasks to aria2,please check the configuration!' , 'Error' ) ;
177
181
}
178
182
} )
179
- httppost . setRequestHeader ( 'Content-Type' , 'application/x-www-form-urlencoded; charset=UTF-8' ) ;
180
- httppost . send ( JSON . stringify ( addobj ) ) ;
183
+
181
184
return 'ok' ;
182
185
183
186
}
0 commit comments