Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
chrome-ext-messenger
Advanced tools
Small library for messaging across any extension parts (background, content script, popup or devtool).
It has a simple easy to use API, promise based callback support and more.
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 chrome messaging API is not coherent or straight forward, sometimes requiring you to use chrome.runtime.* and sometimes chrome.tabs.* depending on which extension part you are currently in.
$ npm i chrome-ext-messenger
const Messenger = require('chrome-ext-messenger');
let messenger = new Messenger();
messenger.initBackgroundHub();
This step is obligatory and should be done as early as possible in your background page.
* You can also add the library via script tag and use window['chrome-ext-messenger']
.
const Messenger = require('chrome-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.
/*
* {string} to - '<extension part>:<connection name>'.
* {*} message - The message to send (any JSON-ifiable object).
*/
connection.sendMessage(to, message);
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 using the disconnect()
API.
// 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();
/* ---------------------------------------------- */
/* Init connections in desired extension part */
/* (BACKGROUND, CONTENT_SCRIPT, POPUP, DEVTOOL) */
/* ---------------------------------------------- */
const Messenger = require('chrome-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);
});
$ 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: chrome-ext-messenger-test.
MIT
FAQs
Chrome extension message passing made easy
The npm package chrome-ext-messenger receives a total of 3 weekly downloads. As such, chrome-ext-messenger popularity was classified as not popular.
We found that chrome-ext-messenger demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.