alias-extension
v1.0.0
Published
Execute functions remotely in different contexts of your extension
Downloads
7
Maintainers
Readme
Alias-Extension
Execute functions remotely in different contexts of your extension.
Install
npm install --save alias-extension
Get started
An example of the execution of functions from content in the context of background.
content.js:
import { createAliases } from 'alias-extension';
const aliases = {
FOO: 'foo'
}
const { foo } = createAliases(aliases);
foo();
background.js:
import { executeAliases } from 'alias-extension';
const aliases = {
FOO: () => {
console.log('bar');
return 'foo';
}
}
executeAliases(aliases);
Calling the foo function from the content script will cause FOO
alias to be executed in the background script. The call will return the value returned from FOO
alias.
API
createAliases(aliases[, options])
aliases
an object in which the key is the name of the alias and the key is the name of the function to call the alias.options
:senderName
- sender identifier string.extensionId
- id of the extension to which alias will be sent.toActiveTab
- iftrue
then alias will be sent to the content script of the active page.tabId
- sends alias for the content script of the page with the specified id.sendOptions
- options for sendMessage.showBrowserError
- if true, when an error occurs, the console will also display an error generated by the browser.activeTabNotOptimized
- by default, the id for sending alias for the content script of the active page is calculated only once for eachcreateAliases
. Set this flag totrue
so that the id of the active page is calculated each time alias is executed.
createAliasesToTab(aliases[, options])
A wrapper for the createAliases
function which sets the toActiveTab
flag if tabId
is not passed or == false.
executeAliases(aliases[, options])
aliases
an object in which the key is the name of the alias and the value of the function that is executed when this alias is executed.options
:senderName
- string or array of strings. If this parameter is specified, aliases will be executed only if thesenderName
of the sender aliases matches thesenderName
of the executor or is contained in thesenderName
array of the executing aliases.tabsOnly
- iftrue
only aliases from content scripts are executed.excludeTabs
- iftrue
then aliases from content scripts are ignored.extensionId
- checks the extension id of the sending alias to match the one specified in the parameter.
executeTabAliases(aliases[, options])
Wrapper for the executeAliases
function with set tabsOnly: true
and excludeTabs: false
.