cycle-extensions
Cycle.js drivers to use in Chrome extensions and WebExtensions.
Drivers
hostPageDriver
hostPageDriver
accepts a stream of JavaScript snippets to execute in the active tab, and returns a stream of the results of those snippets.
For example, here's a popup that displays a list of all the links in the active tab:
; ; { return hostPage: Observable DOM: links$ }
messagesDriver
In the Web Extension architecture, logic and state both live in a long-running background page. The user interface lives in a separate page called a popup. The two pages communicate by passing messages between each other. The background page awaits connections; the popup initiates them.
messagesDriver
accepts a stream of messages to send to the other page and returns a stream of messages received from the other page.
makeMessagesDriver
takes a single named argument, shouldInitiate
. Set it to false
in the background page and true
in any other pages.
Here's an example of a simple counter. Notice that when the popup opens, it sends a request for the current state to the background page. The response it receives creates the first frame of DOM.
// In your background script ; ; { const initialState = count: 0 ; const state$ = messages$; return messages: state$ } // and in your popup script; ; { const count$ = messages$; return messages: DOM DOM: count$ }
To see an real extension written with these drivers, check out the midicast source code.
Installation
yarn add cycle-extensions