msgx
A tiny, flexible, fast, promise-based library for messaging in Firefox and Chrome extensions.
Install
npm install --save msgx
Example
Try this example yourself with cd test_extension && npm run build
Content Script
const actions = console { console }const debug = trueconst msg = // 7 // 10
Background Page
const actions = datasum += n browsertabs { { if sendertabid === tabId } browsertabsonZoomChange dataonZoomChange = onZoomChange datasum = 0} { console browsertabsonZoomChange}const debug = true
API
server
- server -
(actions, onConnect, onDisconnect, debug) => void
- actions - an object in which keys are message endpoints whose values are actions, which are functions to call when a message is received. The return value of an actions is sent as the response.
- action -
(arg, sender, data) => result
- arg - the message's argument, or null if none or undefined
- sender - the MessageSender of the client that sent the message
- data - data object that exists for the connection's lifetime. Use it to store data that should be garbage collected on disconnect.
- result - value (potentiall void) to be sent as the response. If a promise, first resolved.
- action -
- onConnect -
(sender, msg, data) => void
- sender - the connecting client's MessageSender
- msg -
(action, arg) => void
- action - a string identifying the client action that should be triggerd by a message from the server
- arg - the argument that should be passed to the action
- data - data object that exists for the connection's lifetime
- onDisconnect -
(sender, data) => void
- sender - the disconnecting client's MessageSender
- data - data object that exists for the connection's lifetime
- debug - a boolean that enables logging, default is true
- actions - an object in which keys are message endpoints whose values are actions, which are functions to call when a message is received. The return value of an actions is sent as the response.
client
- client -
(actions, onDisconnect, debug) => msg
- actions - an object in which keys are message endpoints whose values are actions, which are functions called by the client when a message is received. The return value is ignored.
- action -
(arg) => any
- arg - the message's argument, or null if none or undefined
- action -
- onDisconnect -
(sender) => void
- sender - the client's MessageSender
- debug - a boolean that enables logging, default is true
- msg -
(action, arg) => Promise<result>
- action - a string identifying the server action that should be triggerd by a message from the client
- arg - the argument that should be passed to the action
- result - the value returned by the action
- actions - an object in which keys are message endpoints whose values are actions, which are functions called by the client when a message is received. The return value is ignored.
The Best Docs
msgx is tiny! Read its source code below.
client.js
{ const port = chromeruntime let t = 0 const transactions = {} portonMessage onDisconnect && portonDisconnect return { t++ if debug console port transactionst = resolve }}
server.js
{ chromeruntimeonConnect}