Skip to content

Commit

Permalink
Merge pull request #64 from basil79/aderror-as-object
Browse files Browse the repository at this point in the history
refactor vpaid iframe
  • Loading branch information
basil79 authored Sep 19, 2022
2 parents c3b8738 + 629499b commit 001244d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
2 changes: 1 addition & 1 deletion dist/ads-manager.es.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ads-manager.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions public/js/ads-manager.js

Large diffs are not rendered by default.

30 changes: 12 additions & 18 deletions src/ads-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ const AdsManager = function(adContainer) {
this._mediaFile = null;

this._isVPAID = false;
this._vpaidIframe = null;
this._vpaidCreative = null;

// Timers, Intervals
Expand Down Expand Up @@ -736,33 +737,27 @@ AdsManager.prototype._handleCreativeMessage = function(msg) {
}
}
AdsManager.prototype.loadCreativeAsset = function(fileURL) {
const vpaidIframe = document.getElementById('vpaidIframe'),
iframe = document.createElement('iframe');
this._vpaidIframe = document.createElement('iframe');
this._adContainer.appendChild(this._vpaidIframe);

iframe.id = 'vpaidIframe';
//vpaidIframe == null ? document.body.appendChild(iframe) : document.body.replaceChild(iframe, vpaidIframe);
vpaidIframe == null
? this._adContainer.appendChild(iframe)
: this._adContainer.replaceChild(iframe, vpaidIframe);

iframe.style.display = 'none';
iframe.style.width = '0px';
iframe.style.height = '0px';
this._vpaidIframe.style.display = 'none';
this._vpaidIframe.style.width = '0px';
this._vpaidIframe.style.height = '0px';

window.addEventListener('message', this._handleCreativeMessageFn);

iframe.contentWindow.document.open();
iframe.contentWindow.document.write(`
this._vpaidIframe.contentWindow.document.open();
this._vpaidIframe.contentWindow.document.write(`
<script>function sendMessage(msg) {
var post = 'adm://' + JSON.stringify(msg);
window.parent.postMessage(post, '*'); }
\x3c/script>
<script type="text/javascript" onload="sendMessage('load')" onerror="sendMessage('error')" src="${fileURL}"> \x3c/script>
`);
iframe.contentWindow.document.close();
this._vpaidIframe.contentWindow.document.close();

this._creativeLoadTimer = setInterval(() => {
let VPAIDAd = document.getElementById('vpaidIframe').contentWindow.getVPAIDAd;
let VPAIDAd = this._vpaidIframe.contentWindow.getVPAIDAd;
VPAIDAd &&
typeof VPAIDAd === 'function' &&
(clearInterval(this._creativeLoadTimer),
Expand All @@ -777,9 +772,8 @@ AdsManager.prototype.loadCreativeAsset = function(fileURL) {
}
AdsManager.prototype.removeCreativeAsset = function() {
// Remove VPAID iframe
const vpaidIframe = document.getElementById('vpaidIframe');
if(vpaidIframe) {
vpaidIframe.parentNode.removeChild(vpaidIframe);
if(this._vpaidIframe) {
this._vpaidIframe.parentNode.removeChild(this._vpaidIframe);
}
}
AdsManager.prototype._abort = function() {
Expand Down

0 comments on commit 001244d

Please sign in to comment.