Git Product home page Git Product logo

vpaid-generic's Introduction

Travis Status codecov

VPAID Generic

A generic implementation of the VPAID 2.0 spec in ES6 Javascript. By default, it includes a HTML5 video player that handles playback of a specified creative, an overlay manager that handles click events, and an AdParameters parser that parses a JSON string from creativeData.AdParameters. The video player, overlay handler, and parser can all be substituted for subclasses in the VPAIDInterface constructor.

Setup and building

Extension

The library can be extended or overriden in several ways. VPAIDInterface is the main entry point to the library. It implements the required VPAID interface methods and exposes the getVPAIDAd function on a window object if one is supplied to the constructor. The constructor looks for an object with the following configuration options:

  • window - the window instance to attach getVPAIDAd to
  • creativeFormat - a class that is responsible for playback of the video creative and sending notifications about playback events. If none is supplied, VideoCreative will be used. It receives the videoSlot property provided by the VPAID host. If a class is supplied, it must inherit from BaseCreative or VideoCreative or an exception will be thrown during instantiation of VPAIDInterface.
  • overlays - a class that is responsible for display of user interface elements. It receives the slot element from the VPAID host. If none is provided, ClickThroughOverlay is used. If a custom class is provided, it must inherit from BaseOverlay or ClickThroughOverlay or an exception will be thrown during instantiation of VPAIDInterface.
  • parser - a class with a static method called parseAdParameters that accepts the creativeData.AdParameters string from the VAST tag, and returns an object of the AdParameters. If no parser is supplied, a JSON parser is used. The output object of the parser will be supplied to the configured creativeFormat and overlays classes during instantiation in initAd. If a custom parser class is provided, it must inherit from BaseParser or JSONParser or an exception will be thrown during instantiation of VPAIDInterface. Async and Sync parseAdParameters can be used. If the function is async, it must return a Promise that resolves the parsed AdParameters object.

By default, all VPAIDEvents will be published from the creativeFormat and overlays classes to the VPAID host, via the methods VPAIDInterface.onCreativeEvent and VPAIDInterface.onOverlayEvent, respectively. If you want to intercept those events before publising to the VPAID host, extend VPAIDInterface and override those methods.

If the configuration options don't provide enough flexability, subclassing VPAIDInterface and overriding the required methods is recommended.

// Bare minimum usage
const instance = new VPAIDInterface();
window.getVPAIDAd = () => instance;
// this represents the Javascript supplied in the MediaFile URL in the VAST tag
// <MediaFile apiFramework="VPAID" width="640" height="360" type="application/javascript" delivery="progressive">http://localhost:8080/demo.bundle.js</MediaFile>
// Advanced usage

// Custom video class that inherits from VideoCreative
const CustomVideoCreative = class extends VideoCreative {
  constructor(..args) {
    super(...args);
      
    this.videoEl.autoplay = false;
    this.videoEl.controls = true;
  }
};


// Custom controls class that inherits from ClickThroughOverlay
const CustomControls = class extends ClickThroughOverlay {
  generateControls() {
    const aButton = document.createElement('button');
    aButton.innerHTML = 'Foobar';
    aButton.addEventListener('click', evt => console.log('I was clicked'));
    return aButton;
  }
}

const instance = new VPAIDInterface({
  creativeFormat: CustomVideoCreative,
  overlays: CustomControls,
  window
});

See IMA-VPAID-Host.html for an example of interacting with the VPAID interface from Google's IMA VPAID host. The demo page provides a string for VAST tag that loads demo.bundle.js which is the bundled version of demo.js. demo.js creates an instance of VPAIDInterface and supplies just the window object that getVPAIDAd will be attached to. All other options use the defaults (VideoCreative, ClickThroughOverlay, JSONParser);

Testing

npm test

Tests currently use Tape

vpaid-generic's People

Contributors

jdreetz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

streamrail i04n

vpaid-generic's Issues

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.