-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
171 lines (149 loc) · 5.56 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!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>
<style>
body {
--bg-color: var(--tg-theme-bg-color);
font: 12px/18px "Lucida Grande", "Lucida Sans Unicode", Arial, Helvetica, Verdana, sans-serif;
background-color: var(--bg-color);
color: var(--tg-theme-text-color);
margin: 48px 24px;
padding: 0;
color-scheme: var(--tg-color-scheme);
}
a {
color: var(--tg-theme-link-color);
}
#viewport {
position: fixed;
left: 0;
right: 0;
top: 0;
height: var(--tg-viewport-stable-height, 100vh);
pointer-events: none;
transition: all .2s ease;
}
#viewport:after {
content: '';
display: block;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
border-width: 4px;
border-style: solid;
border-image: linear-gradient(45deg, rgba(64, 224, 208, .5), rgba(173, 255, 47, .5)) 1;
}
#viewport-params-size,
#viewport-params-expand {
content: attr(text);
position: absolute;
display: inline-block;
background: var(--tg-theme-link-color, rgb(64, 224, 208));
right: 4px;
left: auto;
font-size: 8px;
padding: 4px;
vertical-align: top;
}
#viewport-params-size {
top: 4px;
}
#viewport-params-expand {
top: 30px;
}
</style>
</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>