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

droopy-postmessage

v1.0.0

Published

Wraps HTML5 PostMessage API to support callbacks and promises

Downloads

11

Readme

Droopy PostMessage

Wrapper for HTML5 PostMessage api to add support for callbacks and promises.


##Why? Let's say you have 2 web apps, and want to iframe App B inside of App A (maybe you have a Wordpress site and want to embed a widget hosted by a separate custom web application). For security reasons, windows can not communicate cross-origin. So your IFramed custom widget will not be able to access window.top values. To solve this problem we have the HTML5 PostMessage API.

The native PostMessage API is very simple and only allows passing a message string and a target window. Droopy PostMessage builds on this native capability to allow sending javascript objects and handles querying another window and waiting for a response (through callbacks or promises). ##Install & Setup

Option 1: Node.js Prereq: Install Node.js, go for highest version.

#Install the droopy-postmessage module in your project
npm install --save droopy-postmessage

Option 2: Global Include

  1. Download the source code and include it on your page:

Last, you need to include droopyPostMessage.js on both pages, the parent page, and the iframed page.

##Methods

  • droopyPostMessage.subscribe(key, handler) - allows a page to listen for a particular key
  • droopyPostMessage.unsubscribe(key, handler) - disables the handler function that is bound to that key
  • droopyPostMessage.post(targetWindow, key, payload) - sends a message to another window. If the targeted window has subscribed to the key, the targeted window's handler will receive the payload
  • droopyPostMessage.postParent(key, payload) - syntax sugar that calls .post() with window.top as the target window
  • droopyPostMessage.query(targetWindow, key, payload, cb)- sends a message to another window with the expectation that the target window is listening for that event key and will respond back. The cb function will be invoked when the target window responds. If the browser supports Promises, a Promise will be returned as an alternative to a callback
  • droopyPostMessage.queryParent(key, payload, cb) - syntax sugar that calls .query() with window.top as the target window.

##Example Usage

Parent Window - has a global variable _privateNumber, and an IFramed widget. The parent window trusts the IFramed widget and wants to allow it to get and set the _privateNumber

window._privateNumber = 0;
var handlers = {
    setNumber: function(event, newNumber) {
	    // Update with a value passed from the IFramed Page
        window._privateNumber = newNumber;
    },
    getNumber: function(event) {
	    // Send a response back to the IFramed page
        event.respond(window._privateNumber);
    }
};
// Listen for the iframed window to send a message
droopyPostMessage.subscribe("get-number", handlers.getNumber);
droopyPostMessage.subscribe("set-number", handlers.setNumber);

IFramed Window - The IFamed window wants to be able to send messages to the parent to get and set the private number

// This will throw an ERROR if cross-origin
var number = window.top._privateNumber

// Tell the parent window to update the number
droopyPostMessage.postParent("set-number", 5);

// Get the number from the parent window
droopyPostMessage.queryParent("get-number", null, function(num) {
	console.log("This number should be 5: " + num);
})

// PROMISE instead of callback
droopyPostMessage.queryParent("get-number").then(function(num) {
	console.log("This number should be 5: " + num);
})