diff --git a/js/adblockManager.js b/js/adblockManager.js new file mode 100644 index 0000000..ddd7bf6 --- /dev/null +++ b/js/adblockManager.js @@ -0,0 +1,19 @@ +async function checkAdBlock() { + while(window.unityInstance === undefined) + { + await sleep(500) + } + + window.setTimeout(function () { + var adBlockEnabled = "false"; + if (window.canRunAds === undefined) { + adBlockEnabled = "true"; + } + + window.unityInstance.SendMessage("PersistantObjects", "SendAdblockData", adBlockEnabled); + }, 1000); +} + +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} diff --git a/js/ads-prebid.js b/js/ads-prebid.js new file mode 100644 index 0000000..3f4cf2e --- /dev/null +++ b/js/ads-prebid.js @@ -0,0 +1 @@ +window.canRunAds = true; // who would win in a fight? the collaborative effort of brilliant minds designing a plugin to disable all ads on a site? or one boolean boi? \ No newline at end of file diff --git a/js/adsController.js b/js/adsController.js new file mode 100644 index 0000000..ef86f83 --- /dev/null +++ b/js/adsController.js @@ -0,0 +1,153 @@ +window.adsControllerLoaded = true; +let adsVisible = false; + +if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) { + container.className = "unity-mobile"; + // Avoid draining fillrate performance on mobile devices, + // and default/override low DPI mode on mobile browsers. + config.devicePixelRatio = 1; + mobileWarning.style.display = "block"; + setTimeout(() => { + mobileWarning.style.display = "none"; + }, 5000); +} + +document.onkeydown = function (e) { + if (e.altKey || e.ctrlKey || e.key === "F1" || e.key === "F2" || e.key === "F3" || e.key === "F4") { + e.preventDefault(); + } +} + +document.onmouseup = function (e) { + e.preventDefault(); +} + +document.addEventListener('pointerlockchange', lockChangeAlert, false); +document.addEventListener('mozpointerlockchange', lockChangeAlert, false); + +let lockedOccured = false; +function lockChangeAlert() { + if (!lockedOccured && document.pointerLockElement) + lockedOccured = true; + if (!document.pointerLockElement && lockedOccured){ + lockedOccured = false; + window.unityInstance.SendMessage("Pause Menu", "OnCursorUnlocked"); + } +} + +window.addEventListener('resize', injectAdByWindowSize); + +function injectAdByWindowSize() { + if(!window.cpmstarAPI) + return; + // Inject small ad if screen is small, or large ad if screen is large + if (window.innerHeight < 900) { + if (document.getElementById("adRectangleBottom") == null) { + var el = document.getElementsByClassName("ad-rectangle-bottom")[0]; + el.id = "adRectangleBottom"; + cpmstarAPI({kind:'go',module:'POOL 83025', config: { conditions: { target: { el: el, kind: 'replace' }}}}); + } + } + else { + if (document.getElementById("adLeaderboardBottom") == null) { + var el = document.getElementsByClassName("ad-leaderboard-bottom")[0]; + el.id = "adLeaderboardBottom"; + cpmstarAPI({kind:'go',module:'POOL 85420', config: { conditions: { target: { el: el, kind: 'replace' }}}}); + } + } +} + +injectAdByWindowSize(); + +var refreshNextTime = true; + +async function showAds() { + while (window.cpmstarAPI === undefined) { + await sleep(500) + } + + document.getElementsByClassName("ad-rectangle-bottom")[0].style.display = "block"; + document.getElementsByClassName("ad-leaderboard-bottom")[0].style.display = "block"; + document.getElementsByClassName("ad-rectangle-upper")[0].style.display = "block"; + + if (typeof counter === 'undefined') { + startCounter(); + resumeCounter(); + } + else { + resumeCounter(); + refresh(); + } +} + +async function hideAds() { + while (window.cpmstarAPI === undefined) { + await sleep(500) + } + + document.getElementsByClassName("ad-rectangle-bottom")[0].style.display = "none"; + document.getElementsByClassName("ad-leaderboard-bottom")[0].style.display = "none"; + document.getElementsByClassName("ad-rectangle-upper")[0].style.display = "none"; + + pauseCounter(); +} + +function refresh() { + + //console.log("time since ads refresh = " + timeSinceRefresh + " seconds"); + //console.log("time ads visible = " + timeAdsVisible + " seconds"); + + if (timeSinceRefresh <= 30 || timeAdsVisible <= 2) { + //console.log("don't refresh"); + return; + } + + if (document.getElementById("adRectangleBottom") != null && + window.getComputedStyle(document.getElementsByClassName("ad-smallscreen")[0]).display != "none") { + cpmstarAPI({ kind: "adcmd", module: "POOL 83023", command: "refresh" }); + } + + if (document.getElementById("adLeaderboardBottom") != null && + window.getComputedStyle(document.getElementsByClassName("ad-largescreen")[0]).display != "none") { + cpmstarAPI({ kind: "adcmd", module: "POOL 85420", command: "refresh" }); + } + + cpmstarAPI({ kind: "adcmd", module: "POOL 83025", command: "refresh" }); + + timeSinceRefresh = 0; + timeAdsVisible = 0; + //console.log("refresh ads"); +} + +window.onfocus = function () { + //console.log("onfocus"); + resumeCounter(); + refresh(); +}; + +window.onblur = function () { + //console.log("onblur"); + pauseCounter(); +}; + +var timeSinceRefresh = 0; +var timeAdsVisible = 0; +var counter; + +function startCounter() { + timeSinceRefresh++; + if (adsVisible) + timeAdsVisible++; + + counter = setTimeout(function () { + startCounter(); + }, 1000); +} + +function resumeCounter() { + adsVisible = true; +} + +function pauseCounter() { + adsVisible = false; +} \ No newline at end of file