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.
npm install- Build:
npm run build - Dev:
npm run devhosts IMA-VPAID-Host.html at http://localhost:8080/IMA-VPAID-Host.html
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 attachgetVPAIDAdtocreativeFormat- a class that is responsible for playback of the video creative and sending notifications about playback events. If none is supplied,VideoCreativewill be used. It receives thevideoSlotproperty provided by the VPAID host. If a class is supplied, it must inherit fromBaseCreativeorVideoCreativeor an exception will be thrown during instantiation of VPAIDInterface.overlays- a class that is responsible for display of user interface elements. It receives theslotelement from the VPAID host. If none is provided,ClickThroughOverlayis used. If a custom class is provided, it must inherit fromBaseOverlayorClickThroughOverlayor an exception will be thrown during instantiation of VPAIDInterface.parser- a class with a static method calledparseAdParametersthat accepts thecreativeData.AdParametersstring 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 configuredcreativeFormatandoverlaysclasses during instantiation ininitAd. If a custom parser class is provided, it must inherit fromBaseParserorJSONParseror an exception will be thrown during instantiation of VPAIDInterface. Async and SyncparseAdParameterscan 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);
npm test
Tests currently use Tape