WebUI: migrate to fetch API

This is the final part of it.

PR #22072.
This commit is contained in:
Chocobo1
2024-12-29 15:44:28 +08:00
committed by GitHub
parent e740a42366
commit 9c0475ebfa
15 changed files with 626 additions and 551 deletions

View File

@@ -749,7 +749,11 @@ window.addEventListener("DOMContentLoaded", () => {
let syncRequestInProgress = false;
const syncMainData = () => {
syncRequestInProgress = true;
fetch(new URI("api/v2/sync/maindata").setData("rid", syncMainDataLastResponseId), {
const url = new URL("api/v2/sync/maindata", window.location);
url.search = new URLSearchParams({
rid: syncMainDataLastResponseId
});
fetch(url, {
method: "GET",
cache: "no-store"
})

View File

@@ -360,13 +360,12 @@ const initializeWindows = () => {
toggleSequentialDownloadFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/toggleSequentialDownload",
method: "post",
data: {
fetch("api/v2/torrents/toggleSequentialDownload", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
}
}).send();
})
});
updateMainData();
}
};
@@ -374,13 +373,12 @@ const initializeWindows = () => {
toggleFirstLastPiecePrioFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/toggleFirstLastPiecePrio",
method: "post",
data: {
fetch("api/v2/torrents/toggleFirstLastPiecePrio", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
}
}).send();
})
});
updateMainData();
}
};
@@ -388,14 +386,13 @@ const initializeWindows = () => {
setSuperSeedingFN = (val) => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/setSuperSeeding",
method: "post",
data: {
value: val,
hashes: hashes.join("|")
}
}).send();
fetch("api/v2/torrents/setSuperSeeding", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
value: val
})
});
updateMainData();
}
};
@@ -403,14 +400,13 @@ const initializeWindows = () => {
setForceStartFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/setForceStart",
method: "post",
data: {
value: "true",
hashes: hashes.join("|")
}
}).send();
fetch("api/v2/torrents/setForceStart", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
value: "true"
})
});
updateMainData();
}
};
@@ -494,22 +490,23 @@ const initializeWindows = () => {
});
}
else {
new Request({
url: "api/v2/torrents/delete",
method: "post",
data: {
hashes: hashes.join("|"),
deleteFiles: forceDeleteFiles
},
onSuccess: () => {
fetch("api/v2/torrents/delete", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
deleteFiles: forceDeleteFiles
})
})
.then((response) => {
if (!response.ok) {
alert("QBT_TR(Unable to delete torrents.)QBT_TR[CONTEXT=HttpServer]");
return;
}
torrentsTable.deselectAll();
updateMainData();
updatePropertiesPanel();
},
onFailure: () => {
alert("QBT_TR(Unable to delete torrents.)QBT_TR[CONTEXT=HttpServer]");
}
}).send();
});
}
}
};
@@ -523,13 +520,12 @@ const initializeWindows = () => {
stopFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/stop",
method: "post",
data: {
fetch("api/v2/torrents/stop", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
}
}).send();
})
});
updateMainData();
}
};
@@ -537,13 +533,12 @@ const initializeWindows = () => {
startFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/start",
method: "post",
data: {
fetch("api/v2/torrents/start", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
}
}).send();
})
});
updateMainData();
}
};
@@ -565,20 +560,21 @@ const initializeWindows = () => {
});
}
else {
new Request({
url: "api/v2/torrents/setAutoManagement",
method: "post",
data: {
hashes: hashes.join("|"),
enable: enableAutoTMM
},
onSuccess: () => {
fetch("api/v2/torrents/setAutoManagement", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
enable: enableAutoTMM
})
})
.then((response) => {
if (!response.ok) {
alert("QBT_TR(Unable to set Auto Torrent Management for the selected torrents.)QBT_TR[CONTEXT=HttpServer]");
return;
}
updateMainData();
},
onFailure: () => {
alert("QBT_TR(Unable to set Auto Torrent Management for the selected torrents.)QBT_TR[CONTEXT=HttpServer]");
}
}).send();
});
}
}
};
@@ -596,19 +592,20 @@ const initializeWindows = () => {
});
}
else {
new Request({
url: "api/v2/torrents/recheck",
method: "post",
data: {
hashes: hashes.join("|"),
},
onSuccess: () => {
fetch("api/v2/torrents/recheck", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
})
})
.then((response) => {
if (!response.ok) {
alert("QBT_TR(Unable to recheck torrents.)QBT_TR[CONTEXT=HttpServer]");
return;
}
updateMainData();
},
onFailure: () => {
alert("QBT_TR(Unable to recheck torrents.)QBT_TR[CONTEXT=HttpServer]");
}
}).send();
});
}
}
};
@@ -616,13 +613,12 @@ const initializeWindows = () => {
reannounceFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/reannounce",
method: "post",
data: {
hashes: hashes.join("|"),
}
}).send();
fetch("api/v2/torrents/reannounce", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
})
});
updateMainData();
}
};
@@ -703,40 +699,42 @@ const initializeWindows = () => {
startVisibleTorrentsFN = () => {
const hashes = torrentsTable.getFilteredTorrentsHashes(selectedStatus, selectedCategory, selectedTag, selectedTracker);
if (hashes.length > 0) {
new Request({
url: "api/v2/torrents/start",
method: "post",
data: {
hashes: hashes.join("|")
},
onSuccess: () => {
fetch("api/v2/torrents/start", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
})
})
.then((response) => {
if (!response.ok) {
alert("QBT_TR(Unable to start torrents.)QBT_TR[CONTEXT=HttpServer]");
return;
}
updateMainData();
updatePropertiesPanel();
},
onFailure: () => {
alert("QBT_TR(Unable to start torrents.)QBT_TR[CONTEXT=HttpServer]");
}
}).send();
});
}
};
stopVisibleTorrentsFN = () => {
const hashes = torrentsTable.getFilteredTorrentsHashes(selectedStatus, selectedCategory, selectedTag, selectedTracker);
if (hashes.length > 0) {
new Request({
url: "api/v2/torrents/stop",
method: "post",
data: {
hashes: hashes.join("|")
},
onSuccess: () => {
fetch("api/v2/torrents/stop", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
})
})
.then((response) => {
if (!response.ok) {
alert("QBT_TR(Unable to stop torrents.)QBT_TR[CONTEXT=HttpServer]");
return;
}
updateMainData();
updatePropertiesPanel();
},
onFailure: () => {
alert("QBT_TR(Unable to stop torrents.)QBT_TR[CONTEXT=HttpServer]");
}
}).send();
});
}
};
@@ -760,22 +758,23 @@ const initializeWindows = () => {
});
}
else {
new Request({
url: "api/v2/torrents/delete",
method: "post",
data: {
hashes: hashes.join("|"),
deleteFiles: false,
},
onSuccess: () => {
fetch("api/v2/torrents/delete", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
deleteFiles: false,
})
})
.then((response) => {
if (!response.ok) {
alert("QBT_TR(Unable to delete torrents.)QBT_TR[CONTEXT=HttpServer]");
return;
}
torrentsTable.deselectAll();
updateMainData();
updatePropertiesPanel();
},
onFailure: () => {
alert("QBT_TR(Unable to delete torrents.)QBT_TR[CONTEXT=HttpServer]");
}
}).send();
});
}
}
};
@@ -809,17 +808,19 @@ const initializeWindows = () => {
const categoryName = category_list.has(categoryHash)
? category_list.get(categoryHash).name
: "";
new Request({
url: "api/v2/torrents/setCategory",
method: "post",
data: {
hashes: hashes.join("|"),
category: categoryName
},
onSuccess: () => {
fetch("api/v2/torrents/setCategory", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
category: categoryName
})
})
.then((response) => {
if (!response.ok)
return;
updateMainData();
}
}).send();
});
};
createCategoryFN = () => {
@@ -879,18 +880,19 @@ const initializeWindows = () => {
};
removeCategoryFN = (categoryHash) => {
const categoryName = category_list.get(categoryHash).name;
new Request({
url: "api/v2/torrents/removeCategories",
method: "post",
data: {
categories: categoryName
},
onSuccess: () => {
fetch("api/v2/torrents/removeCategories", {
method: "POST",
body: new URLSearchParams({
categories: category_list.get(categoryHash).name
})
})
.then((response) => {
if (!response.ok)
return;
setCategoryFilter(CATEGORIES_ALL);
updateMainData();
}
}).send();
});
};
deleteUnusedCategoriesFN = () => {
@@ -899,18 +901,19 @@ const initializeWindows = () => {
if (torrentsTable.getFilteredTorrentsNumber("all", hash, TAGS_ALL, TRACKERS_ALL) === 0)
categories.push(category.name);
});
fetch("api/v2/torrents/removeCategories", {
method: "POST",
body: new URLSearchParams({
categories: categories.join("\n")
})
})
.then((response) => {
if (!response.ok)
return;
new Request({
url: "api/v2/torrents/removeCategories",
method: "post",
data: {
categories: categories.join("\n")
},
onSuccess: () => {
setCategoryFilter(CATEGORIES_ALL);
updateMainData();
}
}).send();
});
};
torrentAddTagsFN = () => {
@@ -939,27 +942,24 @@ const initializeWindows = () => {
if (hashes.length <= 0)
return;
const tagName = tagList.has(tagHash) ? tagList.get(tagHash).name : "";
new Request({
url: (isSet ? "api/v2/torrents/addTags" : "api/v2/torrents/removeTags"),
method: "post",
data: {
fetch((isSet ? "api/v2/torrents/addTags" : "api/v2/torrents/removeTags"), {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
tags: tagName,
}
}).send();
tags: (tagList.get(tagHash)?.name || "")
})
});
};
torrentRemoveAllTagsFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: ("api/v2/torrents/removeTags"),
method: "post",
data: {
hashes: hashes.join("|"),
}
}).send();
fetch("api/v2/torrents/removeTags", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
})
});
}
};
@@ -983,14 +983,12 @@ const initializeWindows = () => {
};
removeTagFN = (tagHash) => {
const tagName = tagList.get(tagHash).name;
new Request({
url: "api/v2/torrents/deleteTags",
method: "post",
data: {
tags: tagName
}
}).send();
fetch("api/v2/torrents/deleteTags", {
method: "POST",
body: new URLSearchParams({
tags: tagList.get(tagHash).name
})
});
setTagFilter(TAGS_ALL);
};
@@ -1000,13 +998,12 @@ const initializeWindows = () => {
if (torrentsTable.getFilteredTorrentsNumber("all", CATEGORIES_ALL, hash, TRACKERS_ALL) === 0)
tags.push(tag.name);
});
new Request({
url: "api/v2/torrents/deleteTags",
method: "post",
data: {
fetch("api/v2/torrents/deleteTags", {
method: "POST",
body: new URLSearchParams({
tags: tags.join(",")
}
}).send();
})
});
setTagFilter(TAGS_ALL);
};
@@ -1130,13 +1127,12 @@ const initializeWindows = () => {
e.stopPropagation();
if (confirm("QBT_TR(Would you like to stop all torrents?)QBT_TR[CONTEXT=MainWindow]")) {
new Request({
url: "api/v2/torrents/stop",
method: "post",
data: {
fetch("api/v2/torrents/stop", {
method: "POST",
body: new URLSearchParams({
hashes: "all"
}
}).send();
})
});
updateMainData();
}
});
@@ -1146,13 +1142,12 @@ const initializeWindows = () => {
e.stopPropagation();
if (confirm("QBT_TR(Would you like to start all torrents?)QBT_TR[CONTEXT=MainWindow]")) {
new Request({
url: "api/v2/torrents/start",
method: "post",
data: {
fetch("api/v2/torrents/start", {
method: "POST",
body: new URLSearchParams({
hashes: "all"
}
}).send();
})
});
updateMainData();
}
});
@@ -1165,13 +1160,12 @@ const initializeWindows = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
hashes.each((hash, index) => {
new Request({
url: "api/v2/torrents/" + item,
method: "post",
data: {
fetch(`api/v2/torrents/${item}`, {
method: "POST",
body: new URLSearchParams({
hashes: hash
}
}).send();
})
});
});
updateMainData();
}
@@ -1189,13 +1183,12 @@ const initializeWindows = () => {
setQueuePositionFN = (cmd) => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/" + cmd,
method: "post",
data: {
fetch(`api/v2/torrents/${cmd}`, {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
}
}).send();
})
});
updateMainData();
}
};
@@ -1229,13 +1222,15 @@ const initializeWindows = () => {
e.preventDefault();
e.stopPropagation();
new Request({
url: "api/v2/auth/logout",
method: "post",
onSuccess: () => {
fetch("api/v2/auth/logout", {
method: "POST"
})
.then((response) => {
if (!response.ok)
return;
window.location.reload(true);
}
}).send();
});
});
addClickEvent("shutdown", (e) => {
@@ -1243,17 +1238,19 @@ const initializeWindows = () => {
e.stopPropagation();
if (confirm("QBT_TR(Are you sure you want to quit qBittorrent?)QBT_TR[CONTEXT=MainWindow]")) {
new Request({
url: "api/v2/app/shutdown",
method: "post",
onSuccess: () => {
fetch("api/v2/app/shutdown", {
method: "POST"
})
.then((response) => {
if (!response.ok)
return;
const shutdownMessage = "QBT_TR(%1 has been shutdown)QBT_TR[CONTEXT=HttpServer]".replace("%1", window.qBittorrent.Client.mainTitle());
document.write(`<!doctype html><html lang="${LANG}"><head> <meta charset="UTF-8"> <meta name="color-scheme" content="light dark"> <title>${shutdownMessage}</title> <style>* {font-family: Arial, Helvetica, sans-serif;}</style></head><body> <h1 style="text-align: center;">${shutdownMessage}</h1></body></html>`);
document.close();
window.stop();
window.qBittorrent.Client.stop();
}
}).send();
});
}
});

View File

@@ -354,7 +354,12 @@ window.qBittorrent.PropFiles ??= (() => {
current_hash = new_hash;
loadedNewTorrent = true;
}
fetch(new URI("api/v2/torrents/files").setData("hash", current_hash), {
const url = new URL("api/v2/torrents/files", window.location);
url.search = new URLSearchParams({
hash: current_hash
});
fetch(url, {
method: "GET",
cache: "no-store"
})

View File

@@ -88,7 +88,11 @@ window.qBittorrent.PropGeneral ??= (() => {
return;
}
fetch(new URI("api/v2/torrents/properties").setData("hash", current_id), {
const propertiesURL = new URL("api/v2/torrents/properties", window.location);
propertiesURL.search = new URLSearchParams({
hash: current_id
});
fetch(propertiesURL, {
method: "GET",
cache: "no-store"
})
@@ -230,7 +234,11 @@ window.qBittorrent.PropGeneral ??= (() => {
loadTorrentDataTimer = loadTorrentData.delay(5000);
});
fetch(new URI("api/v2/torrents/pieceStates").setData("hash", current_id), {
const pieceStatesURL = new URL("api/v2/torrents/pieceStates", window.location);
pieceStatesURL.search = new URLSearchParams({
hash: current_id
});
fetch(pieceStatesURL, {
method: "GET",
cache: "no-store"
})

View File

@@ -58,19 +58,23 @@ window.qBittorrent.PropTrackers ??= (() => {
torrentTrackersTable.clear();
current_hash = new_hash;
}
const url = new URI("api/v2/torrents/trackers?hash=" + current_hash);
new Request.JSON({
url: url,
method: "get",
noCache: true,
onComplete: () => {
clearTimeout(loadTrackersDataTimer);
loadTrackersDataTimer = loadTrackersData.delay(10000);
},
onSuccess: (trackers) => {
const url = new URL("api/v2/torrents/trackers", window.location);
url.search = new URLSearchParams({
hash: current_hash
});
fetch(url, {
method: "GET",
cache: "no-store"
})
.then(async (response) => {
if (!response.ok)
return;
const selectedTrackers = torrentTrackersTable.selectedRowsIds();
torrentTrackersTable.clear();
const trackers = await response.json();
if (trackers) {
trackers.each((tracker) => {
let status;
@@ -113,8 +117,11 @@ window.qBittorrent.PropTrackers ??= (() => {
if (selectedTrackers.length > 0)
torrentTrackersTable.reselectRows(selectedTrackers);
}
}
}).send();
})
.finally(() => {
clearTimeout(loadTrackersDataTimer);
loadTrackersDataTimer = loadTrackersData.delay(10000);
});
};
const updateData = () => {
@@ -214,18 +221,19 @@ window.qBittorrent.PropTrackers ??= (() => {
if (current_hash.length === 0)
return;
const selectedTrackers = torrentTrackersTable.selectedRowsIds();
new Request({
url: "api/v2/torrents/removeTrackers",
method: "post",
data: {
hash: current_hash,
urls: selectedTrackers.map(encodeURIComponent).join("|")
},
onSuccess: () => {
fetch("api/v2/torrents/removeTrackers", {
method: "POST",
body: new URLSearchParams({
hash: current_hash,
urls: torrentTrackersTable.selectedRowsIds().map(encodeURIComponent).join("|")
})
})
.then((response) => {
if (!response.ok)
return;
updateData();
}
}).send();
});
};
const clear = () => {

View File

@@ -58,18 +58,23 @@ window.qBittorrent.PropWebseeds ??= (() => {
torrentWebseedsTable.clear();
current_hash = new_hash;
}
new Request.JSON({
url: new URI("api/v2/torrents/webseeds").setData("hash", current_hash),
method: "get",
noCache: true,
onComplete: () => {
clearTimeout(loadWebSeedsDataTimer);
loadWebSeedsDataTimer = loadWebSeedsData.delay(10000);
},
onSuccess: (webseeds) => {
const url = new URL("api/v2/torrents/webseeds", window.location);
url.search = new URLSearchParams({
hash: current_hash
});
fetch(url, {
method: "GET",
cache: "no-store"
})
.then(async (response) => {
if (!response.ok)
return;
const selectedWebseeds = torrentWebseedsTable.selectedRowsIds();
torrentWebseedsTable.clear();
const webseeds = await response.json();
if (webseeds) {
// Update WebSeeds data
webseeds.each((webseed) => {
@@ -84,8 +89,11 @@ window.qBittorrent.PropWebseeds ??= (() => {
if (selectedWebseeds.length > 0)
torrentWebseedsTable.reselectRows(selectedWebseeds);
}
}).send();
})
.finally(() => {
clearTimeout(loadWebSeedsDataTimer);
loadWebSeedsDataTimer = loadWebSeedsData.delay(10000);
});
};
const updateData = () => {
@@ -190,18 +198,19 @@ window.qBittorrent.PropWebseeds ??= (() => {
if (current_hash.length === 0)
return;
const selectedWebseeds = torrentWebseedsTable.selectedRowsIds();
new Request({
url: "api/v2/torrents/removeWebSeeds",
method: "post",
data: {
hash: current_hash,
urls: selectedWebseeds.map(webseed => encodeURIComponent(webseed)).join("|")
},
onSuccess: () => {
fetch("api/v2/torrents/removeWebSeeds", {
method: "POST",
body: new URLSearchParams({
hash: current_hash,
urls: torrentWebseedsTable.selectedRowsIds().map(webseed => encodeURIComponent(webseed)).join("|")
})
})
.then((response) => {
if (!response.ok)
return;
updateData();
}
}).send();
});
};
const clear = () => {

View File

@@ -47,7 +47,7 @@ window.qBittorrent.MultiRename ??= (() => {
onChanged: (rows) => {},
onInvalidRegex: (err) => {},
onRenamed: (rows) => {},
onRenameError: (err) => {},
onRenameError: (response) => {},
_inner_update: function() {
const findMatches = (regex, str) => {
@@ -240,21 +240,19 @@ window.qBittorrent.MultiRename ??= (() => {
const newPath = parentPath
? parentPath + window.qBittorrent.Filesystem.PathSeparator + newName
: newName;
const renameRequest = new Request({
url: isFolder ? "api/v2/torrents/renameFolder" : "api/v2/torrents/renameFile",
method: "post",
data: {
hash: this.hash,
oldPath: oldPath,
newPath: newPath
}
});
try {
await renameRequest.send();
await fetch((isFolder ? "api/v2/torrents/renameFolder" : "api/v2/torrents/renameFile"), {
method: "POST",
body: new URLSearchParams({
hash: this.hash,
oldPath: oldPath,
newPath: newPath
})
});
replaced.push(match);
}
catch (err) {
this.onRenameError(err, match);
catch (response) {
this.onRenameError(response, match);
}
}.bind(this);

View File

@@ -247,13 +247,12 @@ window.qBittorrent.Search ??= (() => {
tab.destroy();
new Request({
url: new URI("api/v2/search/delete"),
method: "post",
data: {
fetch("api/v2/search/delete", {
method: "POST",
body: new URLSearchParams({
id: searchId
},
}).send();
})
});
const searchJobs = JSON.parse(LocalPreferences.get("search_jobs", "[]"));
const jobIndex = searchJobs.findIndex((job) => job.id === searchId);
@@ -395,42 +394,45 @@ window.qBittorrent.Search ??= (() => {
const startSearch = (pattern, category, plugins) => {
searchPatternChanged = false;
fetch("api/v2/search/start", {
method: "POST",
body: new URLSearchParams({
pattern: pattern,
category: category,
plugins: plugins
})
})
.then(async (response) => {
if (!response.ok)
return;
const responseJSON = await response.json();
const url = new URI("api/v2/search/start");
new Request.JSON({
url: url,
method: "post",
data: {
pattern: pattern,
category: category,
plugins: plugins
},
onSuccess: (response) => {
document.getElementById("startSearchButton").lastChild.textContent = "QBT_TR(Stop)QBT_TR[CONTEXT=SearchEngineWidget]";
const searchId = response.id;
const searchId = responseJSON.id;
createSearchTab(searchId, pattern);
const searchJobs = JSON.parse(LocalPreferences.get("search_jobs", "[]"));
searchJobs.push({ id: searchId, pattern: pattern });
LocalPreferences.set("search_jobs", JSON.stringify(searchJobs));
}
}).send();
});
};
const stopSearch = (searchId) => {
const url = new URI("api/v2/search/stop");
new Request({
url: url,
method: "post",
data: {
id: searchId
},
onSuccess: (response) => {
fetch("api/v2/search/stop", {
method: "POST",
body: new URLSearchParams({
id: searchId
})
})
.then((response) => {
if (!response.ok)
return;
resetSearchState(searchId);
// not strictly necessary to do this when the tab is being closed, but there's no harm in it
updateStatusIconElement(searchId, "QBT_TR(Search aborted)QBT_TR[CONTEXT=SearchJobWidget]", "images/task-reject.svg");
}
}).send();
});
};
const getSelectedSearchId = () => {
@@ -638,11 +640,16 @@ window.qBittorrent.Search ??= (() => {
};
const getPlugins = () => {
new Request.JSON({
url: new URI("api/v2/search/plugins"),
method: "get",
noCache: true,
onSuccess: (response) => {
fetch("api/v2/search/plugins", {
method: "GET",
cache: "no-store"
})
.then(async (response) => {
if (!response.ok)
return;
const responseJSON = await response.json();
const createOption = (text, value, disabled = false) => {
const option = document.createElement("option");
if (value !== undefined)
@@ -652,10 +659,10 @@ window.qBittorrent.Search ??= (() => {
return option;
};
if (response !== prevSearchPluginsResponse) {
prevSearchPluginsResponse = response;
if (prevSearchPluginsResponse !== responseJSON) {
prevSearchPluginsResponse = responseJSON;
searchPlugins.length = 0;
response.forEach((plugin) => {
responseJSON.forEach((plugin) => {
searchPlugins.push(plugin);
});
@@ -697,8 +704,7 @@ window.qBittorrent.Search ??= (() => {
reselectPlugin();
}
}
}).send();
});
};
const getPlugin = (name) => {
@@ -766,30 +772,30 @@ window.qBittorrent.Search ??= (() => {
const loadSearchResultsData = function(searchId) {
const state = searchState.get(searchId);
const url = new URL("api/v2/search/results", window.location);
url.search = new URLSearchParams({
id: searchId,
limit: 500,
offset: state.rowId
});
fetch(url, {
method: "GET",
cache: "no-store"
})
.then(async (response) => {
if (!response.ok) {
if ((response.status === 400) || (response.status === 404)) {
// bad params. search id is invalid
resetSearchState(searchId);
updateStatusIconElement(searchId, "QBT_TR(An error occurred during search...)QBT_TR[CONTEXT=SearchJobWidget]", "images/error.svg");
}
else {
clearTimeout(state.loadResultsTimer);
state.loadResultsTimer = loadSearchResultsData.delay(3000, this, searchId);
}
return;
}
const maxResults = 500;
const url = new URI("api/v2/search/results");
new Request.JSON({
url: url,
method: "get",
noCache: true,
data: {
id: searchId,
limit: maxResults,
offset: state.rowId
},
onFailure: function(response) {
if ((response.status === 400) || (response.status === 404)) {
// bad params. search id is invalid
resetSearchState(searchId);
updateStatusIconElement(searchId, "QBT_TR(An error occurred during search...)QBT_TR[CONTEXT=SearchJobWidget]", "images/error.svg");
}
else {
clearTimeout(state.loadResultsTimer);
state.loadResultsTimer = loadSearchResultsData.delay(3000, this, searchId);
}
},
onSuccess: function(response) {
$("error_div").textContent = "";
const state = searchState.get(searchId);
@@ -800,12 +806,13 @@ window.qBittorrent.Search ??= (() => {
return;
}
if (response) {
const responseJSON = await response.json();
if (responseJSON) {
const state = searchState.get(searchId);
const newRows = [];
if (response.results) {
const results = response.results;
if (responseJSON.results) {
const results = responseJSON.results;
for (let i = 0; i < results.length; ++i) {
const result = results[i];
const row = {
@@ -838,7 +845,7 @@ window.qBittorrent.Search ??= (() => {
searchResultsTable.updateTable();
}
if ((response.status === "Stopped") && (state.rowId >= response.total)) {
if ((responseJSON.status === "Stopped") && (state.rowId >= responseJSON.total)) {
resetSearchState(searchId);
updateStatusIconElement(searchId, "QBT_TR(Search has finished)QBT_TR[CONTEXT=SearchJobWidget]", "images/task-complete.svg");
return;
@@ -847,8 +854,7 @@ window.qBittorrent.Search ??= (() => {
clearTimeout(state.loadResultsTimer);
state.loadResultsTimer = loadSearchResultsData.delay(2000, this, searchId);
}
}).send();
});
};
const updateSearchResultsData = function(searchId) {