Ultra minimalistic UI/Web components and Chrome extension Javascript framework. (OMG not another one!)
There are two parts to this "framework" that are related but can be used independantly from eachother:
- KISS-WebUI, This is a minimal WebComponent framework that provides just enough scaffold to define a UI App and Components
- KISS-Chrome-extension, This is a minimal library (yes, this one is just a library) to help with writing Chrome extensions
And I suppose there is third part to all of this:
- Set of boilerplate templates that go along with the peices mentioned above, as a way to setup projects quickly and define the intended approach
- Structure to define UI web apps
- A simplified way to define WebComponents
- Basic utils to work with WebComponent HTML templates and shadowdom
- Opinionated approach to interaction between UI app and "business" logic
- Using "public" events and "public" UI app commands
- A message dispatching and command registration system to allow interactions between the extension and the content sctipts (where the actual website code/ data reside)
- A way to easily inject content scripts in the site and use code sparated in ECMA modules
Start by creating root UI app component as seen in the template below. All componenets inherit from the BaseComponent class follow the structure shown in the web_ui
templates, this is what takes care of handling all the WebComponents boilerplate.
The creating and hydrating the Components DOM is done in the componenets buildComponent
method. Here we get a new template instance from this.defaultTemplate
and are then able to manipulate the template instance DOM using the provided DOM utils under templateInstance.utils
. This template instance is returned by the buildComponent
method and becomes the components shadow DOM.
kiss-components.js/boilerplate_templates/web_ui/ui_app_template.js
Lines 23 to 30 in 632dbce
Components shadow DOM can be manipulated to up dynamically update the component by using the utilities on the component instance under this.domUtils
.
kiss-components.js/boilerplate_templates/web_ui/ui_app_template.js
Lines 36 to 41 in 632dbce
On the root componenet is where we declare any "public" commands that the business logic will use to request change in the UI. In the case of the template file here we are also declaring our "public" events that the UI will issue when it needs to communicate something to the business logic. You can see how the business logic, instanciates the UI app, subscribes to those public events, and calls UI commands in the template below.
kiss-components.js/boilerplate_templates/web_ui/main.js
Lines 1 to 25 in 632dbce
The chrome extension libarary allows to inject a contnet script in the webpage and have the extension execute commands that have been define in the content scripts by using a command channel that is established between both. This allows the extension to manipulate the underlying web page, extract data from its DOM, issue fetch request in the pages/tabs context etc.
Here you can see how the extensions script is able to issue a command to execute in the active tab and get back the results of that command:
And below is the basic way to set up those commands in the content script module:
kiss-components.js/boilerplate_templates/chrome_extension/main.js
Lines 1 to 25 in 632dbce
The main content script module will be automatically injected in the page, allowing for ECMA style modules loading for any additioal code that needs to run in the page/ tab sandbox. The library finds the the main content script module by looking for the first file with name main.js
listed under the first webresource listed in manifest.json
, so make sure set that up accordingly:
For more complete details look here:
Checkout:
https://github.com/izo0x90/kiss-components.js/tree/632dbcef2c082332a8bcdd66ad8fcdc3d6ebd3bb/example
and