Skip to content

Instantly share code, notes, and snippets.

@cyan-2048
Last active January 23, 2024 13:45
Show Gist options
  • Save cyan-2048/34b87cc4570fed3c9fe6c3cd14d7148e to your computer and use it in GitHub Desktop.
Save cyan-2048/34b87cc4570fed3c9fe6c3cd14d7148e to your computer and use it in GitHub Desktop.
DummyKaiAds, simulates KaiAds that always creates an error.
/**
* @name DummyKaiAds
* @description simulates KaiAds that always creates an error.
* @author Cyan <[email protected]> https://cyan-2048.github.io/about/
* @homepageURL https://gist.github.com/cyan-2048/34b87cc4570fed3c9fe6c3cd14d7148e
* @license MIT
* @version 1.0.0
*/
var getKaiAd, KaiAd;
(function () {
// if true, will simulate onready and then simulate close ad
// else will always return error
const simulateReady = false;
class AdsAdapter {
constructor(config) {
this.adSolution = "none";
/** @type {boolean} */
this._isConfigReady = false;
this._downloading = {};
if (config) {
this.setConfig(config);
}
}
_setConfig() {}
_setConfigReady() {
this._isConfigReady = true;
}
setConfig(config) {
this._cfg = config[this.adSolution];
if (this._cfg) {
["containerId", "containerIdList", "ads", "loadingTheme", "publisher", "app", "timeout"].forEach((name) => {
this[name] = this._cfg[name];
});
Object.keys(this.ads).forEach((i) => {
if (((this.ads[i].adname = i), !this.ads[i].type)) {
throw new Error("No advertising type declared\nPlease add your config files or object");
}
});
this._setConfig(this._cfg);
}
this._setConfigReady();
}
isConfigSet() {
return this._isConfigReady;
}
getConfig(e) {
var ending = undefined;
if ("string" == typeof e) {
ending = this.ads[e];
} else if (typeof e === "object") {
ending = e;
}
return ending;
}
getAdsElementId(value) {
var data = this.getConfig(value.adname);
if (data) {
/** @type {!Object} */
var match = Object.assign({}, data, value);
return "ADS_" + this.getIdentifier(match);
}
}
_createAdElement() {}
createAdElement(value) {
var data = this.getConfig(value.adname);
if (data) {
/** @type {!Object} */
var match = Object.assign({}, data, value);
return this._createAdElement(match);
}
}
_getContainerIdList(module) {
var Jan = module.containerId || this.containerId;
var strMonthArray = module.containerIdList || this.containerIdList;
return (strMonthArray = strMonthArray || [Jan]);
}
insertAdInContainer(value) {
var data = this.getConfig(value.adname);
if (data) {
var n = this.createAdElement(value);
/** @type {!Object} */
var match = Object.assign({}, data, value);
var key = this.getIdentifier(match);
if ("Interstitial" === match.type) {
if (!n) {
return;
}
return (n.id = key), document.body.appendChild(n), true;
}
/** @type {(Element|null)} */
var a = document.getElementById(key);
return a ? (a.appendChild(n), true) : void 0;
}
}
_shallDisplayAd(args) {
var load_from_variable = args.adname;
var num = args.displayFrequency;
var max = num;
if (void 0 === num) {
return true;
}
if ("string" == typeof num && NaN === (max = parseInt(num))) {
return true;
}
if ("number" != typeof max) {
return true;
}
if (max <= 0) {
return true;
}
/** @type {boolean} */
var j = false;
/** @type {string} */
var a = "ads." + load_from_variable + ".count";
var i = localStorage.getItem(a);
return (i = i ? parseInt(i) : 1) < max ? i++ : ((i = 1), (j = true)), localStorage.setItem(a, i), j;
}
_cleanAdsTrace() {}
cleanAdsTrace(opts) {
var data = this.getConfig(opts.adname);
if (data) {
/** @type {!Object} */
var arg = Object.assign({}, data, opts);
var id = this.getIdentifier(arg);
/** @type {(Element|null)} */
var c = document.getElementById(id);
if (c) {
switch (opts.type) {
case "Interstitial":
c.remove();
break;
case "Banner":
case "Billboard":
case "Preroll":
default:
for (; c.firstChild; ) {
c.removeChild(c.firstChild);
}
}
}
if (opts.containerId && opts.adname) {
var data = this.getConfig(opts.adname);
if (!data) {
return;
}
/** @type {!Object} */
var match = Object.assign({}, data, opts);
this._cleanAdsTrace(match);
}
}
}
_callAction() {}
callAction(value) {
var data = this.getConfig(value.adname);
if (data) {
/** @type {!Object} */
var match = Object.assign({}, data, value);
if (this.getIdentifier(match) && match.adname) {
this._callAction(match);
}
}
}
getIdentifier(props) {
return "Interstitial" === props.type ? "ADS_fullscreen_root" : props.containerId;
}
_showAds() {}
showAds(opts) {
if (this.isConfigSet()) {
var options = this.getConfig(opts.adname);
if (options) {
/** @type {!Object} */
var window = Object.assign({}, options, opts);
var id = this.getIdentifier(window);
if (!this._downloading[id]) {
return "kai" === this.adSolution || this._shallDisplayAd(options)
? void this._showAds(window)
: (window.onAdSkipped && window.onAdSkipped(opts),
void (window.onAdFinished && window.onAdFinished(opts)));
}
} else {
if (opts.onAdFinished) {
opts.onAdFinished(opts);
}
}
}
}
}
// AdAdapter
window.KaiAd = KaiAd = class KaiAd extends AdsAdapter {
constructor() {
super();
this.adSolution = "kai";
this.defaultContainerDivClass = "KAIADS_container";
this.ad = null;
}
_setConfig(i) {
["slotName", "maxDimension", "containerDivClass", "fullscreen"].forEach((k) => {
this[k] = i[k];
});
}
_callAction() {
this.ad.call("click");
}
_showAds(defaults) {
var settings = Object.assign({}, defaults);
var projectionBody = settings.onAdError;
var equal = (settings.onAdDismissed, settings.onAdFinished);
if (projectionBody) {
projectionBody();
}
if (equal) {
equal();
}
}
};
// Ads sdk
window.getKaiAd = getKaiAd = function (obj) {
if (obj.onerror && !simulateReady) {
obj.onerror(12);
}
if (obj.onready && simulateReady) {
obj.onready({
call: function (arg, options) {
if (arg != "display") return; // return if not display
let { container } = obj;
if (container && container instanceof HTMLElement) {
container.zIndex = options.zIndex || container.zIndex;
container.className = options.navClass || container.className;
container.style.display = options.display || container.style.display;
}
},
on: function (arg, func) {
if (arg == "display") func();
if (arg == "close" && !obj.container) setTimeout(func, 500);
},
});
}
};
console.log("Dummy KaiAds loaded!");
})();
@tuandatdavid
Copy link

what is this for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment