Code Monkey home page Code Monkey logo

ext-messenger's Introduction

Extension message passing made easy

Latest Stable Version NPM Downloads

What?

Small library for messaging across any extension parts (background, content script, popup or devtool).

Works both for Chrome extensions and Firefox add-ons, has a simple API, promise based callback support and more.

Why?

Sending messages between extension parts can get complicated and usually requires some relaying mechanism in the background page. Adding callback functionality to these messages can make it even trickier.

Furthermore, the messaging API is not coherent or straight forward, sometimes requiring you to use runtime.* API and sometimes tabs.* API depending on which extension part you are currently in.

How?

$ npm i ext-messenger --save

1) In the background page: create a messenger instance and init the background hub.

const Messenger = require('ext-messenger');
let messenger = new Messenger();

messenger.initBackgroundHub();

This step is obligatory and should be done as soon as possible in your background page.

* You can also add the library via script tag and use window['ext-messenger'].

2) Init connections (in any extension parts).

const Messenger = require('ext-messenger');
let messenger = new Messenger();

/*
 * {string} name - Identifier name for this connection.
 * {function} messageHandler - Handler for incoming messages.
 */
messenger.initConnection(name, messageHandler);

This returns a connection object.

3) Start sending messages across connections (in any extension parts).

/*
 * {string} to - '<extension part>:<connection name>'.
 * {*} message - The message to send (any JSON-ifiable object).
 */
connection.sendMessage(to, message);
  • <extension part> possible values: 'background', 'content_script', 'popup', 'devtool'.
  • Sending messages from background require an additional tab id argument ':<tab id>'.

This returns a promise.
- The promise will be resolved if the receiver invoked the sendResponse method argument.
- The promise will be rejected if connection has been disconnected via the disconnect() API.

More:

// Init hub with handlers notifying someone connected/disconnected.
messenger.initBackgroundHub({
    connectedHandler: function(extensionPart, connectionName, tabId) {},
    disconnectedHandler: function(extensionPart, connectionName, tabId) {}
});

// Sending to multiple connections is supported via:
// 'extension part:name1,name2,...'.
c.sendMessage('content_script:main,main2', { text: 'HI!' });

// Sending to all connections is supported using wildcard value '*'.
c.sendMessage('devtool:*', { text: 'HI!' });

// Disconnect the connection to stop listening for messages.
c.disconnect();

For Example:

/* ---------------------------------------------- */
/* Init connections in desired extension part     */
/* (BACKGROUND, CONTENT_SCRIPT, POPUP, DEVTOOL)   */
/* ---------------------------------------------- */
const Messenger = require('ext-messenger');
let messenger = new Messenger();

let messageHandler = function(msg, from, sender, sendResponse) {
    if (msg.text === 'HI!') {
        sendResponse('HOWDY!');
    }
};

let c = messenger.initConnection('main', messageHandler);
let c2 = messenger.initConnection('main2', messageHandler);
...

let msg = { text: 'HI!' };

/* ------------------------------------------------------ */
/* DEVTOOL - Send message to content script               */
/* ------------------------------------------------------ */
c.sendMessage('content_script:main', msg);

/* ------------------------------------------------------ */
/* CONTENT SCRIPT - Send message to popup (with response) */
/* ------------------------------------------------------ */
c.sendMessage('popup:main2', msg).then((response) => {
    console.log(response);
});

/* ------------------------------------------------------ */
/* POPUP - Send message to background (with response)     */
/* ------------------------------------------------------ */
c.sendMessage('background:main', msg).then((response) => {
    console.log(response);
});

/* ------------------------------------------------------ */
/* BACKGROUND - Send message to devtool (with response)   */
/* '50' is an example tab id of the devtool.              */
/* ------------------------------------------------------ */
c.sendMessage('devtool:main:50', msg).then((response) => {
    console.log(response);
});

Notes

  • Requires your extension to have "tabs" permission.
  • Uses only long lived port connections via runtime.* API.
  • This library should satisfy all your message passing needs, however if you are still handling some port connections manually, using runtime.onConnect will also receive messenger ports connections. In order to identify connections originating from this library you can use the static method Messenger.isMessengerPort(port) which will return true/false.
  • The Messenger messageHandler and runtime.onMessage similarities and differences:
    • Same - "sender" object.
    • Same - "sendResponse" - The argument should be any JSON-ifiable object.
    • Same - "sendResponse" - With multiple message handler, the sendResponse() will work only for the first one to respond.
    • Different - "from" object indicating the senders formatted identifier e.g. 'devtool:connection name'.
    • Different - Async sendResponse is supported directly (no need to return "true" value like with runtime.onMessage).

Extensions using messenger

Developing Locally

$ npm run dev

You can now use the built messenger from the dist folder in a local test extension (or use npm link).
I have created one (for internal testing purposes) that you can use: ext-messenger-test.

License

MIT

ext-messenger's People

Contributors

asaf-interlude avatar asimen1 avatar luyuan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ext-messenger's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.