-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
108 lines (94 loc) · 3.97 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Telegram Mini Apps Vanilla JS Sample App</title>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<main>
<div align="center">
<a href="https://ton.org/"><img width="48" src="./assets/tapps.png" alt="logo of telegram web apps"></a>
</div>
<h1>Modals</h1>
<button onclick="Telegram.WebApp.showAlert('Hello World!');">Launch Alert</button>
<button onclick="showPopup();">Launch Popup</button>
<h1>Links</h1>
<ul>
<li>
<a href="javascript:Telegram.WebApp.openTelegramLink('https://t.me/trendingapps');">Open link within Telegram</a>
</li>
<li>
<a href="javascript:Telegram.WebApp.openLink('https://ton.org/');">Open link in external browser</a>
</li>
<li>
<a href="javascript:Telegram.WebApp.openLink('https://telegra.ph/api',{try_instant_view:true});">Open link inside Telegram webview</a>
</li>
</ul>
<h1>Buttons</h1>
<button onclick="Telegram.WebApp.expand();">Expand Webview</button>
<button onclick="toggleMainButton();">Toggle Main Button</button>
</main>
<div id="viewport"></div>
<div id="viewport-params-size"></div>
<div id="viewport-params-expand"></div>
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<script>
// Init TWA
Telegram.WebApp.ready();
// Event occurs whenever theme settings are changed in the user's Telegram app (including switching to night mode).
Telegram.WebApp.onEvent('themeChanged', function() {
document.documentElement.className = Telegram.WebApp.colorScheme;
});
// Show main button
Telegram.WebApp.MainButton.setParams({
text: 'Main Button'
});
Telegram.WebApp.MainButton.onClick(function () {
Telegram.WebApp.showAlert('Main Button was clicked')
});
Telegram.WebApp.MainButton.show();
// Function to call showPopup API
function showPopup() {
Telegram.WebApp.showPopup({
title: 'Title',
message: 'Some message',
buttons: [
{id: 'link', type: 'default', text: 'Open ton.org'},
{type: 'cancel'},
]
}, function(btn) {
if (btn === 'link') {
Telegram.WebApp.openLink('https://ton.org/');
}
});
};
// Function to toggle main TWA button
function toggleMainButton() {
if (Telegram.WebApp.MainButton.isVisible) {
Telegram.WebApp.MainButton.hide();
} else {
Telegram.WebApp.MainButton.show();
}
};
function setViewportData() {
var sizeEl = document.getElementById('viewport-params-size');
sizeEl.innerText = 'width: ' + window.innerWidth + ' x ' +
'height: ' + Telegram.WebApp.viewportStableHeight;
var expandEl = document.querySelector('#viewport-params-expand');
expandEl.innerText = 'Is Expanded: ' + (Telegram.WebApp.isExpanded ? 'true' : 'false');
}
Telegram.WebApp.setHeaderColor('secondary_bg_color');
setViewportData();
Telegram.WebApp.onEvent('viewportChanged', setViewportData);
Telegram.WebApp.onEvent('themeChanged', function() {
document.body.setAttribute('style', '--bg-color:' + Telegram.WebApp.backgroundColor);
});
</script>
<!-- Eruda is console for mobile browsers -->
<script src="https://cdn.jsdelivr.net/npm/eruda"></script>
<script>eruda.init();</script>
</body>
</html>