Free javascript Hosting


crowdsalead.js

Uploaded on May 05 2022 00:38 by allen_lamb

(() => {
const baseUrl = 'https://www.thegamecrafter.com';
const target = getTarget();

processJson(baseUrl + '/api/crowdsale', json => {
const item = randomElementFrom(json.result.items);
displayAd(item, target);
});

//---------------------

function getTarget() {
if (document.currentScript) {
return document.currentScript.parentNode;
}

const scripts = document.getElementsByTagName('script');
return scripts[scripts.length - 1].parentNode;
}

function processJson(url, processor) {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
processor(JSON.parse(this.response));
}
};
xhttp.open('GET', url, true);
xhttp.send();
}

function randomElementFrom(elements) {
const randomIndex = Math.floor(Math.random() * elements.length);
return elements[randomIndex];
}

function displayAd(item, target) {
const scaledImageSize = scaleImageSize(item.ad_size, target.clientWidth, target.clientWidth);
const img = document.createElement('img');
img.src = 'https:' + item.ad_uri;
img.style.width = scaledImageSize[0];
img.style.height = scaledImageSize[1];
const a = document.createElement('a');
a.href = baseUrl + item.buy_web_uri;
a.appendChild(img);
target.appendChild(a);
}

function scaleImageSize(adSize, parentWidth, parentHeight) {
const imageSize = adSize.split('x');
const imageWidth = +imageSize[0];
const imageHeight = +imageSize[1];
const scale = Math.min(parentWidth / imageWidth, parentHeight / imageHeight);

return [Math.round(imageWidth * scale), Math.round(imageHeight * scale)];
}
})();

Back to list