telegram-crawler/data/webappcontent.telegram.org/demo.html

393 lines
12 KiB
HTML
Raw Normal View History

2022-04-07 08:19:13 +02:00
<!DOCTYPE html>
<html>
<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 name="format-detection" content="telephone=no" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="MobileOptimized" content="176" />
<meta name="HandheldFriendly" content="True" />
<meta name="robots" content="noindex,nofollow" />
2022-04-13 17:11:59 +02:00
<script src="https://telegram.org/js/telegram-web-app.js?1"></script>
2022-04-07 08:19:13 +02:00
<script>
function setThemeClass() {
2022-04-10 00:07:30 +02:00
document.documentElement.className = Telegram.WebApp.colorScheme;
2022-04-07 08:19:13 +02:00
}
2022-04-11 03:58:11 +02:00
Telegram.WebApp.onEvent('themeChanged', setThemeClass);
2022-04-07 08:19:13 +02:00
setThemeClass();
</script>
<style>
body {
font-family: sans-serif;
background-color: var(--tg-theme-bg-color, #ffffff);
color: var(--tg-theme-text-color, #222222);
font-size: 16px;
margin: 0;
padding: 0;
2022-04-10 00:07:30 +02:00
color-scheme: var(--tg-color-scheme);
2022-04-07 08:19:13 +02:00
}
a {
color: var(--tg-theme-link-color, #2678b6);
}
button {
display: block;
width: 100%;
font-size: 14px;
margin: 15px 0;
padding: 12px 20px;
border: none;
border-radius: 4px;
background-color: var(--tg-theme-button-color, #50a8eb);
color: var(--tg-theme-button-text-color, #ffffff);
cursor: pointer;
}
button[disabled] {
opacity: 0.6;
cursor: auto;
pointer-events: none;
}
button.close_btn {
/*position: fixed;*/
position: absolute;
left: 0;
right: 0;
bottom: 0;
border-radius: 0;
margin: 0;
padding: 16px 20px;
text-transform: uppercase;
}
section {
padding: 15px 15px 65px;
text-align: center;
}
p {
margin: 40px 0 15px;
}
ul {
text-align: left;
}
li {
color: var(--tg-theme-hint-color, #a8a8a8);
}
textarea {
width: 100%;
box-sizing: border-box;
padding: 7px;
}
pre {
background: rgba(0, 0, 0, .07);
border-radius: 4px;
padding: 4px;
margin: 7px 0;
word-break: break-all;
word-break: break-word;
white-space: pre-wrap;
text-align: left;
}
.dark pre {
background: rgba(255, 255, 255, .15);
}
.hint {
font-size: .8em;
color: var(--tg-theme-hint-color, #a8a8a8);
}
.ok {
color: green;
}
.err {
color: red;
}
#fixed_wrap {
position: fixed;
left: 0;
right: 0;
top: 0;
transform: translateY(100vh);
}
2022-04-11 03:58:11 +02:00
.viewport_border,
.viewport_stable_border {
2022-04-09 21:58:54 +02:00
position: fixed;
left: 0;
right: 0;
top: 0;
height: var(--tg-viewport-height, 100vh);
pointer-events: none;
}
2022-04-11 03:58:11 +02:00
.viewport_stable_border {
height: var(--tg-viewport-stable-height, 100vh);
}
.viewport_border:before,
.viewport_stable_border:before {
2022-04-09 21:58:54 +02:00
content: attr(text);
display: inline-block;
2022-04-11 03:58:11 +02:00
position: absolute;
background: gray;
right: 0;
2022-04-09 21:58:54 +02:00
top: 0;
font-size: 7px;
padding: 2px 4px;
vertical-align: top;
}
2022-04-11 03:58:11 +02:00
.viewport_stable_border:before {
background: green;
left: 0;
right: auto;
}
.viewport_border:after,
.viewport_stable_border:after {
2022-04-09 21:58:54 +02:00
content: '';
display: block;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
2022-04-11 03:58:11 +02:00
border: 2px dashed gray;
}
.viewport_stable_border:after {
border-color: green;
2022-04-09 21:58:54 +02:00
}
2022-04-07 08:19:13 +02:00
</style>
</head>
<body style="visibility: hidden;">
<section>
<button id="main_btn" onclick="sendMessage('');">Send «Hello, World!»</button>
<button id="with_webview_btn" onclick="sendMessage('', true);">Send «Hello, World!» with inline webview button</button>
<button id="data_btn" onclick="sendTime(true);">Send current time to bot (x10)</button>
<button onclick="webviewExpand();">Expand Webview</button>
2022-04-11 03:58:11 +02:00
<button onclick="toggleMainButton(this);">Hide Main Button</button>
2022-04-07 08:19:13 +02:00
<div id="btn_status" class="hint" style="display: none;">
</div>
<p>Test links:</p>
<ul>
<li><a id="regular_link" href="?nextpage=1">Regular link #1</a> (opens inside webview)</li>
<li><a href="https://telegram.org/" target="_blank">target="_blank" link</a> (opens outside webview)</li>
<li><a href="javascript:window.open('https://telegram.org/');">window.open() link</a> (opens outside webview)</li>
<li><a href="https://t.me/like">LikeBot t.me link</a> (opens inside Telegram app)</li>
<li><a href="tg://resolve?domain=vote">VoteBot tg:// link</a> (does not open)</li>
</ul>
<p>Test permissions:</p>
<ul>
<li><a href="javascript:;" onclick="return requestLocation(this);">Request Location</a> <span></span></li>
<li><a href="javascript:;" onclick="return requestVideo(this);">Request Video</a> <span></span></li>
<li><a href="javascript:;" onclick="return requestAudio(this);">Request Audio</a> <span></span></li>
</ul>
<pre><code id="webview_data"></code></pre>
<div class="hint">
Data passed to webview.
<span id="webview_data_status" style="display: none;">Checking hash...</span>
</div>
2022-04-13 00:22:26 +02:00
<pre><code id="theme_data"></code></pre>
<div class="hint">
Theme params
</div>
2022-04-07 08:19:13 +02:00
</section>
2022-04-09 21:58:54 +02:00
<div class="viewport_border"></div>
2022-04-11 03:58:11 +02:00
<div class="viewport_stable_border"></div>
2022-04-07 08:19:13 +02:00
<script src="/js/jquery.min.js"></script>
<script>
2022-04-08 17:01:52 +02:00
Telegram.WebApp.ready();
2022-04-13 17:11:59 +02:00
var initData = Telegram.WebApp.initData || '';
var initDataUnsafe = Telegram.WebApp.initDataUnsafe || {};
2022-04-07 08:19:13 +02:00
function sendMessage(msg_id, with_webview) {
2022-04-13 17:11:59 +02:00
if (!initDataUnsafe.query_id) {
2022-04-07 08:19:13 +02:00
alert('WebViewQueryId not defined');
return;
}
$('button').prop('disabled', true);
$('#btn_status').text('Sending...').removeClass('ok err').show();
$.ajax('/demo/sendMessage', {
type: 'POST',
2022-04-13 17:11:59 +02:00
data: {_auth: initData, msg_id: msg_id || '', with_webview: !initDataUnsafe.receiver && with_webview ? 1 : 0},
2022-04-07 08:19:13 +02:00
dataType: 'json',
success: function(result) {
$('button').prop('disabled', false);
if (result.response) {
if (result.response.ok) {
$('#btn_status').html('Message sent successfully!').addClass('ok').show();
} else {
$('#btn_status').text(result.response.description).addClass('err').show();
alert(result.response.description);
}
} else {
$('#btn_status').text('Unknown error').addClass('err').show();
alert('Unknown error');
}
},
error: function(xhr) {
$('button').prop('disabled', false);
$('#btn_status').text('Server error').addClass('err').show();
alert('Server error');
}
});
}
2022-04-08 13:15:09 +02:00
function changeButton() {
$('button').prop('disabled', true);
$('#btn_status').text('Changing button...').removeClass('ok err').show();
$.ajax('/demo/changeMenuButton', {
type: 'POST',
2022-04-13 17:11:59 +02:00
data: {_auth: initData},
2022-04-08 13:15:09 +02:00
dataType: 'json',
success: function(result) {
$('button').prop('disabled', false);
if (result.response) {
if (result.response.ok) {
$('#btn_status').html('Button changed!').addClass('ok').show();
2022-04-08 18:45:32 +02:00
Telegram.WebApp.close();
2022-04-08 13:15:09 +02:00
} else {
$('#btn_status').text(result.response.description).addClass('err').show();
alert(result.response.description);
}
} else {
$('#btn_status').text('Unknown error').addClass('err').show();
alert('Unknown error');
}
},
error: function(xhr) {
$('button').prop('disabled', false);
$('#btn_status').text('Server error').addClass('err').show();
alert('Server error');
}
});
2022-04-08 18:45:32 +02:00
setTimeout(function() {
Telegram.WebApp.close();
}, 50);
2022-04-08 13:15:09 +02:00
}
2022-04-07 08:19:13 +02:00
function byteLength(str) {
if (window.Blob) {
try { return new Blob([str]).size; } catch (e) {}
}
var s = str.length;
for (var i=str.length-1; i>=0; i--) {
var code = str.charCodeAt(i);
if (code > 0x7f && code <= 0x7ff) s++;
else if (code > 0x7ff && code <= 0xffff) s+=2;
if (code >= 0xDC00 && code <= 0xDFFF) i--;
}
return s;
}
function sendText(spam) {
var text = $('#text_field').val();
if (!text.length) {
return $('#text_field').focus();
}
if (byteLength(text) > 4096) {
return alert('Text is too long');
}
var repeat = spam ? 10 : 1;
for (var i = 0; i < repeat; i++) {
Telegram.WebApp.sendData(text);
}
}
function sendTime(spam) {
var repeat = spam ? 10 : 1;
for (var i = 0; i < repeat; i++) {
Telegram.WebApp.sendData(new Date().toString());
}
}
function webviewExpand() {
Telegram.WebApp.expand();
}
function webviewClose() {
Telegram.WebApp.close();
}
function requestLocation(el) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
$(el).next('span').html('(' + position.coords.latitude + ', ' + position.coords.longitude + ')').attr('class', 'ok');
});
} else {
$(el).next('span').html('Geolocation is not supported in this browser.').attr('class', 'err');
}
return false;
}
function requestVideo(el) {
if (navigator.mediaDevices) {
navigator.mediaDevices.getUserMedia({ audio: false, video: true }).then(function(stream) {
$(el).next('span').html('(Access granted)').attr('class', 'ok');
});
} else {
$(el).next('span').html('Media devices is not supported in this browser.').attr('class', 'err');
}
return false;
}
function requestAudio(el) {
if (navigator.mediaDevices) {
navigator.mediaDevices.getUserMedia({ audio: true, video: false }).then(function(stream) {
$(el).next('span').html('(Access granted)').attr('class', 'ok');
});
} else {
$(el).next('span').html('Media devices is not supported in this browser.').attr('class', 'err');
}
return false;
}
2022-04-13 00:22:26 +02:00
Telegram.WebApp.onEvent('themeChanged', function() {
$('#theme_data').html(JSON.stringify(Telegram.WebApp.themeParams, null, 2));
});
2022-04-07 08:19:13 +02:00
2022-04-13 17:11:59 +02:00
$('#main_btn').toggle(!!initDataUnsafe.query_id);
$('#with_webview_btn').toggle(!!initDataUnsafe.query_id && !initDataUnsafe.receiver);
// $('#data_btn').toggle(!initDataUnsafe.query_id || !initDataUnsafe.receiver);
$('#webview_data').html(JSON.stringify(initDataUnsafe, null, 2));
2022-04-13 00:22:26 +02:00
$('#theme_data').html(JSON.stringify(Telegram.WebApp.themeParams, null, 2));
2022-04-07 08:19:13 +02:00
$('#regular_link').attr('href', $('#regular_link').attr('href') + location.hash);
$('#text_field').focus();
2022-04-13 17:11:59 +02:00
if (initDataUnsafe.query_id && initData) {
2022-04-07 08:19:13 +02:00
$('#webview_data_status').show();
$.ajax('/demo/checkData', {
type: 'POST',
2022-04-13 17:11:59 +02:00
data: {_auth: initData},
2022-04-07 08:19:13 +02:00
dataType: 'json',
success: function(result) {
if (result.ok) {
$('#webview_data_status').html('Hash is correct').addClass('ok');
} else {
$('#webview_data_status').html(result.error).addClass('err');
}
},
error: function(xhr) {
$('#webview_data_status').html('Server error').addClass('err');
}
});
}
$('body').css('visibility', '');
Telegram.WebApp.MainButton
.setText('CLOSE WEBVIEW')
.show()
.onClick(function(){ webviewClose(); });
2022-04-11 03:58:11 +02:00
function toggleMainButton(el) {
var mainButton = Telegram.WebApp.MainButton;
if (mainButton.isVisible) {
mainButton.hide();
el.innerHTML = 'Show Main Button';
} else {
mainButton.show();
el.innerHTML = 'Hide Main Button';
}
}
function round(val, d) {
var k = Math.pow(10, d || 0);
return Math.round(val * k) / k;
}
function setViewportData() {
$('.viewport_border').attr('text', window.innerWidth + ' x ' + round(Telegram.WebApp.viewportHeight, 2));
$('.viewport_stable_border').attr('text', window.innerWidth + ' x ' + round(Telegram.WebApp.viewportStableHeight, 2) + ' | is_expanded: ' + (Telegram.WebApp.isExpanded ? 'true' : 'false'));
2022-04-09 21:58:54 +02:00
}
2022-04-11 03:58:11 +02:00
Telegram.WebApp.onEvent('viewportChanged', setViewportData);
setViewportData();
2022-04-09 21:58:54 +02:00
2022-04-07 08:19:13 +02:00
</script>
</body>
</html>