1.0.0 • Published 4 years ago

alias-extension v1.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

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 - if true 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.
    • bashowBrowserError - 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 each createAliases. Set this flag to true 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 the senderName of the sender aliases matches the senderName of the executor or is contained in the senderName array of the executing aliases. tabsOnly - if true only aliases from content scripts are executed. excludeTabs - if true 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.

License

MIT