Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace missing native assets with empty strings #171

Merged
merged 3 commits into from
Aug 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/nativeAssetManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ export function newNativeAssetManager(win, pubUrl) {
}
}



function getCacheEndpoint(cacheHost, cachePath) {
let host = (typeof cacheHost === 'undefined' || cacheHost === "") ? DEFAULT_CACHE_HOST : cacheHost;
let path = (typeof cachePath === 'undefined' || cachePath === "") ? DEFAULT_CACHE_PATH : cachePath;
Expand Down Expand Up @@ -170,6 +168,10 @@ export function newNativeAssetManager(win, pubUrl) {
}
}

function pbNativeDataHasValidType() {
return typeof win.pbNativeData !== 'undefined'
}

/*
* Entry point to search for placeholderes and set up postmessage roundtrip
* to retrieve native assets. Looks for placeholders for the given adId and
Expand All @@ -178,16 +180,16 @@ export function newNativeAssetManager(win, pubUrl) {
* to retrieve native assets that have a value on the corresponding bid
*/
function loadAssets(adId, cb) {
const placeholders = scanForPlaceholders(adId), flag = (typeof win.pbNativeData !== 'undefined');
const placeholders = scanForPlaceholders(adId);

if (flag && win.pbNativeData.hasOwnProperty('assetsToReplace')) {
if (pbNativeDataHasValidType() && win.pbNativeData.hasOwnProperty('assetsToReplace')) {
win.pbNativeData.assetsToReplace.forEach((asset) => {
const key = (asset.match(/hb_native_/i)) ? asset : NATIVE_KEYS[asset];
if (key) {placeholders.push(key);}
});
}

if (flag && win.pbNativeData.hasOwnProperty('requestAllAssets') && win.pbNativeData.requestAllAssets) {
if (pbNativeDataHasValidType() && win.pbNativeData.hasOwnProperty('requestAllAssets') && win.pbNativeData.requestAllAssets) {
callback = cb;
cancelMessageListener = requestAllAssets(adId);
} else if (placeholders.length > 0) {
Expand All @@ -202,11 +204,10 @@ export function newNativeAssetManager(win, pubUrl) {
function scanForPlaceholders(adId) {
let placeholders = [];
const doc = win.document;
const flag = (typeof win.pbNativeData !== 'undefined');

Object.keys(NATIVE_KEYS).forEach(key => {
const placeholderKey = NATIVE_KEYS[key];
const placeholder = (adId && !flag) ? `${placeholderKey}:${adId}` : `${placeholderKey}`;
const placeholder = (adId && !pbNativeDataHasValidType()) ? `${placeholderKey}:${adId}` : `${placeholderKey}`;
const placeholderIndex = (~doc.body.innerHTML.indexOf(placeholder)) ? doc.body.innerHTML.indexOf(placeholder) : (doc.head.innerHTML && doc.head.innerHTML.indexOf(placeholder));

if (~placeholderIndex) {
Expand Down Expand Up @@ -280,7 +281,7 @@ export function newNativeAssetManager(win, pubUrl) {
if (data.message === 'assetResponse') {
const body = win.document.body.innerHTML;
const head = win.document.head.innerHTML;
const flag = (typeof win.pbNativeData !== 'undefined');
const flag = pbNativeDataHasValidType();

if (flag && data.adId !== win.pbNativeData.adId) return;

Expand Down Expand Up @@ -333,16 +334,18 @@ export function newNativeAssetManager(win, pubUrl) {
/**
* Replaces occurrences of native placeholder values with their actual values
* in the given document.
* If there's no actual value, the placeholder gets replaced by an empty string.
*/
function replace(document, { assets, adId }) {
let html = document;

(assets || []).forEach(asset => {
const flag = (typeof win.pbNativeData !== 'undefined');
const searchString = (adId && !flag) ? `${NATIVE_KEYS[asset.key]}:${adId}` : ((flag) ? '##'+`${NATIVE_KEYS[asset.key]}`+'##' : `${NATIVE_KEYS[asset.key]}`);
scanForPlaceholders().forEach(placeholder => {
const flag = pbNativeDataHasValidType();
const searchString = (adId && !flag) ? `${placeholder}:${adId}` : ((flag) ? '##'+`${placeholder}`+'##' : `${placeholder}`);
const searchStringRegex = new RegExp(searchString, 'g');
html = html.replace(searchStringRegex, asset.value);
});
const fittingAsset = assets.find(asset => placeholder === NATIVE_KEYS[asset.key]);
html = html.replace(searchStringRegex, fittingAsset ? fittingAsset.value : '');
})

return html;
}
Expand Down