<commit_before>
// global variables
var completedTorrents = '',		// string of completed torrents to prevent duplicate notifications
	notificationTimer,
	torrentInfo = {};			// timer for displaying notifications

/*=================================================================================
 showBadge(string text, RGBA color, milliseconds duration)

 displays a text badge on the browser icon

 parameters
	   text: (required) text to display
	  color: (required) color of badge
   duration: (required) how long to show badge for

 returns
	nothing
=================================================================================*/
function showBadge(text, color, duration) {

	duration = (duration == undefined) ? localStorage.browserbadgetimeout : duration;

	chrome.browserAction.setBadgeBackgroundColor({ color: color });
	chrome.browserAction.setBadgeText({ text: text });

	setTimeout(function () { chrome.browserAction.setBadgeText({ 'text': '' }); }, duration);
}

/*=================================================================================
 showNotification(string title, string message)

 displays a text badge on the browser icon

 parameters
	message: (required) title of notification
	   text: (required) text to display

 returns
	nothing
=================================================================================*/
function showNotification(title, message) {

	var options = {
		type: "basic",
		title: title,
		message: message,
		iconUrl: "images/icon128.png"
	};

	var creationCallback = function() { /* Error checking goes here */ };

	if (localStorage.notificationsnewtorrent == "true") {
		chrome.notifications.create("tadd"+Math.random(), options, creationCallback);
	}
}

/*=================================================================================
 rpcTransmission(object args, string method, int tag, function callback)

 send a request to a remote Transmission client

 parameters
		args: (required) data to pass to the Transmission client
	  method: (required) tells the Transmission client how to handle the data
		 tag: makes it easy to know what to do with the response
	callback: function to reference with the response

 returns
		nothing
=================================================================================*/
function rpcTransmission(args, method, tag, callback) {
	$.ajax(
		{
			url: localStorage.server + localStorage.rpcPath,
			type: 'POST',
			username: localStorage.user,
			password: <PASSWORD>.pass,
			headers: {'X-Transmission-Session-Id': localStorage.sessionId},
			data: '{ "arguments": {' + args + '}, "method": "' + method + '"' + (tag ? ', "tag": ' + tag : '') + '}'
		}
	).complete(
		function(jqXHR, textStatus) {
			var xSid = jqXHR.getResponseHeader('X-Transmission-Session-Id');
			if(xSid) {
				localStorage.sessionId = xSid;
				return rpcTransmission(args, method, tag, callback);
			}
			if (jqXHR.responseText == ""){		//If the server is unreachable, get null request
				callback(JSON.parse(
					'{"arguments":{"torrents":[{"addedDate":0,"doneDate":0,"downloadDir":"","eta":-1,"id":1,"leftUntilDone":0,"metadataPercentComplete":1,"name":"Unable to connect to '+localStorage.server+'.","rateDownload":0,"rateUpload":0,"recheckProgress":0,"sizeWhenDone":0,"status":0,"uploadedEver":0}]},"result":"fail","tag":1}'
				));
				return;
			}
			if (callback) {
				callback(JSON.parse(jqXHR.responseText));
			}
		}
	);
}

/*=================================================================================
 getTorrent(URL url)

 attempt to download url as a torrent file

 parameters
	url: (required) url to download
<commit_msg>Correct handling of jqXHR error response<commit_after>
// global variables
var completedTorrents = '',		// string of completed torrents to prevent duplicate notifications
	notificationTimer,
	torrentInfo = {};			// timer for displaying notifications

/*=================================================================================
 showBadge(string text, RGBA color, milliseconds duration)

 displays a text badge on the browser icon

 parameters
	   text: (required) text to display
	  color: (required) color of badge
   duration: (required) how long to show badge for

 returns
	nothing
=================================================================================*/
function showBadge(text, color, duration) {

	duration = (duration == undefined) ? localStorage.browserbadgetimeout : duration;

	chrome.browserAction.setBadgeBackgroundColor({ color: color });
	chrome.browserAction.setBadgeText({ text: text });

	setTimeout(function () { chrome.browserAction.setBadgeText({ 'text': '' }); }, duration);
}

/*=================================================================================
 showNotification(string title, string message)

 displays a text badge on the browser icon

 parameters
	message: (required) title of notification
	   text: (required) text to display

 returns
	nothing
=================================================================================*/
function showNotification(title, message) {

	var options = {
		type: "basic",
		title: title,
		message: message,
		iconUrl: "images/icon128.png"
	};

	var creationCallback = function() { /* Error checking goes here */ };

	if (localStorage.notificationsnewtorrent == "true") {
		chrome.notifications.create("tadd"+Math.random(), options, creationCallback);
	}
}

/*=================================================================================
 rpcTransmission(object args, string method, int tag, function callback)

 send a request to a remote Transmission client

 parameters
		args: (required) data to pass to the Transmission client
	  method: (required) tells the Transmission client how to handle the data
		 tag: makes it easy to know what to do with the response
	callback: function to reference with the response

 returns
		nothing
=================================================================================*/
function rpcTransmission(args, method, tag, callback) {
	$.ajax(
		{
			url: localStorage.server + localStorage.rpcPath,
			type: 'POST',
			username: localStorage.user,
			password: <PASSWORD>,
			headers: {'X-Transmission-Session-Id': localStorage.sessionId},
			data: '{ "arguments": {' + args + '}, "method": "' + method + '"' + (tag ? ', "tag": ' + tag : '') + '}'
		}
	).complete(
		function(jqXHR, textStatus) {
			var xSid = jqXHR.getResponseHeader('X-Transmission-Session-Id');
			if(xSid) {
				localStorage.sessionId = xSid;
				return rpcTransmission(args, method, tag, callback);
			}
			if (textStatus == "error"){		//If the server is unreachable, get null request
				callback(JSON.parse(
					'{"arguments":{"torrents":[{"addedDate":0,"doneDate":0,"downloadDir":"","eta":0,"id":0,"leftUntilDone":0,"metadataPercentComplete":0,"name":"Unable to connect to '+localStorage.server+'.","rateDownload":0,"rateUpload":0,"recheckProgress":0,"sizeWhenDone":0,"status":0,"uploadedEver":0}]},"result":"Unable to connect to server.","tag":1}'
				));
				return;
			}
			if (callback) {
				callback(JSON.parse(jqXHR.responseText));
			}
		}
	);
}

/*=================================================================================
 getTorrent(URL url)

 attempt to download url as a torrent file

 parameters
	url: (required) url to download

{'fraction_non_alphanumeric': 0.26821917808219176, 'fraction_numerical': 0.004383561643835616, 'mean_word_length': 5.5304114490161, 'pattern_counts': {'":': 42, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4191861', 'n_tokens_mistral': 2089, 'n_tokens_neox': 1816, 'n_words': 526}