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