2022-11-15 11:46:10 +00:00
|
|
|
import browser from 'webextension-polyfill';
|
|
|
|
|
2022-11-21 12:23:27 +00:00
|
|
|
import {toggleAllRedirects} from '../utilities/toggle-all-redirects.js';
|
2022-11-15 11:46:10 +00:00
|
|
|
|
|
|
|
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<void> {
|
|
|
|
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<string>,
|
|
|
|
info: browser.Menus.OnClickData,
|
|
|
|
tab?: browser.Tabs.Tab,
|
|
|
|
): Promise<void> {
|
|
|
|
const id = info.menuItemId.toString();
|
|
|
|
if (!contextMenuIds.has(id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id === 're-nav-toggle-redirects') {
|
2022-11-21 12:23:27 +00:00
|
|
|
await toggleAllRedirects();
|
2022-11-15 11:46:10 +00:00
|
|
|
}
|
|
|
|
}
|