webext-detect-page
Detects where the current browser extension code is being run. Chrome and Firefox.

Install
You can just download the standalone bundle (it might take a minute to download) and include the file 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()) {
browser.runtime.onMessage.addListener(console.log);
} else if (isContentScript) {
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