Skip to content

Instantly share code, notes, and snippets.

@binux
Created July 15, 2012 13:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save binux/3116833 to your computer and use it in GitHub Desktop.
Save binux/3116833 to your computer and use it in GitHub Desktop.
aria2 jsonrpc client
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