Skip to content

Commit

Permalink
fix renderCrossDomain after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Michele Nasti committed May 26, 2022
1 parent 74e15ed commit ddf060f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 44 deletions.
19 changes: 10 additions & 9 deletions src/nativeRenderManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,20 @@ export function newNativeRenderManager(win) {
sendMessage = prebidMessenger(nativeTag.pubUrl, win);
const nativeAssetManager = newNativeAssetManager(window, nativeTag.pubUrl);

if (targetingData.hasOwnProperty('adId')) {

if (nativeTag.hasOwnProperty('rendererUrl') && !nativeTag.rendererUrl.match(/##.*##/i)) {
const scr = document.createElement('SCRIPT');
scr.src = nativeTag.rendererUrl,
scr.id = 'pb-native-renderer';
if (nativeTag.hasOwnProperty("adId")) {
if (
nativeTag.hasOwnProperty("rendererUrl") &&
!nativeTag.rendererUrl.match(/##.*##/i)
) {
const scr = document.createElement("SCRIPT");
(scr.src = nativeTag.rendererUrl), (scr.id = "pb-native-renderer");
document.body.appendChild(scr);
}
nativeAssetManager.loadAssets(targetingData.adId, fireNativeCallback);
nativeAssetManager.loadAssets(nativeTag.adId, fireNativeCallback);
fireNativeCallback();
fireNativeImpTracker(targetingData.adId);
fireNativeImpTracker(nativeTag.adId);
} else {
console.warn('Prebid Native Tag object was missing \'adId\'.');
console.warn("Prebid Native Tag object was missing 'adId'.");
}
}

Expand Down
50 changes: 28 additions & 22 deletions src/renderingManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,32 @@ export function renderCrossDomain(win, adId, pubAdServerDomain = '', pubUrl) {
const sendMessage = prebidMessenger(pubUrl, win);

function renderAd(ev) {
let key = ev.message ? 'message' : 'data';
let key = ev.message ? "message" : "data";
let adObject = {};
try {
adObject = JSON.parse(ev[key]);
} catch (e) {
return;
}

if (adObject.message && adObject.message === 'Prebid Response' &&
adObject.adId === adId) {
if (
adObject.message &&
adObject.message === "Prebid Response" &&
adObject.adId === adId
) {
try {
let body = win.document.body;
let ad = adObject.ad;
let url = adObject.adUrl;
let width = adObject.width;
let height = adObject.height;

if (adObject.mediaType === 'video') {
if (adObject.mediaType === "video") {
signalRenderResult(false, {
reason: 'preventWritingOnMainDocument',
message: `Cannot render video ad ${adId}`
reason: "preventWritingOnMainDocument",
message: `Cannot render video ad ${adId}`,
});
console.log('Error trying to write ad.');
console.log("Error trying to write ad.");
} else if (ad) {
const iframe = getEmptyIframe(adObject.height, adObject.width);
body.appendChild(iframe);
Expand All @@ -78,35 +81,38 @@ export function renderCrossDomain(win, adId, pubAdServerDomain = '', pubUrl) {
signalRenderResult(true);
} else if (url) {
const iframe = getEmptyIframe(height, width);
iframe.style.display = 'inline';
iframe.style.overflow = 'hidden';
iframe.style.display = "inline";
iframe.style.overflow = "hidden";
iframe.src = url;

insertElement(iframe, document, 'body');
insertElement(iframe, document, "body");
signalRenderResult(true);
} else {
signalRenderResult(false, {
reason: 'noAd',
message: `No ad for ${adId}`
reason: "noAd",
message: `No ad for ${adId}`,
});
console.log(`Error trying to write ad. No ad markup or adUrl for ${adId}`);
console.log(
`Error trying to write ad. No ad markup or adUrl for ${adId}`
);
}
} catch (e) {
signalRenderResult(false, { reason: "exception", message: e.message });
console.log(`Error in rendering ad`, e);
}
}

function signalRenderResult(success, { reason, message } = {}) {
const payload = {
message: 'Prebid Event',
adId,
event: success ? 'adRenderSucceeded' : 'adRenderFailed',
};
if (!success) {
payload.info = { reason, message };
function signalRenderResult(success, { reason, message } = {}) {
const payload = {
message: "Prebid Event",
adId,
event: success ? "adRenderSucceeded" : "adRenderFailed",
};
if (!success) {
payload.info = { reason, message };
}
sendMessage(payload);
}
sendMessage(payload);
}

function requestAdFromPrebid() {
Expand Down
19 changes: 10 additions & 9 deletions test/spec/nativeRenderManager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ describe('nativeRenderManager', function () {
consoleWarn.restore();
});

it('should verify the postMessage for impression trackers was executed', function() {
mockWin.document.getElementsByClassName = () => [{
attributes: {
pbAdId: {
value: 'ad123'
}
},
addEventListener: (type, listener, capture) => {
it("should verify the postMessage for impression trackers was executed", function () {
mockWin.document.getElementsByClassName = () => [
{
attributes: {
pbAdId: {
value: "ad123",
},
},
addEventListener: (type, listener, capture) => {},
},
}];
];
let nativeTracker = new newNativeRenderManager(mockWin);
nativeTracker.renderNativeAd(mockWin.document, tagData);

Expand Down
8 changes: 4 additions & 4 deletions test/spec/renderingManager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ describe('renderingManager', function() {
});
}

it('should render cross domain creative', function() {
it("should render cross domain creative", function () {
mockPrebidResponse({
ad: 'ad',
ad: "ad",
adUrl: ORIGIN,
adId: '123',
adId: "123",
width: 300,
height: 250
height: 250,
});
expect(mockIframe.contentDocument.write.args[0][0]).to.equal("ad");
});
Expand Down

0 comments on commit ddf060f

Please sign in to comment.