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 {updateBadge} from '../utilities/badge.js';
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',
contexts: ['browser_action'],
},
{
id: 'queue-open-options-page',
title: 'Open the extension page',
contexts: ['browser_action'],
},
];
export async function initializeContextMenus(): Promise<void> {
const contextMenus: browser.Menus.CreateCreatePropertiesType[] = [
{
id: 'queue-add-new-link',
title: 'Add to Queue',
contexts: ['link'],
},
{
id: 'queue-open-next-link-in-new-tab',
title: 'Open next link in new tab',
contexts: ['browser_action'],
},
{
id: 'queue-open-options-page',
title: 'Open the extension page',
contexts: ['browser_action'],
},
];
const contextMenuIds = new Set<string>(
contextMenus.map(({id}) => id ?? 'queue-unknown'),
);
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'),
);
await browser.contextMenus.removeAll();
export function initializeContextMenus(): void {
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> {