npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webext-detect-page

v5.0.1

Published

Detects where the current browser extension code is being run. Compatible with Firefox, Chrome and derivates.

Downloads

14,406

Readme

webext-detect-page

Detects where the current browser extension code is being run. Chrome and Firefox.

Install

You can download the standalone bundle and include it in your manifest.json.

Or use npm:

npm install webext-detect-page
// This module is only offered as a ES Module
import {
	isBackgroundPage,
	isContentScript,
	isOptionsPage,
} from 'webext-detect-page';

Usage

import {isBackgroundPage} from 'webext-detect-page';

if (isBackgroundPage()) {
	// Run background code, e.g.
	browser.runtime.onMessage.addListener(console.log);
} else if (isContentScript()) {
	// Run content script code, e.g.
	browser.runtime.sendMessage('wow!');
}

API

The functions are only ever evaluated once. This protects from future "invalidated context" errors. Read the note about testing if you're running this code in a tester.

isWebPage()

Returns a boolean that indicates whether the code is being run on http(s):// pages (it could be in a content script or regular web context).

isExtensionContext()

Returns a boolean that indicates whether the code is being run in extension contexts that have access to the chrome API.

isBackground()

Returns a boolean that indicates whether the code is being run in a background page or background worker.

isBackgroundPage()

Returns a boolean that indicates whether the code is being run in a background page (manifest v2).

isBackgroundWorker()

Returns a boolean that indicates whether the code is being run in a background worker (manifest v3).

isContentScript()

Returns a boolean that indicates whether the code is being run in a content script.

isOptionsPage()

Returns a boolean that 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.

isDevToolsPage()

Returns a boolean that 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.

isChrome()

isFirefox()

isSafari()

isMobileSafari()

Returns a boolean if it matches the current browser. They are loose detections based on the user agent that are useful when developing Web Extensions.

getContextName()

Returns the first matching context among those defined in index.ts, depending on the current context:

  • 'contentScript'
  • 'background'
  • 'options'
  • 'devToolsPage'
  • 'extension'
  • 'web'
  • 'unknown'

Testing

The calls are automatically cached so, if you're using this in a test environment, import and call this function first to ensure that the environment is "detected" every time:

import {disableWebextDetectPageCache} from 'webext-detect-page';
disableWebextDetectPageCache();

Related

License

MIT © Federico Brigante