Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract createContextMenu to webext-tools #64

Merged
merged 4 commits into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 12 additions & 28 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import chromePromised from 'webext-polyfill-kinda';
import {isBackground, isChrome} from 'webext-detect';
import {isUrlPermittedByManifest} from 'webext-permissions';
import {findMatchingPatterns} from 'webext-patterns';
import {getTabUrl} from 'webext-tools';
import {createContextMenu, getTabUrl} from 'webext-tools';
import alert from 'webext-alert';
import {executeFunction, isScriptableUrl} from 'webext-content-scripts';

Expand Down Expand Up @@ -211,19 +211,6 @@ export default function addPermissionToggle(options?: Options): void {

const manifest = chrome.runtime.getManifest();

if (!chrome.contextMenus) {
if (
!manifest.permissions?.includes('contextMenus')
// Disable setup error on Firefox Android
&& !/Android.+Firefox\//.test(navigator.userAgent)
) {
throw new Error('webext-permission-toggle requires the `contextMenus` permission');
}

console.warn('chrome.contextMenus is not available');
return;
}

globalOptions = {
title: `Enable ${manifest.name} on this domain`,
reloadOnSuccess: false,
Expand All @@ -243,24 +230,10 @@ export default function addPermissionToggle(options?: Options): void {
throw new TypeError('webext-permission-toggle requires some wildcard hosts to be specified in `optional_permissions` (MV2) or `optional_host_permissions` (MV3)');
}

// Remove any existing context menu item and silence any error
chrome.contextMenus.remove(contextMenuId, () => chrome.runtime.lastError);

const contexts: chrome.contextMenus.ContextType[] = manifest.manifest_version === 2
? ['page_action', 'browser_action']
: ['action'];

chrome.contextMenus.create({
id: contextMenuId,
type: 'checkbox',
checked: false,
title: globalOptions.title,
contexts,

// Note: This is completely ignored by Chrome and Safari. Great. #14
documentUrlPatterns: optionalHosts,
});

chrome.contextMenus.onClicked.addListener(handleClick);
chrome.tabs.onActivated.addListener(handleTabActivated);
// Chrome won't fire `onFocusChanged` if the window is clicked when a context menu is open
Expand All @@ -271,4 +244,15 @@ export default function addPermissionToggle(options?: Options): void {
void updateItem(url ?? await getTabUrl(tabId) ?? '');
}
});

void createContextMenu({
id: contextMenuId,
type: 'checkbox',
checked: false,
title: globalOptions.title,
contexts,

// Note: This is completely ignored by Chrome and Safari. Great. #14
documentUrlPatterns: optionalHosts,
});
}
Loading