WebUI: Allow to display only hostname in the Tracker column

It is now possible to display only hostname in the Tracker column.
Closes #11357.
PR #21243.
This commit is contained in:
skomerko
2024-09-01 10:34:49 +02:00
committed by GitHub
parent fc82abe7f6
commit 9d0fa213be
4 changed files with 68 additions and 47 deletions

View File

@@ -32,6 +32,8 @@ window.qBittorrent ??= {};
window.qBittorrent.Misc ??= (() => {
const exports = () => {
return {
genHash: genHash,
getHost: getHost,
createDebounceHandler: createDebounceHandler,
friendlyUnit: friendlyUnit,
friendlyDuration: friendlyDuration,
@@ -51,6 +53,40 @@ window.qBittorrent.Misc ??= (() => {
};
};
const genHash = function(string) {
// origins:
// https://stackoverflow.com/a/8831937
// https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0
let hash = 0;
for (let i = 0; i < string.length; ++i)
hash = ((Math.imul(hash, 31) + string.charCodeAt(i)) | 0);
return hash;
};
// getHost emulate the GUI version `QString getHost(const QString &url)`
const getHost = function(url) {
// We want the hostname.
// If failed to parse the domain, original input should be returned
if (!/^(?:https?|udp):/i.test(url))
return url;
try {
// hack: URL can not get hostname from udp protocol
const parsedUrl = new URL(url.replace(/^udp:/i, "https:"));
// host: "example.com:8443"
// hostname: "example.com"
const host = parsedUrl.hostname;
if (!host)
return url;
return host;
}
catch (error) {
return url;
}
};
const createDebounceHandler = (delay, func) => {
let timer = -1;
return (...params) => {