From f43dee71ec215179ed79465ad1351a0cdade42bf Mon Sep 17 00:00:00 2001 From: Bauke Date: Mon, 21 Mar 2022 01:48:26 +0100 Subject: [PATCH] Move contextMenus.onClicked listener to initialize. --- source/background-scripts/context-menus.ts | 16 +++++++--------- source/background-scripts/initialize.ts | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/source/background-scripts/context-menus.ts b/source/background-scripts/context-menus.ts index 8398085..c49ae8c 100644 --- a/source/background-scripts/context-menus.ts +++ b/source/background-scripts/context-menus.ts @@ -3,7 +3,7 @@ import browser from 'webextension-polyfill'; import {Settings} from '../settings/settings.js'; import {updateBadge} from '../utilities/badge.js'; -export async function initializeContextMenus(): Promise { +export function getContextMenus(): browser.Menus.CreateCreatePropertiesType[] { const actionContext = import.meta.env.VITE_BROWSER === 'chromium' ? 'action' : 'browser_action'; @@ -33,19 +33,17 @@ export async function initializeContextMenus(): Promise { }); } - const contextMenuIds = new Set( - contextMenus.map(({id}) => id ?? 'queue-unknown'), - ); + return contextMenus; +} + +export async function initializeContextMenus(): Promise { + const contextMenus = getContextMenus(); await browser.contextMenus.removeAll(); for (const contextMenu of contextMenus) { browser.contextMenus.create(contextMenu, contextCreated); } - - browser.contextMenus.onClicked.addListener(async (info, tab) => { - await contextClicked(contextMenuIds, info, tab); - }); } function contextCreated(): void { @@ -56,7 +54,7 @@ function contextCreated(): void { } } -async function contextClicked( +export async function contextClicked( contextMenuIds: Set, info: browser.Menus.OnClickData, tab?: browser.Tabs.Tab, diff --git a/source/background-scripts/initialize.ts b/source/background-scripts/initialize.ts index 5bd22f6..315a0d4 100644 --- a/source/background-scripts/initialize.ts +++ b/source/background-scripts/initialize.ts @@ -4,7 +4,11 @@ import {Settings} from '../settings/settings.js'; import {updateBadge} from '../utilities/badge.js'; import {History} from '../utilities/history.js'; import {actionClicked, browserActionClicked} from './browser-action.js'; -import {initializeContextMenus} from './context-menus.js'; +import { + contextClicked, + getContextMenus, + initializeContextMenus, +} from './context-menus.js'; browser.runtime.onStartup.addListener(async () => { console.debug('Clearing history.'); @@ -26,3 +30,12 @@ browser.runtime.onInstalled.addListener(async () => { if (import.meta.env.DEV) { void browser.runtime.openOptionsPage(); } + +browser.contextMenus.onClicked.addListener(async (info, tab) => { + const contextMenus = getContextMenus(); + const contextMenuIds = new Set( + contextMenus.map(({id}) => id ?? 'queue-unknown'), + ); + + await contextClicked(contextMenuIds, info, tab); +});