From fd559bb776bf0e991b0be9adb11244fa11fbedea Mon Sep 17 00:00:00 2001 From: skomerko <168652295+skomerko@users.noreply.github.com> Date: Tue, 4 Feb 2025 18:07:20 +0100 Subject: [PATCH] WebUI: Fix memory leak in context menus --- src/webui/www/private/scripts/contextmenu.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/webui/www/private/scripts/contextmenu.js b/src/webui/www/private/scripts/contextmenu.js index 542a8d057..7831461bf 100644 --- a/src/webui/www/private/scripts/contextmenu.js +++ b/src/webui/www/private/scripts/contextmenu.js @@ -67,9 +67,6 @@ window.qBittorrent.ContextMenu ??= (() => { // option diffs menu this.menu = $(this.options.menu); - this.targets = (this.options.targets.length > 0) - ? Array.from(document.querySelectorAll(this.options.targets)) - : []; // fx this.fx = new Fx.Tween(this.menu, { @@ -169,15 +166,18 @@ window.qBittorrent.ContextMenu ??= (() => { } addTarget(t) { + if (t.hasEventListeners) + return; + // prevent long press from selecting this text t.style.userSelect = "none"; - - this.targets[this.targets.length] = t; + t.hasEventListeners = true; this.setupEventListeners(t); } searchAndAddTargets() { - document.querySelectorAll(this.options.targets).forEach((target) => { this.addTarget(target); }); + if (this.options.targets.length > 0) + document.querySelectorAll(this.options.targets).forEach((target) => { this.addTarget(target); }); } triggerMenu(e, el) { @@ -199,8 +199,7 @@ window.qBittorrent.ContextMenu ??= (() => { // get things started startListener() { /* all elements */ - for (const el of this.targets) - this.setupEventListeners(el); + this.searchAndAddTargets(); /* menu items */ this.menu.addEventListener("click", (e) => { @@ -222,6 +221,7 @@ window.qBittorrent.ContextMenu ??= (() => { // hide on body click $(document.body).addEventListener("click", () => { this.hide(); + this.options.element = null; }); }