forked from RichGuk/deluge-in-chrome
-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.html
303 lines (281 loc) · 10.8 KB
/
background.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<!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>