npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@matteolacchio/windowframe

v0.0.3

Published

Simple iFrame-based utility to enable web multi-framework web component isolation and event-based communication

Downloads

4

Readme

WindowFrame

WindowFrame is a simple iframe-based JS library to provide a basic Pub-Sub messaging model for web components, both in same-origin and cross-origin scenarios.

Iframes are useful both for context isolation (especially between cross-origin web components) and complex multi-framework web apps.

My dear friend Davide and I developed this library to avoid cloning the same lines of code all over our projects, and decided to make it open source.


Usage

WindowFrame is freely available on NPM:

npm install @matteolacchio/windowframe

Once installed in your project, simply import it:

import { WindowFrameHost, WindowFrameChild } from '@matteolacchio/windowframe';

Example

//  Parent.js
import { WindowFrameHost } from '@matteolacchio/windowframe';

const wfh = new WindowFrameHost(document.getElementById("my-iframe"), "https://my-child.org");

wfh.addEventListener("loaded", (payload) => { 
    console.log(payload);           //  Expecting: 'Hello from child!';
    wfh.postEvent("response", 'Hello from parent!');
});
//  Child.js
import { WindowFrameChild } from '@matteolacchio/windowframe';

const wfc = new WindowFrameChild("https://my-parent.net");

wfc.addEventListener("response", (payload) => { 
    console.log(payload);           //  Expecting: 'Hello from parent!';
});

wfc.postEvent("loaded", 'Hello from child!');

API

Messaging features are exposed by WindowFrameHost and WindowFrameChild class instances. Needless to say, WindowFrameHost should be used by the parent window, while WindowFrameChild by the child one.

Constructors

WindowFrameHost(iframe_node, iframe_origin):

  • iframe_node: reference to the iframe node you want to communicate to. To support multiple iframes, please create multiple WindowFrameHost instances.
  • iframe_origin: child iframe origin, which can either be:
    • <protocol>://<domain><port> format, e.g. https://www.example.com:443
    • * if any origin is allowed

WindowFrameChild(host_origin):

  • host_origin: allowed origin pattern for the host. Wildcards can be used, for instance http://localhost:*

Methods

Constructors aside, both classes share the same messaging methods:

postEvent(event, data):

  • event: string representing the event name.
  • data: any JSON-serializable payload to be sent to the peer.

addEventListener(event, callback):

  • event: string representing the event name to listen to.
  • callback: callback function to trigger. If the triggering event has a payload, it will be de-serialized and passed to the callback as its first argument.

removeEventListener(event, callback):

  • event: string representing the event name to remove the associated callback function from.
  • callback: callback function to remove.

destroy(): no arguments, it will clean up all native event listeners for the current instance.


Changelog

  • v0.0.3: Fixed a major bug in cross-origin environments, added documentation.
  • v0.0.2 (DO NOT USE)!: Changed bundled library name to index.js for more straightforward imports.
  • v0.0.1 (DO NOT USE)!: First working version of WindowFrame ✨.