Skip to content
Detects where the current browser extension code is being run. Compatibile with Firefox, Chrome and derivates.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
source
.editorconfig
.gitattributes
.gitignore
.npmrc
.travis.yml
license
package.json
readme.md
rollup.config.js
tsconfig.json
webext-detect-page.js

readme.md

webext-detect-page

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

Travis build status npm version

Install

You can just include the file webext-detect-page.js in your manifest.json, or:

npm install --save webext-detect-page
import {
	isBackgroundPage,
	isContentScript,
	isOptionsPage
} from 'webext-detect-page';
const {
	isBackgroundPage,
	isContentScript,
	isOptionsPage
} = require('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

isBackgroundPage()

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

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 URL matches the one specified in the extension's manifest.json

Related

License

MIT © Federico Brigante — Twitter

You can’t perform that action at this time.