| Lastest | Extension | Web App |
|---|---|---|
| aria2.js | Download with Aria2 | Task Manager |
<script src="https://jc3213.github.io/aria2.js/aria2.js"></script>// @require https://jc3213.github.io/aria2.js/aria2.jslet aria2 = new Aria2("http://localhost:6800/jsonrpc", "mysecret");let aria2 = new Aria2("http://localhost:6800/jsonrpc#mysecret");let aria2 = new Aria2();
aria2.url = 'wss://example.com:433/jsonrpc';
aria2.secret = 'mysecret';- the URL of JSON-RPC
aria2.url = url;${scheme}://${hostname}:${port}/jsonrpc- scheme
httphttpswswss
- hostname
www.example.com
- port
6800default443ssl
- scheme
- the secret token
secret=your-secret-tokenin JSON-RPC configuration
aria2.secret = secret;string
- maximum retries when connection to JSON-RPC is closed
aria2.retries = retries;integer10: Default-1or other negative numbers for unlimited retries
- time interval between retries
aria2.timeout = timeout;integer10: Default, equivalent to 10000 millisecond- It is recommended to use numbers larger than
3
- connect to
WebSocketof aria2 JSON-RPC
aria2.connect();- disconnect from
WebSocketof aria2 JSON-RPC
aria2.disconnect();- send message to JSON-RPC
let response = aria2.call( { method, params } );
let response = aria2.call([ { method, params }, { method, params }, ..., { method, params } ]);- use
WebSocketorPOSTmethod based on scheme - response
Promiseobject, return an array that contains the response from JSON-RPC if fulfilled
- method required
- Read RPC method calls
- params optional
- JSON-RPC method call parameters
let { result } = await aria2.call( { method: 'aria2.tellActive' } );
console.log(result) // All downloading sessions;
let { result } = await aria2.call([ { method: 'aria2.getGlobalOption' }, { method: 'aria2.getVersion' } ]);
let [ [globalOption], [version] ] = result;
console.log(globalOption, version); // The options, version and enabled features of JSON-RPC;- callback function triggered when JSON-RPC connection is opened.
aria2.onopen = function(event) { ... };- callback function triggered when JSON-RPC connection is closed.
aria2.onclose = function(event) { ... };- callback function triggered when a message is received from JSON-RPC.
aria2.onmessage = function (response: object[]) { ... };let jsonrpc = {};
let session = {};
let keeplive;
let waiting = new Set([ 'waiting', 'paused' ]);
let dispatch = {
'aria2.onDownloadStart' (gid, result) {
console.log("The session #" + gid + " has started");
session.active[gid] = result;
if (session.waiting[gid]) {
delete session.waiting[gid];
}
},
'aria2.onDownloadComplete' (gid, result) {
console.log("The session #" + gid + " has completed");
dispatch.default(gid, result);
},
default (gid, result) {
if (session.active[gid]) {
delete session.active[gid];
if (waiting.has(result.status)) {
session.waiting[gid] = result;
} else {
session.stopped[gid] = result;
}
}
}
};
let aria2 = new Aria2("http://localhost:6800/jsonrpc#mysecret");
aria2.retries = -1;
aria2.onopen = async () => {
session.all = {};
session.active = {};
session.waiting = {};
session.stopped = {};
let { result: [
[options], [version], [stats], [active], [waiting], [stopped]
] } = await aria2.call([
{ method: 'aria2.getGlobalOption' },
{ method: 'aria2.getVersion' },
{ method: 'aria2.getGlobalStat' },
{ method: 'aria2.tellActive' },
{ method: 'aria2.tellWaiting', params: [0, 999] },
{ method: 'aria2.tellStopped', params: [0, 999] }
]);
jsonrpc.options = options;
jsonrpc.version = version;
jsonrpc.stats = stats;
for (let a of active) {
session.active[a.gid] = session.all[a.gid] = a;
}
for (let w of waiting) {
session.waiting[w.gid] = session.all[w.gid] = w;
}
for (let s of stopped) {
session.stopped[s.gid] = session.all[s.gid] = s;
}
keeplive = setInterval(async () => {
let { result: [
[stats], [active]
] } = await aria2.call([
{ method: 'aria2.getGlobalStat' },
{ method: 'aria2.tellActive'}
]);
jsonrpc.stats = stats;
active.forEach((a) => session.active[a.gid] = session.all[a.gid] = a);
}, 10000);
};
aria2.onclose = () => clearInterval(keeplive);
aria2.onmessage = async ({ method, params }) => {
if (method === 'aria2.onBtDownloadComplete') {
return;
}
let [{ gid }] = params;
let { result } = await aria2.call({ method: 'aria2.tellStatus', params: [gid] });
(dispatch[method] ?? dispatch.default)(gid, result);
};
aria2.connect();