Move contextMenus.onClicked listener to initialize.

This commit is contained in:
Bauke 2022-03-21 01:48:26 +01:00
parent 6fc61d476c
commit f43dee71ec
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 21 additions and 10 deletions

View File

@ -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<void> {
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<void> {
});
}
const contextMenuIds = new Set<string>(
contextMenus.map(({id}) => id ?? 'queue-unknown'),
);
return contextMenus;
}
export async function initializeContextMenus(): Promise<void> {
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<string>,
info: browser.Menus.OnClickData,
tab?: browser.Tabs.Tab,

View File

@ -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<string>(
contextMenus.map(({id}) => id ?? 'queue-unknown'),
);
await contextClicked(contextMenuIds, info, tab);
});