Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

coframe

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

coframe

Cross-origin iframe messenger.

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

coframe

npm

Cross-origin iframe messenger.

npm i coframe

Usage

coframe is designed for TypeScript, though of course TS is optional. Basic usage looks like this, just be sure your iframe is fully loaded in the browser. Note the separate files.

Parent
// https://parent.com/index.js
import { connect } from "coframe";

const parent = connect(document.getElementById("my-iframe"));

parent.emit("init");
Child
// https://child.com/index.js
import { listen } from "coframe";

const child = listen();

child.on("init", (data) => {
  // do something
});

Multiple Connections

By default, coframe assumes a single connection. To enable multiple connections to the same iframe, and/or to enhance security, pass a connection name when creating a connection or listener.

This will ensure that events from other coframe instances do not interfere with yours. Plus, after establishing a connection, coframe will ignore events from any source other than the source that established that connection.

import { connect, listen } from "coframe";

const parent = connect(iframe, "my-connection");
const child = listen("my-connection");

Typescript

coframe really shines when used with TypeScript because it allows you to strictly type the events and payloads sent between windows. To do so, you'll need a shared type interface that can be included in the compiled bundles of both the parent and child windows.

Here, each key of the type corresponds to an event name, and each value corresponds to the payload of that event. For events with no payload, specify undefined.

type InitPayload = {
  name: string;
  date: string;
};

export type Events = {
  init: InitPayload;
  open: undefined;
  close: undefined;
};

Usage then looks very similar, but you'll get strict type checking in dev.

// https://parent.com/index.js
import { connect } from "coframe";
import { Events } from "./shared/events";

const parent = connect<Events>(document.getElementById("my-iframe"));

parent.emit("init", {
  name: "Truework",
  date: new Date(), // TypeError
});
parent.emit("open");
// https://child.com/index.js
import { listen } from "coframe";
import { Events } from "./shared/events";

const child = listen<Events>();

child.on("init", ({ name, date }) => {}); // strictly typed parameters!
child.on("open", () => {});

License

MIT License © Truework

Keywords

FAQs

Package last updated on 13 Apr 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc