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
157 lines (134 loc) · 4.06 KB
/
Copy pathstartAd.js
File metadata and controls
157 lines (134 loc) · 4.06 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import {
AdConsentCoordinator,
EMPTY_PRIVACY_STATE,
} from "./adConsentCoordinator.mjs";
import {
BANNER_SUPPRESSION_REASON,
bannerVisibilityController,
} from "./bannerVisibilityController.mjs";
import config from "./config";
export { BANNER_SUPPRESSION_REASON };
export let adUnitIdBanner = "ca-app-pub-5911839694379275/9157899592"; // Production
export let adUnitIdInterstitial = "ca-app-pub-5911839694379275/9570937608"; // Production
export let adUnitIdRewarded = "ca-app-pub-5911839694379275/1633667633"; // Production
export let initialized = false;
/** @type {import("plugins/admob/src/www").BannerAd} */
export let bannerAd = null;
/** @type {import("plugins/admob/src/www").InterstitialAd} */
export let interstitialAd = null;
let consentCoordinator = null;
export default async function startAd() {
if (!canUseAdmob()) {
if (
!config.HAS_PRO &&
typeof admob !== "undefined" &&
window.ANDROID_SDK_INT < 29
) {
console.warn("AdMob not supported on this Android version, skipping ads");
}
return;
}
try {
await getConsentCoordinator().start();
} catch (error) {
console.error("Failed to initialize ads:", error);
}
}
export function getPrivacyState() {
return consentCoordinator?.state ?? { ...EMPTY_PRIVACY_STATE };
}
/**
* Subscribes to normalized UMP state. The current state is emitted immediately.
* @param {(state:typeof EMPTY_PRIVACY_STATE)=>void} listener
* @returns {()=>void}
*/
export function subscribePrivacyState(listener) {
if (typeof listener !== "function") {
throw new TypeError("Privacy state listener must be a function.");
}
if (!canUseAdmob()) {
listener(getPrivacyState());
return () => {};
}
return getConsentCoordinator().subscribe(listener);
}
export async function showPrivacyOptions() {
if (!canUseAdmob()) return getPrivacyState();
return getConsentCoordinator().showPrivacyOptions();
}
function canUseAdmob() {
return (
!config.HAS_PRO &&
typeof admob !== "undefined" &&
window.ANDROID_SDK_INT >= 29
);
}
function getConsentCoordinator() {
consentCoordinator ??= new AdConsentCoordinator({
privacy: admob.privacy,
initializeAds,
onError: (error) =>
console.warn("Unable to refresh AdMob consent information:", error),
});
return consentCoordinator;
}
async function initializeAds() {
if (initialized) return;
if (BuildInfo.buildType === "debug") {
console.info("!!! Using test ads");
adUnitIdBanner = "ca-app-pub-3940256099942544/6300978111";
adUnitIdInterstitial = "ca-app-pub-3940256099942544/1033173712";
adUnitIdRewarded = "ca-app-pub-3940256099942544/5224354917";
}
await admob.start();
const currentHour = new Date().getHours();
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().catch((error) => {
console.warn("Failed to preload interstitial ad:", error);
});
interstitial.on("dismiss", () => {
interstitial.load().catch((error) => {
console.warn("Failed to reload interstitial ad:", error);
});
});
bannerAd = banner;
interstitialAd = interstitial;
bannerVisibilityController.setBanner(banner);
window.ad = banner;
window.iad = interstitial;
window.adRewardedUnitId = adUnitIdRewarded;
initialized = true;
}
/**
* Adds or removes one independent reason for hiding banner ads.
* @param {string} reason
* @param {boolean} suppressed
*/
export function setBannerSuppressed(reason, suppressed) {
bannerVisibilityController.setSuppressed(reason, suppressed);
}
/**
* Registers a page as eligible to display the shared banner.
* @param {HTMLElement} page
*/
export function requestBannerForPage(page) {
bannerVisibilityController.registerPage(page);
}
/**
* Temporarily suppresses the banner while the soft keyboard is visible.
* @param {boolean} visible
*/
export function setBannerKeyboardVisible(visible) {
bannerVisibilityController.setKeyboardVisible(visible);
}