Skip to content

Instantly share code, notes, and snippets.

@dz0ny
Created December 13, 2010 16:13
Show Gist options
  • Save dz0ny/739158 to your computer and use it in GitHub Desktop.
Save dz0ny/739158 to your computer and use it in GitHub Desktop.
function rpcTransmission(args, method, tag, port) {
var xhr = new XMLHttpRequest();
xhr.open('POST', localStorage.server + '/rpc', true, localStorage.user, localStorage.pass);
xhr.setRequestHeader('X-Transmission-Session-Id', localStorage.sessionId);
if (method === 'torrent-add' && typeof(new XMLHttpRequest().responseType) != "undefined") {
var torrent = new XMLHttpRequest();
torrent.open('GET', JSON.parse("{" + args + "}").filename, true);
torrent.overrideMimeType('text/plain; charset=x-user-defined');
torrent.responseType = "arraybuffer";
torrent.onload = function(ev) {
var blob = new BlobBuilder();
blob.append(torrent.response);
var reader = new FileReader();
reader.onload = function(e) {
//TODO: Implement torrent preview and file selector see http://wiki.theory.org/JScript:_Converting_a_torrent_file_to_a_JScript_dictionary or http://wiki.theory.org/Decoding_encoding_bencoded_data_with_PHP
xhr.send('{ "arguments": { "metainfo": "' + reader.result.replace("data:base64,", "") + '" }, "method": "' + method + '" }');
}
reader.readAsDataURL(blob.getBlob());
};
torrent.send(null);
} else {
if (typeof tag === 'undefined') {
xhr.send('{ "arguments": { ' + args + ' }, "method": "' + method + '" }');
} else {
xhr.send('{ "arguments": { ' + args + ' }, "method": "' + method + '", "tag": ' + tag + ' }');
}
}
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.getResponseHeader('X-Transmission-Session-Id')) { // re-request with the correct session id
localStorage.sessionId = xhr.getResponseHeader('X-Transmission-Session-Id');
return rpcTransmission(args, method, tag, port);
}
var responseJSON = JSON.parse(xhr.responseText);
if (method === 'torrent-add') {
switch (responseJSON.result) {
case 'success':
showBadge('add', [0, 255, 0, 255], 5000);
break;
case 'duplicate torrent':
showBadge('dup', [0, 0, 255, 255], 5000);
break;
default:
showBadge('fail', [255, 0, 0, 255], 5000);
alert('Torrent download failed!\n\n' + responseJSON.result);
}
} else if (tag === 10) { // create a notification when a torrent finishes
var notification;
for (var i = 0, torrent; torrent = responseJSON.arguments.torrents[i]; ++i) {
if (torrent.status === 16 && torrent.leftUntilDone === 0 && completedTorrents.indexOf(torrent.id) < 0) {
notification = webkitNotifications.createNotification('images/icon48.png', 'Torrent Download Complete', torrent.name + ' has finished downloading.');
notification.show();
// hide the notification after 30 seconds
setTimeout(function() {
notification.cancel();
}, '30000');
// mark the completed torrent so another notification isn't displayed for it
completedTorrents += torrent.id + ',';
}
}
} else if (typeof port !== 'undefined') {
port.postMessage({
args: responseJSON.arguments,
tag: responseJSON.tag
});
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment