Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2af2d36c89
Fetching contributors…

Cannot retrieve contributors at this time

304 lines (281 sloc) 11.105 kb
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<meta charset="utf-8" />
<head>
<script src="js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
/**
* Add a torrent to Deluge using a URL. This method is meant to be called
* as part of Chrome extensions messaging system.
*
* @see chrome.extension.sendRequest && chrome.extension.onRequest
*/
function add_torrent_from_url(request, sender, sendResponse) {
var ENDPOINT = localStorage.deluge_address + '/json';
/**
* Fetches the configuration values needed to add the torrent before
* adding the torrent to Deluge.
*
* @param {String} tmp_torrent The temp path to the downloaded torrent file (used by deluge to find the torrent).
*/
function add_torrent(tmp_torrent) {
/**
* Add the torrent file into Deluge with the correct options.
*
* @param {Object} options The options for the torrent (download_path, max_connections, etc...).
*/
function add_to_deluge(options) {
$.ajax({
type: 'POST',
dataType: 'json',
url: ENDPOINT,
data: JSON.stringify({
method: 'web.add_torrents',
params: [[{'path': tmp_torrent, 'options': options}]],
id: '-52'
}),
success: function(obj) {
if(obj && obj.result) {
console.log('deluge: added torrent to deluge.');
sendResponse({msg: 'success', result: obj, error: null});
return;
}
console.log('deluge: unable to add torrent to deluge.');
sendResponse({msg: 'error', result: null, error: 'unable to add torrent to deluge'});
},
error: function(req, status, err) {
console.log('deluge: unable to add torrent to deluge.');
sendResponse({msg: 'error', result: null, error: 'unable to add torrent to deluge'});
}
});
}
// Need to get config values to add with the torrent first.
$.ajax({
type: 'POST',
dataType: 'json',
url: ENDPOINT,
data: JSON.stringify({
method: 'core.get_config_values',
params: [['add_paused', 'compact_allocation', 'download_location',
'max_connections_per_torrent', 'max_download_speed_per_torrent',
'max_upload_speed_per_torrent', 'max_upload_slots_per_torrent',
'prioritize_first_last_pieces']],
id: '-51'
}),
success: function(obj) {
if(obj && obj.result) {
console.log('deluge: got options!');
add_to_deluge(obj.result);
return;
}
console.log('deluge: unable to fetch options.');
sendResponse({msg: 'error', result: null, error: 'unable to fetch options.'});
},
error: function(req, status, err) {
console.log('deluge: unable to fetch options.');
sendResponse({msg: 'error', result: null, error: 'unable to fetch options.'});
}
});
}
// First we need to download the torrent file to a temp location in Deluge.
$.ajax({
type: 'POST',
dataType: 'json',
url: ENDPOINT,
data: JSON.stringify({
method: 'web.download_torrent_from_url',
params: [request.url, ''],
id: '-50'
}),
success: function(obj) {
if(obj && obj.result) {
console.log('deluge: downloaded torrent.');
add_torrent(obj.result);
return;
}
console.log('deluge: failed to download torrent from URL.');
sendResponse({msg: 'error', result: null, error: 'failed to download torrent from URL.'});
},
error: function(req, status, err) {
console.log('deluge: failed to download torrent from URL.');
sendResponse({msg: 'error', result: null, error: 'failed to download torrent from URL.'});
}
});
}
/**
* Checks Deluge web interface is logged in, will attempt to login if
* if not already. It also checks the web interface is connected to a
* daemon, however it will not attempt to connect to one (this must be
* done by the user in the web interface).
*
* @param args Callback methods.
* @param args.success Callback for successful connection to Deluge.
* @param args.error Callback for connection failure to Deluge.
*/
function deluge_status_check(args) {
var ENDPOINT = localStorage.deluge_address + '/json';
/**
* Attempt to login to the web interface using the password from
* localStorage.
* Upon successful login `check_connection` is called, however on failure
* the `error` callback method is called.
*
*/
function login() {
console.log('deluge: attempting to login now...');
$.ajax({
type: 'POST',
dataType: 'json',
url: ENDPOINT,
data: JSON.stringify({method: 'auth.login', params: [PASSWORD], id: '-2'}),
success: function(obj) {
if(obj && obj.result) {
console.log('deluge: now logged in!');
check_connection();
return;
}
console.log('deluge: failed to login :(');
if(args.error) {
args.error(obj);
}
},
error: function(obj, status, error) {
console.log('deluge: unable to perform ajax request.');
if(args.error) {
args.error(obj);
}
}
});
}
/**
* Checks the web interface is connected to a running daemon.
* Upon successful connection the `success` callback method is called,
* otherwise the `error` callback is called.
*/
function check_connection() {
console.log('deluge: checking daemon connection status.');
$.ajax({
type: 'POST',
dataType: 'json',
url: ENDPOINT,
data: JSON.stringify({method: 'web.connected', params:[], id: '-3'}),
success: function(obj) {
if(obj && obj.result) {
console.log('deluge: connected!');
args.success(obj);
return;
}
console.log('deluge: could not find connection to daemon.');
args.error(obj);
},
error: function(obj, status, error) {
if(args.error) {
args.error(obj);
}
}
});
}
/*
* Perform the initial session checking. This is the first ajax call in
* a chain of callback checks.
* If successful the `check_connection` method is called (we can skip
* login attempt). If the check returns false then `login` is called.
* If an error occurs the `error` callback is called.
*/
$.ajax({
type: 'POST',
dataType: 'json',
url: ENDPOINT,
data: JSON.stringify({method: 'auth.check_session', params: [], id: '-1'}),
success: function(obj) {
if(obj && obj.result) {
console.log('deluge: already logged in!');
check_connection();
return;
}
console.log('deluge: currently not logged in.');
login();
},
error: function(obj, status, error) {
console.log('deluge: unable to perform ajax request, settings correct?');
if(args.error) {
args.error(obj);
}
}
});
}
/**
* Find's the current active status page (popup).
* @returns DomWindow on success or NULL on failure.
*/
function getStatusPage() {
var views = chrome.extension.getViews();
for(var i = 0; i < views.length; i++) {
if(views[i].location.pathname == '/torrent_status.html') {
return views[i];
}
}
return null;
}
/**
* Enables the active icon/tooltip message and shows the correct content
* on the popup page.
*/
function enable_icon() {
chrome.browserAction.setIcon({path: 'images/icons/deluge_active.png'});
chrome.browserAction.setTitle({title: 'Deluge - Torrent information in Chrome!'});
// Enable torrent status view in the popup.
var view = getStatusPage();
if(view !== null) {
$('#torrent-status', view.document).show();
$('#disconnected', view.document).hide();
}
}
/**
* Disables the active icon/tooltip message and shows the correct content
* on the popup page.
*/
function disable_icon() {
chrome.browserAction.setIcon({path: 'images/icons/deluge.png'});
chrome.browserAction.setTitle({title: 'Could not connect to deluge!'});
// Disable torrent status view in the popup.
var view = getStatusPage();
if(view !== null) {
$('#torrent-status', view.document).hide();
$('#disconnected', view.document).show();
}
}
/*
* =====================================================================
* Event bindings.
* =====================================================================
*/
// Any requests send via chrome ext messaging system.
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if(request.msg == 'add_torrent_from_url') {
add_torrent_from_url(request, sender, sendResponse);
return;
} else if(request.msg == 'enable_download_icon') {
sendResponse(localStorage.deluge_download_icon);
}
// We need to send a reponse, even if it's empty.
sendResponse({msg: 'error', result: null, error: 'nothing called!'});
});
// Stuff that should be on document load.
var INTERVAL_CHECK = 0;
$(document).ready(function() {
if(!INTERVAL_CHECK) {
// Perform the connection check every minute.
INTERVAL_CHECK = setInterval('deluge_status_check({success: enable_icon, error: disable_icon})', 60000);
}
deluge_status_check({
success: enable_icon,
error: disable_icon
});
});
</script>
</head>
<body>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.