Skip to content

Commit

Permalink
Add isActionPopup
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Nov 8, 2024
1 parent 4033fd3 commit 3a8d8bd
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 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 @@ -108,6 +109,21 @@ export const isSidePanel = once((): boolean => {
return url.pathname === location.pathname;
});

export const isActionPopup = once((): boolean => {
// Chrome-only; Firefox uses the whole window…
if (globalThis.outerHeight - globalThis.innerHeight === 14) {
return true;
}

const path = getManifest(3)?.action?.default_popup ?? getManifest(2)?.browser_action?.default_popup;
if (typeof path !== 'string') {
return false;
}

const url = new URL(path, location.origin);
return url.pathname === location.pathname;
});

/** 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;
Expand Down Expand Up @@ -150,6 +166,7 @@ const contextChecks = {
background: isBackground,
options: isOptionsPage,
sidePanel: isSidePanel,
actionPopup: isActionPopup,
devTools: isDevTools,
devToolsPage: isDevToolsPage,
offscreenDocument: isOffscreenDocument,
Expand Down

0 comments on commit 3a8d8bd

Please sign in to comment.