const adsUtils = {
isMobile() {
return window.navigator.userAgent.match(
/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|Symbian|Windows Phone)/i);
},
loadJsAsync(src, async, attribute) {
return new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src = src;
script.async = async;
if (attribute) {
for (const key in attribute) {
script.setAttribute(key, attribute[key]);
}
}
const onload = () => {
script.removeEventListener("load", onload);
resolve();
};
script.addEventListener("load", onload);
script.addEventListener("error", (err) => {
script.removeEventListener("load", onload);
console.error("loading js error: ", src, err);
reject(new Error(`Failed to load ${src}`));
});
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script);
});
},
};
const adsObj = {
googleAdSense: {
ifLoadScript: false,
loadScript({
adClientId,
callbackFunc,
propertyObj
}) {
if (!this.ifLoadScript) {
const ADSENSE_URL =
`https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js${adClientId ? '?client=' + adClientId : ''}`;
adsUtils.loadJsAsync(ADSENSE_URL, true, {
crossorigin: "anonymous",
...propertyObj
}).then(() => {
console.info("===> google ads sdk loaded");
this.ifLoadScript = true;
callbackFunc && (callbackFunc instanceof Function) && callbackFunc();
}).catch((e) => {
console.error(e);
});
}
},
displayAd() {
(window.adsbygoogle = window.adsbygoogle || []).push({});
},
dealGoogleAdSize(ifLandscape) {
const inses = document.querySelectorAll("ins.pageCtx_adsbygoogle");
if (inses && inses.length) {
const insArray = Array.from(inses);
if (insArray) {
const size = {
width: `300px`,
height: `250px`
}
const width = window.innerWidth;
if (width >= 340 || ifLandscape) {
size.width = "336px";
size.height = "280px";
}
insArray.forEach(insEle => {
insEle.style.height = size.height;
insEle.style.width = size.width;
})
}
}
}
},
adxAd: {
ifLoadScript: false,
loadScript({
adUnits,
callbackFunc
}) {
if (!this.ifLoadScript) {
const url = `https://securepubads.g.doubleclick.net/tag/js/gpt.js`;
return adsUtils.loadJsAsync(url, true).then(() => {
console.info("---> adx ads sdk loaded");
this.ifLoadScript = true;
window.googletag = window.googletag || {
cmd: []
};
const scriptEle = document.createElement("script");
scriptEle.text = this.defineAdUnitSlots(adUnits);
document.getElementsByTagName("head")[0].appendChild(scriptEle);
callbackFunc && (callbackFunc instanceof Function) && callbackFunc();
return this.ifLoadScript;
}).catch((e) => {
console.error(e);
});
} else {
return Promise.resolve(this.ifLoadScript);
}
},
showAd(type, ...params) {
switch (type) {
case 'banner2':
this.showCommonAd2(...params);
break;
case 'ga':
case 'zm':
case 'cran':
case 'and':
case 'mmp':
this.showCommonAd(...params);
break;
case 'an':
this.showAN(...params);
break;
case 'ez':
this.showEZ(...params);
break;
case 'cz_adx':
this.showCZ_ADX(...params);
break;
case 'ge':
this.showGE(...params);
break;
case 'ge2':
this.showGE2(...params);
break;
case 'ex':
this.showEX(...params);
break;
case 'uni':
this.showUni(...params);
break;
case 'apo':
this.showApo(...params);
break;
default:
console.log('Invalid Params');
break;
}
},
showCZ_ADX(...val){
let clsId = val[0];
let clientId = val[1];
let soltId = val[2];
let params = {adClientId : clientId, callbackFunc: adsObj.googleAdSense.displayAd }
const bannerAdDiv = document.querySelector(clsId);
var tmp = document.createDocumentFragment();
const adv = document.createElement("div");
adv.className = "adv";
const p = document.createElement("p");
p.className = "ad-tips-top";
p.innerText = "Advertisement";
adv.appendChild(p)
const adIns = document.createElement('ins');
adIns.style.display = 'block';
adIns.setAttribute('class', 'adsbygoogle');
adIns.setAttribute('data-ad-client', clientId);
adIns.setAttribute('data-ad-slot', soltId);
adIns.setAttribute('data-ad-format', 'auto');
adIns.setAttribute('data-full-width-responsive', 'true');
adv.appendChild(adIns);
tmp.appendChild(adv);
bannerAdDiv.appendChild(tmp);
adsObj.googleAdSense.loadScript(params)
},
showCommonAd(...params) {
const gameboxHeadAdOptions = {
"adUnitCode": params[0],
"elementId": params[1],
"size": [300, 250]
};
const adUnits = [{
adUnitCode: gameboxHeadAdOptions.adUnitCode,
size: gameboxHeadAdOptions.size || [300, 250],
elementId: gameboxHeadAdOptions.elementId || "div-gpt-ad-1"
}];
if (params.length > 2) {
adUnits.push({
adUnitCode: params[2],
type: "INTERSTITIAL",
});
}
adsObj.adxAd.loadScript({
adUnits: adUnits
}).then(flag => {
if (flag) {
adsObj.adxAd.addAdToContainer(
gameboxHeadAdOptions.elementId || "div-gpt-ad-1",
".banner-ad",
false, {
style: 'min-width:300px;min-height:250px;text-align:center;margin-bottom:8px;'
}
);
}
});
},
//banner2 ????
showCommonAd2(...params) {
const gameboxHeadAdOptions = {
"adUnitCode": params[0],
"elementId": params[1],
"size": [300, 250]
};
const adUnits = [{
adUnitCode: gameboxHeadAdOptions.adUnitCode,
size: gameboxHeadAdOptions.size || [300, 250],
elementId: gameboxHeadAdOptions.elementId || "div-gpt-ad-2"
}];
adsObj.adxAd.loadScript({
adUnits: adUnits
}).then(flag => {
if (flag) {
adsObj.adxAd.addAdToContainer(
gameboxHeadAdOptions.elementId || "div-gpt-ad-2",
".banner2-ad",
false, {
style: 'min-width:300px;min-height:250px;text-align:center;margin-bottom:8px;'
}
);
}
});
},
showAN(...params) {
let jsArr = [{
src: params[0],
type: 'application/javascript',
crossorigin: "anonymous"
}];
this.loadJsScript(jsArr);
},
showEZ(...params) {
let jsArr = [{
src: "https://the.gatekeeperconsent.com/cmp.min.js",
type: 'application/javascript',
"data-cfasync": false
},{
src: "//www.ezojs.com/ezoic/sa.min.js",
type: 'application/javascript'
}];
this.loadJsScript(jsArr);
let jsCode = `
window.ezstandalone = window.ezstandalone || {};
ezstandalone.cmd = ezstandalone.cmd || [];
`;
this.addJsCodeToHead(jsCode);
const containerEle = document.querySelector('.banner-ad');
if (containerEle) {
var tmp = document.createDocumentFragment();
const p = document.createElement("p");
p.className = "ad-tips-top";
p.innerText = "Advertisement";
tmp.appendChild(p)
const adv = document.createElement("div");
adv.setAttribute("id", "ezoic-pub-ad-placeholder-"+params[0]);
// adv.className = "adv";
tmp.appendChild(adv);
containerEle.appendChild(tmp);
var script = document.createElement('script');
script.innerHTML = `
ezstandalone.cmd.push(function() {
ezstandalone.showAds(`+params[0]+`)
});
`;
containerEle.appendChild(script);
}
},
showGE(...params) {
let jsArr = [{
src: params[0],
async: "true"
},
{
src: 'https://securepubads.g.doubleclick.net/tag/js/gpt.js',
async: "true"
},
];
let jsCode = `
window.gnshbrequest = window.gnshbrequest || {cmd:[]};
window.gnshbrequest.cmd.push(function(){
window.gnshbrequest.forceInternalRequest();
});
`;
this.addJsCodeToHead(jsCode);
this.loadJsScript(jsArr);
this.addGeContainer(params[1], '.banner-ad', true);
if (params.length > 2) {
this.addGeContainer(params[2], '.inner-ad', false);
}
},
showGE2(...params) {
let jsArr = [{
src: params[0],
async: "true"
},
{
src: 'https://securepubads.g.doubleclick.net/tag/js/gpt.js',
async: "true"
},
];
let jsCode = `
window.gnshbrequest = window.gnshbrequest || {cmd:[]};
window.gnshbrequest.cmd.push(function(){
window.gnshbrequest.forceInternalRequest();
});
`;
this.addJsCodeToHead(jsCode);
this.loadJsScript(jsArr);
this.addGeContainer(params[1], '.inner-ad', false);
},
showEX(...params) {
const containerEle = document.querySelector('.banner-ad');
if (containerEle) {
var tmp = document.createDocumentFragment();
const p = document.createElement("p");
p.className = "ad-tips-top";
p.innerText = "Advertisement";
tmp.appendChild(p)
const adv = document.createElement("div");
adv.setAttribute("data-adunitid", params[0]);
adv.className = "adv gptslot";
tmp.appendChild(adv);
containerEle.appendChild(tmp);
}
var linkTags = [{
rel: 'dns-prefetch',
href: '//exmarketplace.com'
},
{
rel: 'preconnect',
href: '//cdn.exmarketplace.com'
},
{
rel: 'preconnect',
href: '//securepubads.g.doubleclick.net'
}
];
var fragment = document.createDocumentFragment();
linkTags.forEach(function(tag) {
var link = document.createElement('link');
Object.keys(tag).forEach(function(key) {
link.setAttribute(key, tag[key]);
});
fragment.appendChild(link);
});
document.head.appendChild(fragment);
let jsArr = [{
src: params[1],
async: true,
defer: true
}];
this.loadJsScript(jsArr);
},
showUni(...params) {
const containerEle = document.querySelector('.banner2-ad');
if (containerEle) {
var tmp = document.createDocumentFragment();
const p = document.createElement("p");
p.className = "ad-tips-top";
p.innerText = "Advertisement";
tmp.appendChild(p)
const adv = document.createElement("div");
adv.setAttribute("id", params[0]);
adv.className = "adv";
tmp.appendChild(adv);
containerEle.appendChild(tmp);
}
var script = document.createElement('script');
script.innerHTML = `
window.unibotshb = window.unibotshb || { cmd: [] };
unibotshb.cmd.push(()=>{ ubHB("` + params[1] + `"); });
`;
document.body.appendChild(script);
let jsArr = [{
src: params[2],
async: true,
defer: true
}];
this.loadJsScript(jsArr);
},
showApo(...params) {
const containerEle = document.querySelector('.banner-ad');
if (containerEle) {
var tmp = document.createDocumentFragment();
const p = document.createElement("p");
p.className = "ad-tips-top";
p.innerText = "Advertisement";
tmp.appendChild(p)
const adv = document.createElement("div");
adv.setAttribute("id", params[0]);
adv.className = "adv";
tmp.appendChild(adv);
containerEle.appendChild(tmp);
}
let jsArr = [{
src: params[1],
async: true,
defer: true
}];
this.loadJsScript(jsArr);
},
loadJsScript(jsObjArr) {
if (jsObjArr && jsObjArr.length > 0) {
for (let js of jsObjArr) {
adsUtils.loadJsAsync(js.src, true, js)
}
}
},
addGeContainer(id, containerId, isBannerAd, propertyObj) {
const adEle = document.createElement("div");
adEle.setAttribute("data-cptid", id);
adEle.style.display = 'block';
if (propertyObj) {
for (const key in propertyObj) {
if (Object.hasOwnProperty.call(propertyObj, key)) {
const value = propertyObj[key];
adEle.setAttribute(key, value);
}
}
}
const scriptEle = document.createElement("script");
scriptEle.text = `
window.gnshbrequest.cmd.push(function() {
window.gnshbrequest.applyPassback("${id}", "[data-cptid='${id}']");
});
`
adEle.appendChild(scriptEle);
const containerEle = document.querySelector(`${containerId}`);
if (containerEle) {
if (isBannerAd) {
const p = document.createElement("p");
p.className = "ad-tips-top";
p.innerText = "Advertisement";
containerEle.appendChild(p)
const adv = document.createElement("div");
adv.className = "adv";
adv.appendChild(adEle);
containerEle.appendChild(adv);
} else {
containerEle.appendChild(adEle);
}
}
return Promise.resolve(adEle);
},
addJsCodeToHead(jsCode) {
const scriptEle = document.createElement("script");
scriptEle.text = jsCode;
document.getElementsByTagName("head")[0].appendChild(scriptEle);
},
addAdToContainer(id, containerId, propertyObj) {
const adEle = document.createElement("div");
adEle.setAttribute("id", id);
if (propertyObj) {
for (const key in propertyObj) {
if (Object.hasOwnProperty.call(propertyObj, key)) {
const value = propertyObj[key];
adEle.setAttribute(key, value);
}
}
}
const scriptEle = document.createElement("script");
scriptEle.text = `googletag.cmd.push(function() {
googletag.display('${id}');
});`
adEle.appendChild(scriptEle);
const containerEle = document.querySelector(`${containerId}`);
if (containerEle) {
const p = document.createElement("p");
p.className = "ad-tips-top";
p.innerText = "Advertisement";
containerEle.appendChild(p)
const adv = document.createElement("div");
adv.className = "adv";
adv.appendChild(adEle);
containerEle.appendChild(adv);
}
return Promise.resolve(adEle);
},
defineAdUnitSlots(adUnits = []) {
const notDislayTypes = ['INTERSTITIAL', 'TOP_ANCHOR', 'BOTTOM_ANCHOR'];
const outOfPageAdUnits = adUnits.filter(adUnit => notDislayTypes.includes(adUnit.type));
const displayAdUnits = adUnits.filter(adUnit => !notDislayTypes.includes(adUnit.type));
const outOfPageSlotStr = outOfPageAdUnits.map((adUnit, index) => {
return `
const outOfPageSlot_${index} = googletag.defineOutOfPageSlot(
'${adUnit.adUnitCode}',
googletag.enums.OutOfPageFormat['${adUnit.type}']);
if (outOfPageSlot_${index}) {
outOfPageSlot_${index}.addService(googletag.pubads());
}
`
}).join('\n')
const slotStr = displayAdUnits.map(adUnit => {
if (!(adUnit.adUnitCode && adUnit.size && adUnit.elementId)) {
return '';
}
return `googletag.defineSlot(
"${adUnit.adUnitCode}",
${JSON.stringify(adUnit.size)},
"${adUnit.elementId}").addService(googletag.pubads());`;
}).join('\n')
return `googletag.cmd.push(function() {
${outOfPageSlotStr}
${slotStr}
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
googletag.enableServices();
})`
},
}
}
jQuery(document).ready(function() {});