From bc51a07bb117059ef9a3762af73816496ab6cadf Mon Sep 17 00:00:00 2001 From: Federico Date: Sat, 9 Nov 2024 01:21:21 +0700 Subject: [PATCH 1/2] Reduce code --- index.ts | 39 +++++++++++---------------------------- package.json | 7 ++++--- 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/index.ts b/index.ts index 735a093..41b6007 100644 --- a/index.ts +++ b/index.ts @@ -24,6 +24,7 @@ function getManifest(_version?: 2 | 3): chrome.runtime.Manifest | undefined { return globalThis.chrome?.runtime?.getManifest?.(); } +/* @__PURE__ */ function once(function_: () => boolean): () => boolean { let result: boolean; return () => { @@ -86,38 +87,20 @@ export const isPersistentBackgroundPage = once((): boolean => ); /** Indicates whether you're in an options page. This only works if the current page’s URL matches the one specified in the extension's `manifest.json` */ -export const isOptionsPage = once((): boolean => { - const path = getManifest()?.options_ui?.page; - if (typeof path !== 'string') { - return false; - } - - const url = new URL(path, location.origin); - return url.pathname === location.pathname; -}); +export const isOptionsPage = once((): boolean => isCurrentPathname(getManifest()?.options_ui?.page)); /** Indicates whether you're in a side panel. This only works if the current page’s URL matches the one specified in the extension's `manifest.json` */ -export const isSidePanel = once((): boolean => { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- Not yet in @types/chrome - const path = getManifest(3)?.['side_panel']?.default_path; - if (typeof path !== 'string') { - return false; - } - - const url = new URL(path, location.origin); - return url.pathname === location.pathname; -}); +export const isSidePanel = once((): boolean => + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- Not yet in @types/chrome + isCurrentPathname(getManifest(3)?.['side_panel']?.default_path), +); /** Indicates whether you're in the main dev tools page, the one specified in the extension's `manifest.json` `devtools_page` field. */ -export const isMainDevToolsPage = once((): boolean => { - const devtoolsPage = isExtensionContext() && chrome.devtools && getManifest()?.devtools_page; - if (typeof devtoolsPage !== 'string') { - return false; - } - - const url = new URL(devtoolsPage, location.origin); - return url.pathname === location.pathname; -}); +export const isMainDevToolsPage = once((): boolean => + isExtensionContext() + && Boolean(chrome.devtools) + && isCurrentPathname(getManifest()?.devtools_page), +); // TODO: When dropping this, also rename the `devToolsPage` context name below /** @deprecated Use `isMainDevToolsPage` instead */ diff --git a/package.json b/package.json index abf70f2..943b363 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ }, "xo": { "rules": { + "n/no-unsupported-features/node-builtins": "off", "unicorn/prevent-abbreviations": [ "error", { @@ -52,10 +53,10 @@ "@parcel/config-webextension": "^2.12.1-canary.3358", "@parcel/resolver-default": "^2.0.0-canary.1735", "@sindresorhus/tsconfig": "^6.0.0", - "@types/chrome": "^0.0.268", + "@types/chrome": "^0.0.280", "parcel": "^2.0.0-canary.1733", - "typescript": "^5.5.2", - "xo": "^0.58.0" + "typescript": "^5.6.3", + "xo": "^0.59.3" }, "engines": { "node": ">=18" From 61a13a75b6521c0ba64a2e1b6d440e8ce56e8d49 Mon Sep 17 00:00:00 2001 From: Federico Date: Sat, 9 Nov 2024 01:21:34 +0700 Subject: [PATCH 2/2] Add support for `options_page` key --- index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.ts b/index.ts index 41b6007..442d323 100644 --- a/index.ts +++ b/index.ts @@ -87,7 +87,7 @@ export const isPersistentBackgroundPage = once((): boolean => ); /** Indicates whether you're in an options page. This only works if the current page’s URL matches the one specified in the extension's `manifest.json` */ -export const isOptionsPage = once((): boolean => isCurrentPathname(getManifest()?.options_ui?.page)); +export const isOptionsPage = once((): boolean => isCurrentPathname(getManifest()?.options_ui?.page ?? getManifest()?.options_page)); /** Indicates whether you're in a side panel. This only works if the current page’s URL matches the one specified in the extension's `manifest.json` */ export const isSidePanel = once((): boolean =>