Giter VIP home page Giter VIP logo

frame-messenger's Introduction

Frame Messenger

Build Status

A tiny JavaScript class to simplify communication between different browser frames, supporting both promises and callbacks.

Installation

npm install frame-messenger

Documentation

new FrameMessenger(options)

var messenger = new FrameMessenger({
  frame: window,
  targetFrame: window.parent,
  name: 'iframe',
  targetName: 'topFrame',
  Promise: window.Promise // optional
});

Options

frame {Window} required

Frame should be a reference to the current window (i.e., window)

name {String} required

An arbriary name to assign to the current frame. Other frames must use this name when initiating communication.

targetFrame {Window} required

A reference to the frame we will be communicating with (e.g., parent, top, opener, iframeEl.contentWindow)

targetName {Window} required

An arbriary name to assign to the other frame we want to communicate with. The other frame must use this same name to reference itself (see examples below).

Promise {Promise constructor} optional

A promise constructor to use for creating promises. When not passed, regular node-style callbacks will be used.

messenger.postMessage()

Send a message to the other frame:

var messenger = new FrameMessenger({
  frame: window,
  targetFrame: parent,
  name: 'iframe',
  targetName: 'topFrame'
});

messenger.postMessage('Hello top frame!');

Pass in a callback to process the response from the other frame:

messenger.postMessage('Hello top frame!', function(err, data) {
  if (err) {
    throw new Error(err);
  }

  console.log('The other frame replied with: ' + data);
});

Alternately, postMessage will return a promise if FrameMessenger was invoked with a Promise property:

var messenger = new FrameMessenger({
  frame: window,
  targetFrame: window.parent,
  name: 'iframe',
  targetName: 'topFrame',
  Promise: RSVP.Promise  // choose your favorite promise library
});

messenger.postMessage('Hello top frame!').then(function(data) {
  console.log('The other frame replied with: ' + data);
}, function(err) {
  // Process error
});

You can maintain an open communication with the other frame by utilizing the callback argument that is passed to the postMessage callback:

messenger.postMessage('Hello top frame!', function(err, data, callback) {
  if (err) {
    throw new Error(err);
  }

  console.log('The other frame replied with: ' + data);

  // Like node callbacks, the first parameter passed to the callback
  // should be either an error object/message or null (if there are no errors)
  callback(null, 'Thanks for the response!');
});

messenger.onMessage(data, callback)

Handle messages from the other frame with onMessage:

messenger.onMessage(function(data) {
  console.log('The targetFrame sent me: ' + data);
});

The other frame can send a response back by utilizing the callback argument passed to onMessage:

messenger.onMessage(function(data, callback) {
  if (data === 'Knock knock') {
    callback(null, "Who's there?")
  } else {
    callback(new Error('I only know knock knock jokes.'));
  }
});

See the tests for other examples.

Tests

Running the tests:

npm install
npm test

License

This project is distributed under the MIT license.

frame-messenger's People

Contributors

jakedetels avatar

Stargazers

Adam Burke avatar Cary Dunn avatar Michael avatar

Watchers

James Cloos avatar  avatar

Forkers

dbroytman

frame-messenger's Issues

Hi Jake

Cool Project.

Can this be used to send a message from the parent iframe and then in a script tag in the nested iframe catch the message? I'm having trouble when I split up the messengers code across a parent and child frame.

When I access the child frame in the parent scope(similar to the tests) however it all works but I'm not sure how I then use that to say restrict inter iframe communication to allow only messaging between them.

I am clearly a tad confused.

Regards,

Adam

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.