From e5e46b8b4020cb834e981c7cbe59822fca9d8606 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Sun, 21 Jan 2024 23:54:26 +0800 Subject: [PATCH] Skip API polyfill in Chrome MV3 (#43) --- index.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/index.ts b/index.ts index 74f276c..9d18bbe 100644 --- a/index.ts +++ b/index.ts @@ -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'; @@ -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. @@ -180,13 +184,13 @@ export default function addDomainPermissionToggle(options?: Options): void { ].filter((permission: string) => permission === '' || 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'];