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

postmessage-component

v0.1.2

Published

Cross domain messaging made easy with postMessage

Downloads

5

Readme

PostMessage

Build Status

Cross domain messaging made easy with window.postMessage.

Instalation

As component:

$ component install cayasso/postmessage

Or you can just grab the postmessage.js bundle file and include it in your page:

<script src="postmessage.js"></script>

Usage

On the main window

var target = document.getElementById('myiframe').contentWindow;
var pub = postmessage('pub')();
var sub = postmessage('sub')(target);

sub.bind(function(message){
  console.log(message); // -> THANK YOU
});

pub.send('HOOOOOLA');

On the iframe

var pub = postmessage('pub')();
var sub = postmessage('sub')();

sub.subscribe(function(message, event){
  console.log(message); // -> HOOOOOLA

  // send back to parent window
  pub.send('THANK YOU');
});

API

postmessage([type])

This method return a postmessage type, this can be pub for publishing or sub for subscribing.

var Pub = postmessage('pub');
var Sub = postmessage('sub');

Pub([target])

Create a publisher instance, a target window can be passed as first argument, if none the target window will be window.parent.

var Pub = postmessage('pub');
var pub = Pub(targetWindow);

pub.target([window]);

Target window setter and/or getter, if no argument is passed it will get the current target window, else it will set a target window.

pub.target(targetWindow);

// get the current target window
var target = pub.target();

pub.origin([domain]);

Target origin setter and/or getter, if no argument is passed it will get the current target origin, else it will set a target origin.

pub.origin('http://example.com');

// get the current target window
console.log(pub.origin()); // -> http://example.com

pub.defaults()

Reset publisher target window and origin to defaul values.

pub.origin('http://example.com');

// reset publisher
pub.defaults();

// get the current target window
console.log(pub.origin()); // -> '*'

pub.send(data, [target, [origin]])

Send a message to a target window. You can pass the target and origin as second and third parametters correspondingly.


// data is serialized by JSON
pub.send({ hello: 'world' });

// or with a target window:
pub.send({ hello: 'world' }, targetWindow);

// or with specifying the origin:
pub.send({ hello: 'world' }, targetWindow, 'http://example.com');

Sub([window])

Create a subscriber instance, a window can be passed as first argument, if none the default window will be window.

var Sub = postmessage('sub');
var sub = Sub(myWindow);

sub.origin(domain)

Add origin domains to white list.

sub
.origin('http://example.com')
.origin('http://simple.com')
.origin('http://hello.com');

sub.bind(fn)

Bind or subscribe to post messages for the specified window.

sub
.bind(function(message, event){
  console.log(message);
});

sub.unbind([fn])

Unbind or unsubscribe from window post messages. If no funciton is passed it will unbind all listeners.

// unbind by passing the pointer function
sub.unbind(myFunction);

// unbind all listeners
sub.unbind();

sub.destroy()

This unbind all listeners from subscriber and then destroy it.

sub.destroy();

Run tests

First in the root directory do:

$ npm install

Then run the test like this:

$ make test

License

(The MIT License)

Copyright (c) 2013 Jonathan Brumley <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.