Socket
Socket
Sign inDemoInstall

@ampproject/viewer-messaging

Package Overview
Dependencies
0
Maintainers
13
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @ampproject/viewer-messaging

Messaging between an AMP Doc and a Viewer


Version published
Weekly downloads
128
decreased by-14.09%
Maintainers
13
Install size
28.8 kB
Created
Weekly downloads
 

Readme

Source

AMP Viewer Messaging

npm version

The AMP Viewer Messaging library allows an AMP Viewer to establish a communication channel with an AMP document.

See Connecting AMP Viewers with AMP pages for more information.

Installation

Install via:

npm i @ampproject/viewer-messaging

Usage

Assuming you have an AMP document hosted on https://example.com/amp-document:

import {Messaging} from '@ampproject/viewer-messaging';

const iframe = document.createElement('iframe');
iframe.setAttribute(
  'src',
  `https://example.com/amp-document#origin=${window.location.origin}`
);
document.body.appendChild(iframe);

const messaging = await Messaging.waitForHandshakeFromDocument(
  /* source window */ window,
  /* target window */ iframe.contentWindow,
  /* target origin */ 'https://example.com'
);
messaging.setDefaultHandler((name, data, rsvp) => {
  console.log(`Received message: ${name}`);
});
// use messaging.sendRequest(...) to make requests to the AMP document

Note: The AMP document needs to include the amp-viewer-integration script:

<script async src="https://cdn.ampproject.org/v0/amp-viewer-integration-0.1.js"></script>

Keywords

FAQs

Last updated on 28 Jan 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc