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 обёртки
|
// Telegram WebApp SDK обёртки
|
||||||
const TG = window.Telegram?.WebApp;
|
const TG = window.Telegram?.WebApp;
|
||||||
if (TG) {
|
// isAvailable — только если есть initData (значит мы внутри Telegram)
|
||||||
TG.ready();
|
const TG_AVAILABLE = !!(TG && TG.initData);
|
||||||
TG.expand();
|
|
||||||
}
|
|
||||||
|
|
||||||
window.TG_APP = {
|
window.TG_APP = {
|
||||||
initData: TG?.initData || '',
|
initData: TG?.initData || '',
|
||||||
@ -11,22 +9,31 @@ window.TG_APP = {
|
|||||||
user: TG?.initDataUnsafe?.user || null,
|
user: TG?.initDataUnsafe?.user || null,
|
||||||
colorScheme: TG?.colorScheme || 'light',
|
colorScheme: TG?.colorScheme || 'light',
|
||||||
|
|
||||||
isAvailable: !!TG,
|
isAvailable: TG_AVAILABLE,
|
||||||
|
|
||||||
close: () => TG?.close(),
|
close: () => { if (TG_AVAILABLE) TG.close(); },
|
||||||
sendData: (data) => TG?.sendData(JSON.stringify(data)),
|
sendData: (data) => { if (TG_AVAILABLE) TG.sendData(JSON.stringify(data)); },
|
||||||
haptic: (type) => {
|
haptic: (type) => {
|
||||||
|
if (!TG_AVAILABLE) return;
|
||||||
try {
|
try {
|
||||||
TG?.HapticFeedback?.impactOccurred(type || 'light');
|
TG.HapticFeedback?.impactOccurred(type || 'light');
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
},
|
},
|
||||||
showAlert: (msg) => {
|
showAlert: (msg) => {
|
||||||
if (TG?.showAlert) TG.showAlert(msg);
|
try {
|
||||||
else alert(msg);
|
if (TG_AVAILABLE && TG.showAlert) TG.showAlert(String(msg));
|
||||||
|
else alert(String(msg));
|
||||||
|
} catch (e) {
|
||||||
|
try { alert(String(msg)); } catch {}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
showConfirm: (msg) => {
|
showConfirm: (msg) => {
|
||||||
if (TG?.showConfirm) return new Promise((r) => TG.showConfirm(msg, r));
|
try {
|
||||||
return Promise.resolve(confirm(msg));
|
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 || {},
|
theme: () => TG?.themeParams || {},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user