video-ad-sdk
Version:
VAST/VPAID SDK that allows video ads to be played on top of any player
43 lines (42 loc) • 1.42 kB
JavaScript
import { linearEvents } from '../../../../tracker';
const { clickThrough } = linearEvents;
const createDefaultClickControl = () => {
const anchor = document.createElement('a');
anchor.classList.add('mol-vast-clickthrough');
anchor.style.width = '100%';
anchor.style.height = '100%';
anchor.style.position = 'absolute';
anchor.style.left = '0';
anchor.style.top = '0';
return anchor;
};
export const onClickThrough = ({ videoElement, element }, callback, { clickThroughUrl, pauseOnAdClick = true, createClickControl = createDefaultClickControl } = {}) => {
const placeholder = element || videoElement.parentNode;
const anchor = createClickControl();
const isVirtual = !document.body.contains(anchor);
if (isVirtual) {
placeholder.appendChild(anchor);
}
if (clickThroughUrl && anchor instanceof HTMLAnchorElement) {
anchor.href = clickThroughUrl;
anchor.target = '_blank';
}
anchor.onclick = (event) => {
event.stopPropagation();
if (videoElement.paused && pauseOnAdClick) {
event.preventDefault();
videoElement.play();
}
else {
if (pauseOnAdClick) {
videoElement.pause();
}
callback(clickThrough);
}
};
return () => {
if (isVirtual) {
placeholder.removeChild(anchor);
}
};
};