import browser from 'webextension-polyfill'; import {updateBadge} from '../utilities/badge.js'; type ContextMenu = browser.Menus.CreateCreatePropertiesType; export function getContextMenus(): ContextMenu[] { const actionContext = import.meta.env.VITE_BROWSER === 'chromium' ? 'action' : 'browser_action'; const contextMenus: ContextMenu[] = [ { id: 're-nav-toggle-redirects', title: 'Toggle all redirects', contexts: [actionContext], }, ]; return contextMenus; } export async function initializeContextMenus(): Promise { const contextMenus = getContextMenus(); await browser.contextMenus.removeAll(); for (const contextMenu of contextMenus) { browser.contextMenus.create(contextMenu, contextCreated); } } function contextCreated(): void { const error = browser.runtime.lastError; if (error !== null && error !== undefined) { console.error('Re-Nav', error.message); } } export async function contextClicked( contextMenuIds: Set, info: browser.Menus.OnClickData, tab?: browser.Tabs.Tab, ): Promise { const id = info.menuItemId.toString(); if (!contextMenuIds.has(id)) { return; } if (id === 're-nav-toggle-redirects') { const state = await browser.storage.local.get({redirectsEnabled: true}); const redirectsEnabled = !(state.redirectsEnabled as boolean); await browser.storage.local.set({redirectsEnabled}); await updateBadge(redirectsEnabled); } }