From 937ce8c7b1cf080a28e932afcc2ce123bd0312ea Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 26 Apr 2022 15:42:18 +0000 Subject: [PATCH] Update content of files --- data/telegram.org/js/telegram-web-app.js | 70 +++++++++++++++++++++++ data/webappcontent.telegram.org/cafe.html | 2 +- data/webappcontent.telegram.org/demo.html | 2 +- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/data/telegram.org/js/telegram-web-app.js b/data/telegram.org/js/telegram-web-app.js index 0524751376..6074846332 100644 --- a/data/telegram.org/js/telegram-web-app.js +++ b/data/telegram.org/js/telegram-web-app.js @@ -224,6 +224,7 @@ } window.Telegram.WebView = { initParams: initParams, + isIframe: isIframe, onEvent: onEvent, offEvent: offEvent, postEvent: postEvent, @@ -252,10 +253,12 @@ var Utils = window.Telegram.Utils; var WebView = window.Telegram.WebView; var initParams = WebView.initParams; + var isIframe = WebView.isIframe; var WebApp = {}; var webAppInitData = '', webAppInitDataUnsafe = {}; var themeParams = {}, colorScheme = 'light'; + var webAppVersion = '1.0'; if (initParams.tgWebAppData && initParams.tgWebAppData.length) { webAppInitData = initParams.tgWebAppData; @@ -277,6 +280,9 @@ setThemeParams(theme_params); } catch (e) {} } + if (initParams.tgWebAppVersion) { + webAppVersion = initParams.tgWebAppVersion; + } function onThemeChanged(eventType, eventData) { if (eventData.theme_params) { @@ -305,6 +311,20 @@ } } + function linkHandler(e) { + if (e.metaKey || e.ctrlKey) return; + var el = e.target; + while (el.tagName != 'A' && el.parentNode) { + el = el.parentNode; + } + if (el.tagName == 'A' && + el.target != '_blank' && + (el.protocol == 'http:' || el.protocol == 'https:') && + el.hostname == 't.me') { + WebApp.openTgLink(el.href); + } + } + function receiveWebViewEvent(eventType) { var args = Array.prototype.slice.call(arguments); eventType = args.shift(); @@ -395,6 +415,26 @@ return hsp < 120; } + function versionCompare(v1, v2) { + if (typeof v1 !== 'string') v1 = ''; + if (typeof v2 !== 'string') v2 = ''; + v1 = v1.replace(/^\s+|\s+$/g, '').split('.'); + v2 = v2.replace(/^\s+|\s+$/g, '').split('.'); + var a = Math.max(v1.length, v2.length), i, p1, p2; + for (i = 0; i < a; i++) { + p1 = parseInt(v1[i]) || 0; + p2 = parseInt(v2[i]) || 0; + if (p1 == p2) continue; + if (p1 > p2) return 1; + return -1; + } + return 0; + } + + function versionAtLeast(ver) { + return versionCompare(webAppVersion, ver) >= 0; + } + function byteLength(str) { if (window.Blob) { try { return new Blob([str]).size; } catch (e) {} @@ -652,6 +692,10 @@ get: function(){ return webAppInitDataUnsafe; }, enumerable: true }); + Object.defineProperty(WebApp, 'version', { + get: function(){ return webAppVersion; }, + enumerable: true + }); Object.defineProperty(WebApp, 'colorScheme', { get: function(){ return colorScheme; }, enumerable: true @@ -676,6 +720,9 @@ value: MainButton, enumerable: true }); + WebApp.isVersionAtLeast = function(ver) { + return versionAtLeast(ver); + }; WebApp.onEvent = function(eventType, callback) { onWebViewEvent(eventType, callback); }; @@ -692,6 +739,26 @@ } WebView.postEvent('web_app_data_send', false, {data: data}); }; + WebApp.openTgLink = function (url) { + var a = document.createElement('A'); + a.href = url; + if (a.protocol != 'http:' && + a.protocol != 'https:') { + console.error('[Telegram.WebApp] Url protocol is not supported', url); + throw Error('WebAppTgUrlInvalid'); + } + if (a.hostname != 't.me') { + console.error('[Telegram.WebApp] Url host is not supported', url); + throw Error('WebAppTgUrlInvalid'); + } + var path_full = a.pathname + a.search; + if (WebApp.isIframe || + versionAtLeast('1.1')) { + WebView.postEvent('open_tg_link', false, {path_full: path_full}); + } else { + location.href = 'https://t.me' + path_full; + } + }; WebApp.ready = function () { WebView.postEvent('web_app_ready'); }; @@ -707,6 +774,9 @@ setViewportHeight(); window.addEventListener('resize', onWindowResize); + if (isIframe) { + document.addEventListener('click', linkHandler); + } WebView.onEvent('theme_changed', onThemeChanged); WebView.onEvent('viewport_changed', onViewportChanged); diff --git a/data/webappcontent.telegram.org/cafe.html b/data/webappcontent.telegram.org/cafe.html index a5b4246eaf..2f7a6f06b5 100644 --- a/data/webappcontent.telegram.org/cafe.html +++ b/data/webappcontent.telegram.org/cafe.html @@ -9,7 +9,7 @@ - + +