Skip to content

Commit

Permalink
Merge branch 'main' into popup
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Nov 8, 2024
2 parents cda9df1 + 61a13a7 commit 9574777
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 31 deletions.
39 changes: 11 additions & 28 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -86,27 +87,13 @@ 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 ?? 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 => {
// 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),
);

export const isActionPopup = once((): boolean => {
// Chrome-only; Firefox uses the whole window…
Expand All @@ -118,15 +105,11 @@ export const isActionPopup = once((): boolean => {
});

/** 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 */
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"xo": {
"rules": {
"n/no-unsupported-features/node-builtins": "off",
"unicorn/prevent-abbreviations": [
"error",
{
Expand All @@ -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"
Expand Down

0 comments on commit 9574777

Please sign in to comment.