Fix TG WebApp: isAvailable только если есть initData
Раньше isAvailable=true если window.Telegram.WebApp объект существовал (SDK грузится всегда). Но методы кидали WebAppMethodUnsupported вне Telegram. Теперь: isAvailable=true только если есть initData (мы внутри Telegram) Все методы (close, sendData, haptic, showAlert, showConfirm) — early return если не внутри Telegram.
This commit is contained in:
parent
d44eb2be8d
commit
2af273ffbe
@ -1,9 +1,7 @@
|
||||
// Telegram WebApp SDK обёртки
|
||||
const TG = window.Telegram?.WebApp;
|
||||
if (TG) {
|
||||
TG.ready();
|
||||
TG.expand();
|
||||
}
|
||||
// isAvailable — только если есть initData (значит мы внутри Telegram)
|
||||
const TG_AVAILABLE = !!(TG && TG.initData);
|
||||
|
||||
window.TG_APP = {
|
||||
initData: TG?.initData || '',
|
||||
@ -11,22 +9,31 @@ window.TG_APP = {
|
||||
user: TG?.initDataUnsafe?.user || null,
|
||||
colorScheme: TG?.colorScheme || 'light',
|
||||
|
||||
isAvailable: !!TG,
|
||||
isAvailable: TG_AVAILABLE,
|
||||
|
||||
close: () => TG?.close(),
|
||||
sendData: (data) => TG?.sendData(JSON.stringify(data)),
|
||||
close: () => { if (TG_AVAILABLE) TG.close(); },
|
||||
sendData: (data) => { if (TG_AVAILABLE) TG.sendData(JSON.stringify(data)); },
|
||||
haptic: (type) => {
|
||||
if (!TG_AVAILABLE) return;
|
||||
try {
|
||||
TG?.HapticFeedback?.impactOccurred(type || 'light');
|
||||
TG.HapticFeedback?.impactOccurred(type || 'light');
|
||||
} catch (e) {}
|
||||
},
|
||||
showAlert: (msg) => {
|
||||
if (TG?.showAlert) TG.showAlert(msg);
|
||||
else alert(msg);
|
||||
try {
|
||||
if (TG_AVAILABLE && TG.showAlert) TG.showAlert(String(msg));
|
||||
else alert(String(msg));
|
||||
} catch (e) {
|
||||
try { alert(String(msg)); } catch {}
|
||||
}
|
||||
},
|
||||
showConfirm: (msg) => {
|
||||
if (TG?.showConfirm) return new Promise((r) => TG.showConfirm(msg, r));
|
||||
return Promise.resolve(confirm(msg));
|
||||
try {
|
||||
if (TG_AVAILABLE && TG.showConfirm) return new Promise((r) => TG.showConfirm(msg, r));
|
||||
return Promise.resolve(confirm(String(msg)));
|
||||
} catch (e) {
|
||||
try { return Promise.resolve(confirm(String(msg))); } catch { return Promise.resolve(false); }
|
||||
}
|
||||
},
|
||||
|
||||
theme: () => TG?.themeParams || {},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user