-
-
Save kurohai/5d421d12386f8823731bbd6fef330db1 to your computer and use it in GitHub Desktop.
aria2 jsonrpc client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ARIA2 = (function() { | |
var jsonrpc_version = '2.0'; | |
function get_auth(url) { | |
return url.match(/^(?:(?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(?:\/\/)?(?:([^:@]*(?::[^:@]*)?)?@)?/)[1]; | |
}; | |
function request(jsonrpc_path, method, params) { | |
var request_obj = { | |
jsonrpc: jsonrpc_version, | |
method: method, | |
id: (new Date()).getTime().toString(), | |
}; | |
if (params) request_obj['params'] = params; | |
var xhr = new XMLHttpRequest(); | |
var auth = get_auth(jsonrpc_path); | |
xhr.open("POST", jsonrpc_path+"?tm="+(new Date()).getTime().toString(), true); | |
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); | |
if (auth) xhr.setRequestHeader("Authorization", "Basic "+btoa(auth)); | |
xhr.send(JSON.stringify(request_obj)); | |
}; | |
return function(jsonrpc_path) { | |
this.jsonrpc_path = jsonrpc_path; | |
this.addUri = function (uri, options) { | |
request(this.jsonrpc_path, 'aria2.addUri', [[uri, ], options]); | |
}; | |
return this; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment