Skip to content

Commit

Permalink
Add isDevTools (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Dec 29, 2023
1 parent 63c55a4 commit 5fab945
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,6 @@ export const isOptionsPage = once((): boolean => {
return url.pathname === location.pathname;
});

/** Indicates whether the code is being run in a dev tools page. This only works if the current page’s URL matches the one specified in the extension's `manifest.json` `devtools_page` field. */
export const isDevToolsPage = 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;
});

/** Indicates whether the code is being run 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 isSidePanel = once((): boolean => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- Not yet in @types/chrome
Expand All @@ -109,6 +98,20 @@ export const isSidePanel = once((): boolean => {
return url.pathname === location.pathname;
});

/** Indicates whether the code is being run in a dev tools page. This only works if the current page’s URL matches the one specified in the extension's `manifest.json` `devtools_page` field. */
export const isDevToolsPage = 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;
});

/** Indicates whether the code is being run in the dev tools page. Unlike `isDevToolsPage`, this works in any page that has the `chrome.devTools` API */
export const isDevTools = () => Boolean(globalThis.chrome?.devtools);

/** Loosely detect Firefox via user agent */
export const isFirefox = () => globalThis.navigator?.userAgent.includes('Firefox');

Expand All @@ -125,6 +128,8 @@ export const contextNames = {
contentScript: isContentScript,
background: isBackground,
options: isOptionsPage,
sidePanel: isSidePanel,
devTools: isDevTools,
devToolsPage: isDevToolsPage,
extension: isExtensionContext,
web: isWebPage,
Expand Down

0 comments on commit 5fab945

Please sign in to comment.