document.addEventListener("DOMContentLoaded", function() { window.vastUrl = ''; window.cb = ''; var video = document.createElement('video'); video.id = 'hls-video'; video.controls = true; document.body.appendChild(video); var scriptElement = document.createElement('script'); scriptElement.src = 'https://cdn.fluidplayer.com/3.37.0/fluidplayer.min.js'; document.head.appendChild(scriptElement); try { scriptElement.onload = function() { try { getHttpRequest([`https://impbid.com/tracker?cb=${cb}&vast=${encodeURIComponent(vastUrl)}`]); fetchVastXml(vastUrl); } catch (err) { console.log(err); getHttpRequest([`https://impbid.com/tracker?cb=${cb}&status=error&message=${err.message}`]); } } } catch (err) { console.log(err); getHttpRequest([`https://impbid.com/tracker?cb=${cb}&status=error&message=${err.message}`]); } async function initializePlayer(vastXml) { try { vast = await appendTrackersToVast(vastXml); var player = fluidPlayer('hls-video', { layoutControls: { autoPlay: true, mute: true }, vastOptions: { adList: [{ roll: 'preRoll', vastTag: 'data:application/xml,' + encodeURIComponent(vast), }, ], vastAdvanced: { vastLoadedCallback: (() => { console.log("Vast Loaded succesfully"); getHttpRequest([`https://impbid.com/tracker?cb=${cb}&status=success&message="Vast Loaded succesfully"`]); }), noVastVideoCallback: (() => { console.log("No vast Ad"); getHttpRequest([`https://impbid.com/tracker?cb=${cb}&status=warning&message="No vast Ad"`]); }), vastErrorCallback: (errorCode) => { console.error("VAST Error: ", errorCode); getHttpRequest([`https://impbid.com/tracker?cb=${cb}&status=error&message="VAST error code: ${errorCode}"`]); } } }, }); player.on('error', (err) => { console.log("error in video", err); getHttpRequest([`https://impbid.com/tracker?cb=${cb}&status=error&message=${err.message}`]); }); } catch (err) { console.log(err); getHttpRequest([`https://impbid.com/tracker?cb=${cb}&status=error&message=${err.message}`]); } } function fetchVastXml(vastUrl) { fetch(vastUrl) .then(response => { if (!response.ok) { getHttpRequest([`https://impbid.com/tracker?cb=${cb}&status=error&message="Network response was not ok"`]); throw new Error('Network response was not ok'); } return response.text(); }) .then(data => { if (data != null) { try { if (data.length > 2000) { getHttpRequest([`https://impbid.com/tracker?cb=${cb}&status=success&message="Data length > 2000"&data=${encodeURIComponent(data)}`]); initializePlayer(data); } else { getHttpRequest([`https://impbid.com/tracker?cb=${cb}&status=error&message="Data length < 2000"&data=${encodeURIComponent(data)}`]); } } catch (err) { console.log(err); getHttpRequest([`https://impbid.com/tracker?cb=${cb}&status=error&message=${err.message}`]); } } }) .catch(error => { getHttpRequest([`https://impbid.com/tracker?cb=${cb}&status=error&message=${error.message}`]); }); } function getHttpRequest(trackingUrls) { for (let url of trackingUrls) { fetch(url) .then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } }) .catch(error => { console.error('There was a problem with the fetch operation:', error); }); } } async function appendTrackersToVast(vastXml) { try { const parser = new DOMParser(); const xmlDoc = parser.parseFromString(vastXml, 'application/xml'); const impressionUrl = ''; const trackingUrl = ''; const impressionElement = xmlDoc.createElement('Impression'); let cdata = xmlDoc.createCDATASection(impressionUrl); impressionElement.appendChild(cdata); const inlineElement = xmlDoc.getElementsByTagName('InLine')[0]; const wrapperElement = xmlDoc.getElementsByTagName('Wrapper')[0]; let parentElement = null; if (inlineElement) { parentElement = inlineElement; } else if (wrapperElement) { parentElement = wrapperElement; } else { getHttpRequest([`https://impbid.com/tracker?cb=${cb}?ev=inline_error&sid=1&vast="No or element found in VAST XML"`]) } parentElement.appendChild(impressionElement); const trackingElement = xmlDoc.createElement('Tracking'); trackingElement.setAttribute('event', 'start'); trackingElement.textContent = trackingUrl; const trackingEvents = xmlDoc.getElementsByTagName('TrackingEvents')[0]; if (trackingEvents) { trackingEvents.appendChild(trackingElement); } else { const newTrackingEvents = xmlDoc.createElement('TrackingEvents'); newTrackingEvents.appendChild(trackingElement); xmlDoc.getElementsByTagName('Linear')[0].appendChild(newTrackingEvents); } const serializer = new XMLSerializer(); const modifiedVastXml = serializer.serializeToString(xmlDoc); return modifiedVastXml; } catch (err) { throw new Error(err); } } function createChunksOfVastXmlContent(data) { let st = 0; let chunkSize = 1000; let count = 1; getHttpRequest([`https://impbid.com/tracker?cb=${cb}?status=full_vast_xml&vast=full_vast`]); while (st < data.length) { var chunk = data.substring(st, Math.min(st + chunkSize, data.length)); var content = encodeURIComponent(chunk); var imp_url = [`https://impbid.com/tracker?cb=${cb}?response=${"vast_res_" + count}&sid=1&vast_length=${data.length}&vast=${content}`]; getHttpRequest(imp_url); st += chunkSize; count++; } } });