This is a function from a Tampermonkey userscript that I use. I am not the author of the original script, but I am trying to make some modifications to it. The script adds output to an HTML page depending on the original response. When I try to add an if()
statement to only append certain text for certain responses, the script becomes very sluggish on large pages. Is there a better way to handle searching the HTML response for specified text? I will comment the section in the code that I am hoping to improve:
function processResult(result) {
$.each(result, function (i, resp) {
var row = torrentRows[i];
var link, newWin, playerUrl;
if (row.isAdding) {
return;
}
row.actions.empty();
if (resp.status == 'downloaded') {
// Play
link = $('<a href="#">▶</a>');
link.click(function (e) {
e.preventDefault();
playerUrl = 'http://ipaddress/player/?playlist=playlist/' + resp.id;
if (navigator.userAgent.indexOf('Firefox') > -1) {
newWin = window.open(playerUrl, 'player', 'height=670,width=400,menubar=no,status=no,toolbar=no');
} else {
newWin = window.open('', 'player', 'height=670,width=400,menubar=no,status=no,toolbar=no');
newWin.opener = null;
newWin.location.href = playerUrl;
}
});
row.actions.append(' ');
row.actions.append(link);
// Download
link = $('<a href="' + downloadUrl + resp.id + '">↓</a>');
row.actions.append(' | ');
row.actions.append(link);
} else if (resp.status == 'downloading') {
row.actions.text(Math.floor(resp.progress * 100) + '%');
} else if (resp.status == 'missing') {
// Add
link = $('<a href="#">GET</a>');
link.click(function (e) {
e.preventDefault();
if (confirm('Are you sure you want to download ' + resp.id + '?')) {
row.actions.html(loadingImg);
row.isAdding = true;
addTorrent(resp.id, function (addResult) {
row.isAdding = false;
if (addResult.success) {
var htmlTitle = $('<b>').text(addResult.title).prop('outerHTML');
noty({
text: 'Torrent ' + htmlTitle + ' added successfully!',
type: 'success'
});
row.actions.text('OK');
} else if (addResult.error_code == 'already_added') {
var htmlTitle = $('<b>').text(addResult.title).prop('outerHTML');
noty({
text: 'Torrent ' + htmlTitle + ' already added!',
type: 'warning'
});
row.actions.text('ERR');
} else {
noty({
text: 'Error adding ' + resp.id + ': ' + addResult.error,
type: 'error'
});
row.actions.text('ERR');
}
});
}
});
row.actions.append(' ');
row.actions.append(link);
/* This section to analyze. Commented rows cause slow performance. */
//var $row = row.row.context;
//var str_text = $row.innerText;
//if (str_text.indexOf("FLAC") >= 0)
//{
// Transcode request
link = $('<a href="#">TC</a>');
link.click(function (e) {
e.preventDefault();
if (confirm('Are you sure you want to request transcode for ' + resp.id + '?')) {
row.actions.html(loadingImg);
row.isAdding = true;
addTranscodeRequest(resp.id, function (addResult) {
row.isAdding = false;
noty({
text: addResult.message,
type: 'information'
});
row.actions.text('OK');
});
}
});
row.actions.append(' | ');
row.actions.append(link);
//}
/* END section to analyze */
} else {
row.actions.text(result[i].status);
}
});
}
EDIT:
Or, is there a way to make this function asynchronous? I don't mind how long it takes, besides the fact that it locks up that browser window.