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.
This commit is contained in:
bolshoytoster 2025-04-21 10:23:09 +01:00 committed by GitHub
parent 0187f19f60
commit 0160aa28b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 30 additions and 0 deletions

View file

@ -750,6 +750,8 @@ window.addEventListener("DOMContentLoaded", () => {
let syncMainDataTimeoutID = -1; let syncMainDataTimeoutID = -1;
let syncRequestInProgress = false; let syncRequestInProgress = false;
const syncMainData = () => { const syncMainData = () => {
if (document.hidden)
return;
syncRequestInProgress = true; syncRequestInProgress = true;
const url = new URL("api/v2/sync/maindata", window.location); const url = new URL("api/v2/sync/maindata", window.location);
url.search = new URLSearchParams({ 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 () => { window.addEventListener("load", async () => {

View file

@ -305,6 +305,8 @@ window.qBittorrent.PropFiles ??= (() => {
let loadTorrentFilesDataTimer = -1; let loadTorrentFilesDataTimer = -1;
const loadTorrentFilesData = () => { const loadTorrentFilesData = () => {
if (document.hidden)
return;
if ($("propFiles").classList.contains("invisible") if ($("propFiles").classList.contains("invisible")
|| $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) { || $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) {
// Tab changed, don't do anything // Tab changed, don't do anything

View file

@ -76,6 +76,8 @@ window.qBittorrent.PropGeneral ??= (() => {
let loadTorrentDataTimer = -1; let loadTorrentDataTimer = -1;
const loadTorrentData = () => { const loadTorrentData = () => {
if (document.hidden)
return;
if ($("propGeneral").classList.contains("invisible") if ($("propGeneral").classList.contains("invisible")
|| $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) { || $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) {
// Tab changed, don't do anything // Tab changed, don't do anything

View file

@ -43,6 +43,8 @@ window.qBittorrent.PropPeers ??= (() => {
let show_flags = true; let show_flags = true;
const loadTorrentPeersData = () => { const loadTorrentPeersData = () => {
if (document.hidden)
return;
if ($("propPeers").classList.contains("invisible") if ($("propPeers").classList.contains("invisible")
|| $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) { || $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) {
syncTorrentPeersLastResponseId = 0; syncTorrentPeersLastResponseId = 0;

View file

@ -43,6 +43,8 @@ window.qBittorrent.PropTrackers ??= (() => {
let loadTrackersDataTimer = -1; let loadTrackersDataTimer = -1;
const loadTrackersData = () => { const loadTrackersData = () => {
if (document.hidden)
return;
if ($("propTrackers").classList.contains("invisible") if ($("propTrackers").classList.contains("invisible")
|| $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) { || $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) {
// Tab changed, don't do anything // Tab changed, don't do anything

View file

@ -43,6 +43,8 @@ window.qBittorrent.PropWebseeds ??= (() => {
let loadWebSeedsDataTimer = -1; let loadWebSeedsDataTimer = -1;
const loadWebSeedsData = () => { const loadWebSeedsData = () => {
if (document.hidden)
return;
if ($("propWebSeeds").classList.contains("invisible") if ($("propWebSeeds").classList.contains("invisible")
|| $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) { || $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) {
// Tab changed, don't do anything // Tab changed, don't do anything

View file

@ -336,6 +336,9 @@
}; };
const syncLogData = (curTab) => { const syncLogData = (curTab) => {
if (document.hidden)
return;
if (curTab === undefined) if (curTab === undefined)
curTab = currentSelectedTab; curTab = currentSelectedTab;