From 0160aa28b6b9f347125be7d9e8820c1dcb62b14c Mon Sep 17 00:00:00 2001 From: bolshoytoster <91278344+bolshoytoster@users.noreply.github.com> Date: Mon, 21 Apr 2025 10:23:09 +0100 Subject: [PATCH] WebUI: Don't update UI if the page is hidden Currently, there is unnecessary CPU/network usage by the web UI when it's running in the background, this PR prevents it from refreshing in the background. Closes #22565. PR #22567. --- src/webui/www/private/scripts/client.js | 17 +++++++++++++++++ src/webui/www/private/scripts/prop-files.js | 2 ++ src/webui/www/private/scripts/prop-general.js | 2 ++ src/webui/www/private/scripts/prop-peers.js | 2 ++ src/webui/www/private/scripts/prop-trackers.js | 2 ++ src/webui/www/private/scripts/prop-webseeds.js | 2 ++ src/webui/www/private/views/log.html | 3 +++ 7 files changed, 30 insertions(+) diff --git a/src/webui/www/private/scripts/client.js b/src/webui/www/private/scripts/client.js index 6a7511293..15e7af365 100644 --- a/src/webui/www/private/scripts/client.js +++ b/src/webui/www/private/scripts/client.js @@ -750,6 +750,8 @@ window.addEventListener("DOMContentLoaded", () => { let syncMainDataTimeoutID = -1; let syncRequestInProgress = false; const syncMainData = () => { + if (document.hidden) + return; syncRequestInProgress = true; const url = new URL("api/v2/sync/maindata", window.location); url.search = new URLSearchParams({ @@ -1774,6 +1776,21 @@ window.addEventListener("DOMContentLoaded", () => { } } }); + + addEventListener("visibilitychange", (event) => { + if (document.hidden) + return; + + switch (LocalPreferences.get("selected_window_tab")) { + case "log": + window.qBittorrent.Log.load(); + break; + case "transfers": + syncData(100); + updatePropertiesPanel(); + break; + } + }); }); window.addEventListener("load", async () => { diff --git a/src/webui/www/private/scripts/prop-files.js b/src/webui/www/private/scripts/prop-files.js index 482781112..1aa2665b9 100644 --- a/src/webui/www/private/scripts/prop-files.js +++ b/src/webui/www/private/scripts/prop-files.js @@ -305,6 +305,8 @@ window.qBittorrent.PropFiles ??= (() => { let loadTorrentFilesDataTimer = -1; const loadTorrentFilesData = () => { + if (document.hidden) + return; if ($("propFiles").classList.contains("invisible") || $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) { // Tab changed, don't do anything diff --git a/src/webui/www/private/scripts/prop-general.js b/src/webui/www/private/scripts/prop-general.js index dbf80e8a9..d93c9b746 100644 --- a/src/webui/www/private/scripts/prop-general.js +++ b/src/webui/www/private/scripts/prop-general.js @@ -76,6 +76,8 @@ window.qBittorrent.PropGeneral ??= (() => { let loadTorrentDataTimer = -1; const loadTorrentData = () => { + if (document.hidden) + return; if ($("propGeneral").classList.contains("invisible") || $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) { // Tab changed, don't do anything diff --git a/src/webui/www/private/scripts/prop-peers.js b/src/webui/www/private/scripts/prop-peers.js index 80120c86e..8ccb2cbd1 100644 --- a/src/webui/www/private/scripts/prop-peers.js +++ b/src/webui/www/private/scripts/prop-peers.js @@ -43,6 +43,8 @@ window.qBittorrent.PropPeers ??= (() => { let show_flags = true; const loadTorrentPeersData = () => { + if (document.hidden) + return; if ($("propPeers").classList.contains("invisible") || $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) { syncTorrentPeersLastResponseId = 0; diff --git a/src/webui/www/private/scripts/prop-trackers.js b/src/webui/www/private/scripts/prop-trackers.js index fbf5c0eef..e75709724 100644 --- a/src/webui/www/private/scripts/prop-trackers.js +++ b/src/webui/www/private/scripts/prop-trackers.js @@ -43,6 +43,8 @@ window.qBittorrent.PropTrackers ??= (() => { let loadTrackersDataTimer = -1; const loadTrackersData = () => { + if (document.hidden) + return; if ($("propTrackers").classList.contains("invisible") || $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) { // Tab changed, don't do anything diff --git a/src/webui/www/private/scripts/prop-webseeds.js b/src/webui/www/private/scripts/prop-webseeds.js index 96cfbd4ea..7fd517597 100644 --- a/src/webui/www/private/scripts/prop-webseeds.js +++ b/src/webui/www/private/scripts/prop-webseeds.js @@ -43,6 +43,8 @@ window.qBittorrent.PropWebseeds ??= (() => { let loadWebSeedsDataTimer = -1; const loadWebSeedsData = () => { + if (document.hidden) + return; if ($("propWebSeeds").classList.contains("invisible") || $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) { // Tab changed, don't do anything diff --git a/src/webui/www/private/views/log.html b/src/webui/www/private/views/log.html index 99fe8660a..bbafca8cc 100644 --- a/src/webui/www/private/views/log.html +++ b/src/webui/www/private/views/log.html @@ -336,6 +336,9 @@ }; const syncLogData = (curTab) => { + if (document.hidden) + return; + if (curTab === undefined) curTab = currentSelectedTab;