Make contextMenus usage Chromium compatible.

This commit is contained in:
Bauke 2022-03-20 19:24:45 +01:00
parent 8d99e8200b
commit ded38c7bf7
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
1 changed files with 36 additions and 28 deletions

View File

@ -3,17 +3,13 @@ import browser from 'webextension-polyfill';
import {Settings} from '../settings/settings.js';
import {updateBadge} from '../utilities/badge.js';
export async function initializeContextMenus(): Promise<void> {
const contextMenus: browser.Menus.CreateCreatePropertiesType[] = [
{
id: 'queue-add-new-link',
title: 'Add to Queue',
contexts: ['link'],
},
{
id: 'queue-add-new-link-tab',
title: 'Add to Queue',
contexts: ['tab'],
},
{
id: 'queue-open-next-link-in-new-tab',
title: 'Open next link in new tab',
@ -26,27 +22,39 @@ const contextMenus: browser.Menus.CreateCreatePropertiesType[] = [
},
];
if (import.meta.env.VITE_BROWSER === 'firefox') {
contextMenus.push({
id: 'queue-add-new-link-tab',
title: 'Add to Queue',
contexts: ['tab'],
});
}
const contextMenuIds = new Set<string>(
contextMenus.map(({id}) => id ?? 'queue-unknown'),
);
export function initializeContextMenus(): void {
await browser.contextMenus.removeAll();
for (const contextMenu of contextMenus) {
browser.contextMenus.create(contextMenu, contextCreated);
}
browser.contextMenus.onClicked.addListener(contextClicked);
browser.contextMenus.onClicked.addListener(async (info, tab) => {
await contextClicked(contextMenuIds, info, tab);
});
}
function contextCreated(): void {
const error = browser.runtime.lastError;
if (error !== null && error !== undefined) {
console.error('Queue', error);
console.error('Queue', error.message);
}
}
async function contextClicked(
contextMenuIds: Set<string>,
info: browser.Menus.OnClickData,
tab?: browser.Tabs.Tab,
): Promise<void> {