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

framehost

v0.0.6

Published

A lightweight cross domain communication library

Downloads

3

Readme

framehost - A lightweight cross domain communication library

Support data communication between iframe, browser new tab windows (Note: IE10+)

中文文档

Install

npm install framehost --save

How to use?

import FrameHost from 'framehost';

1.iframe code example

// parent.js

const iframe = document.querySelector('iframe');

// Define actions
// When the message is received, the matching method is triggered. The parameter is the received data. Multiple methods can be defined in action objects.
const actions = {
  doParentAction: (data) => {
    console.log(data);
  }
}

// You can set up a secure domain name source, default to '*'.
const origin = 'http://www.children.com';

// Init framehost
const framehost = new FrameHost(actions, origin, iframe.contentWindow);

// Send a message to the iframe window, execute the 'doChildrenAction' method defined in iframe, and pass the data to it. The data can be object, number, string, boolean, array.
framehost.postMessage({
  // Action defined in children.js
  action: 'doChildrenAction',

  // Send data
  data: {
    message: 'this is a message for children.js',
  }
});
// children.js

const actions = {
  doChildrenAction: (data) => {
    console.log(data);
  }
}
const origin = 'http://www.parent.com';
const framehost = new FrameHost(actions, origin);

framehost.postMessage({
  action: 'doParentAction',
  data: 'send message to parent.js'
})

2.browser tab windows code example

This is basically the same as the iframe sample code above, except that the third window parameter passed when the FrameHost is initialized is different.

// pageA

const actions = {
  pageA: (data) => {
    console.log(data);
  }
}
const origin = 'http://www.pageB.com';

const targetWin = window.open('http://www.pageB.com/index.html');
const framehost = new FrameHost(actions, origin, targetWin);

framehost.postMessage({
  action: 'pageB',
  data: 'send message to pageB'
})
// pageB

const actions = {
  pageB: (data) => {
    console.log(data);
  }
}
const origin = 'http://www.pageA.com'
const framehost = new FrameHost(actions, origin);

framehost.postMessage({
  action: 'pageA',
  data: 'send message to pageA'
})

API

new FrameHost(actions: Object, [origin: String], [targetWin: Window])

Initializing FramHost can receive three parameters:

actions

Type is an object and keys are functions. When a message is received, the key in the object is matched and the corresponding method of key is executed, such as:

const actions = {
  doSomething1: (data) => {
    console.log(data);
  },
  doSomething2: (data) => {
    console.log(data);
  },
  doSomething3: (data) => {
    console.log(data);
  }
};
origin

Message source, when the origin parameter is provided, only messages from the domain name set by origin are processed. default to '*'.

targetWin

The target window for message sending, if the message is sent to the iframe window, the parameter value is iframe.contentWindow. If the message is sent to the browser tab window, the parameter value is the window reference returned after the window.open() method is executed.

postMessage({action: String, data: Any}, callback: Function)

postMessage([{action: String, data: Any}, ...], callback: Function)

The method of sending messages is an instance method of FrameHost. This method receives an object or an array of objects and a callback function as parameters. The parameter object must contain action and data fields, action represents the method name that the target window needs to execute, and data represents the data transferred. The callback function will be called immediately after all messages are sent out, and callback is optional. Such as:

const framehost = new FrameHost(actions, origin, targetWin);
framehost.postMessage({
  action: 'doSomething1', // The name of the method to execute in the target window.
  data: 'I am a message data', // Support object/array/number/string/boolean data type.
}, () => {
  console.log('message sent');
});

// or
/*framehost.postMessage([
  {
    action: 'doSomething1',
    data: 'I am a message data',
  },
  {
    action: 'doSomething2',
    data: 'I am a another message data',
  },
], () => {
  console.log('all message sent');
});*/

destroy()

If it is determined that there is no need to continue sending messages, the destroy function can be released. Note: after calling the function, you cannot continue sending messages. Such as:

framehost.postMessage({
  action: 'doSomething1',
  data: 'I am a message data',
}, () => {
  console.log('the message has been sent, and there is no need to send the message again.');
  framehost.destroy();
});

// The following code will not continue to work because the destroy method has been invoked before.
framehost.postMessage({
  action: 'doSomething2',
  data: 'I am a another message data',
});

Example

A working example can be found in the example directory,Please ensure that domain name mapping is set up locally.

127.0.0.1 example.a.com
127.0.0.1 example.b.com

run npm run example,And open http://example.a.com:9000/iframe or http://example.a.com:9000/window in browser.