forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartAd.js
More file actions
76 lines (62 loc) · 1.92 KB
/
startAd.js
File metadata and controls
76 lines (62 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
let adUnitIdBanner = "ca-app-pub-5911839694379275/9157899592"; // Production
let adUnitIdInterstitial = "ca-app-pub-5911839694379275/9570937608"; // Production
let adUnitIdRewarded = "ca-app-pub-5911839694379275/1633667633"; // Production
let initialized = false;
export default async function startAd() {
if (!IS_FREE_VERSION || !admob) return;
if (!initialized) {
initialized = true;
if (BuildInfo.type === "debug") {
adUnitIdBanner = "ca-app-pub-3940256099942544/6300978111"; // Test
adUnitIdInterstitial = "ca-app-pub-3940256099942544/1033173712"; // Test
adUnitIdRewarded = "ca-app-pub-3940256099942544/5224354917"; // Test
}
}
const consentStatus = await consent.getConsentStatus();
if (consentStatus === consent.ConsentStatus.Required) {
await consent.requestInfoUpdate();
}
const formStatus = await consent.getFormStatus();
if (formStatus === consent.FormStatus.Available) {
const form = await consent.loadForm();
form.show();
}
await admob.start();
const currentHour = new Date().getHours();
//currentHour >= 22: Covers 10:00 PM to 11:59 PM.
//currentHour < 4: Covers 12:00 AM to 3:59 AM.
const isQuietHours = currentHour >= 22 || currentHour < 4;
await admob.configure({
appMuted: isQuietHours,
appVolume: isQuietHours ? 0.0 : 1.0,
});
const banner = new admob.BannerAd({
adUnitId: adUnitIdBanner,
position: "bottom",
});
const interstitial = new admob.InterstitialAd({
adUnitId: adUnitIdInterstitial,
});
interstitial.load();
interstitial.on("dismiss", () => {
interstitial.load();
});
window.ad = banner;
window.iad = interstitial;
window.adRewardedUnitId = adUnitIdRewarded;
}
/**
* Hides the ad
* @param {Boolean} [force=false]
*/
export function hideAd(force = false) {
const { ad } = window;
if (ad?.active) {
const $pages = tag.getAll(".page-replacement");
const hide = $pages.length === 1;
if (force || hide) {
ad.active = false;
ad.hide();
}
}
}