{{ page.title }}

{{ page.description }}

Introduction

When getting started implementing OTT Plugins it is imperative to understand these plugins use an event based framework. Essential to the process is using window.postMessage, which provides easy cross domain functionality.

The following diagram provides an overview of plugin functionality, emphasizing event dispatching and handling:

The rest of this document describes fundamentals of using events in your plugins.

Dispatching events

Some of the OTT plugin elements have events you can dispatch. The following describes how to dispatch events in OTT Plugins.

The generic code for dispatching an event is:

window.postMessage({
  event: "eventType",
  data: {
    key1: value1,
    key2: value2,
    ...
  }
}, window.location.origin)

where

The following is a functioning example that adds a button to a details page, passing data for:

window.postMessage({
  event: 'detailsPageAddCustomButton',
  data: {
    title:  'Test Button',
    font_awesome_icon: 'fa fa-info-circle',
    element_id: 'TEST_BTN_ID'
  }
}, window.location.origin);

The button created would appear as follows:

Handling events

When events are dispatched, of course they must be handled. The basic syntax of the addEventListener() method is:

document.addEventListener(event, function, useCapture)

where

Following is the generic code for handling an event in OTT plugin code:

window.addEventListener("message", (event) => {
  const originsAllowed = [
    'validhost1',
    ...
  ];
  if (originsAllowed.includes(event.origin)) {
    // event.data.event contains the event name
    // event.data.data contains event data
  }
},
false
);

where

The following is a functioning example that simply displays two different parts of the event object:

window.addEventListener("message", (event) => {
  const originsAllowed = [
    'https://myapplocation.brightcove.com'
  ];
  if (originsAllowed.includes(event.origin)) {
    console.log('event.data.event: ', event.data.event);
    console.log('event.data.data: ', event.data.data);  }
},
false
);

It is not uncommon to use the values of the event object in your code. Here are example values for event.data.event and event.data.data when an onBeaconPageLoad event is handled:

event object