WebUI: Fix memory leak in context menus

This commit is contained in:
skomerko 2025-02-04 18:07:20 +01:00
commit fd559bb776

View file

@ -67,9 +67,6 @@ window.qBittorrent.ContextMenu ??= (() => {
// option diffs menu // option diffs menu
this.menu = $(this.options.menu); this.menu = $(this.options.menu);
this.targets = (this.options.targets.length > 0)
? Array.from(document.querySelectorAll(this.options.targets))
: [];
// fx // fx
this.fx = new Fx.Tween(this.menu, { this.fx = new Fx.Tween(this.menu, {
@ -169,14 +166,17 @@ window.qBittorrent.ContextMenu ??= (() => {
} }
addTarget(t) { addTarget(t) {
if (t.hasEventListeners)
return;
// prevent long press from selecting this text // prevent long press from selecting this text
t.style.userSelect = "none"; t.style.userSelect = "none";
t.hasEventListeners = true;
this.targets[this.targets.length] = t;
this.setupEventListeners(t); this.setupEventListeners(t);
} }
searchAndAddTargets() { searchAndAddTargets() {
if (this.options.targets.length > 0)
document.querySelectorAll(this.options.targets).forEach((target) => { this.addTarget(target); }); document.querySelectorAll(this.options.targets).forEach((target) => { this.addTarget(target); });
} }
@ -199,8 +199,7 @@ window.qBittorrent.ContextMenu ??= (() => {
// get things started // get things started
startListener() { startListener() {
/* all elements */ /* all elements */
for (const el of this.targets) this.searchAndAddTargets();
this.setupEventListeners(el);
/* menu items */ /* menu items */
this.menu.addEventListener("click", (e) => { this.menu.addEventListener("click", (e) => {
@ -222,6 +221,7 @@ window.qBittorrent.ContextMenu ??= (() => {
// hide on body click // hide on body click
$(document.body).addEventListener("click", () => { $(document.body).addEventListener("click", () => {
this.hide(); this.hide();
this.options.element = null;
}); });
} }