youtube icon indicating copy to clipboard operation
youtube copied to clipboard

background.js: contextMenus.onClicked listener re-registered on every updateContextMenu call (leak, duplicate tabs)

Open chirag127 opened this issue 5 months ago • 1 comments

Problem

updateContextMenu() in background.js:147 registers a fresh chrome.contextMenus.onClicked listener inside its getLocale callback (line 167), without removing prior ones. It is invoked from three sites (lines 180, 188, 396) plus every MV3 service-worker wake, so listeners accumulate.

Steps

  1. Load unpacked (Chrome MV3).
  2. In extension settings, change language twice via the menu (fires storage.onChanged -> updateContextMenu at line 188).
  3. Right-click the toolbar icon, click "donate".

Expected

One tab opens to https://www.improvedtube.com/donate.

Actual

Three tabs open (initial listener from line 396 + two from language changes). Grows unbounded across the session; contextMenus.removeAll() on line 155 clears menu items but not the onClicked listener chain.

Fix

Register chrome.contextMenus.onClicked.addListener(...) once at top level, outside updateContextMenu.

Environment

manifest.json manifest_version: 3, version: "4", background.service_worker: background.js. Repro: Chrome 141 stable, Windows 11.

File: background.js:167 (listener add), background.js:180,188,396 (call sites).

Thanks for maintaining code-charity/youtube!

chirag127 avatar Jul 04 '26 06:07 chirag127

hi I would like to work on this issue

harshjainnn avatar Jul 04 '26 07:07 harshjainnn