Skip to content

Commit

Permalink
Skip API polyfill in Chrome MV3 (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Jan 21, 2024
1 parent d8d5e2f commit e5e46b8
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chromeP from 'webext-polyfill-kinda';
import {isBackground} from 'webext-detect-page';
import chromePromised from 'webext-polyfill-kinda';
import {isBackground, isChrome} from 'webext-detect-page';
import {isUrlPermittedByManifest} from 'webext-permissions';
import {getTabUrl} from 'webext-tools';
import alert from 'webext-alert';
Expand All @@ -8,6 +8,10 @@ import {executeFunction} from 'webext-content-scripts';
const contextMenuId = 'webext-domain-permission-toggle:add-permission';
let globalOptions: Options;

const chromeP = isChrome() && globalThis.chrome?.runtime?.getManifest().manifest_version < 3
? chromePromised
: chrome;

type Options = {
/**
* The title of the action in the context menu.
Expand Down Expand Up @@ -180,13 +184,13 @@ export default function addDomainPermissionToggle(options?: Options): void {
].filter((permission: string) => permission === '<all_urls>' || permission.includes('*'));

if (optionalHosts.length === 0) {
throw new TypeError('webext-domain-permission-toggle requires some wildcard hosts to be specified in `optional_permissions` or `optional_host_permissions` (MV3)');
throw new TypeError('webext-domain-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: chromeP.contextMenus.ContextType[] = manifest.manifest_version === 2
const contexts: chrome.contextMenus.ContextType[] = manifest.manifest_version === 2
? ['page_action', 'browser_action']
: ['action'];

Expand Down

0 comments on commit e5e46b8

Please sign in to comment.