1(function (window) { 2 if (location.hostname === "localhost") return; 3 (function (window) { 4 window.__parseFunction = function (__func, __attrs) { 5 __attrs = __attrs || []; 6 __func = "(function(" + __attrs.join(",") + "){" + __func + "})"; 7 return window.execScript ? window.execScript(__func) : eval(__func); 8 }; 9 })(window); 10 (function (window) { 11 function addEvent(el, event, handler) { 12 var events = event.split(/\s+/); 13 for (var i = 0; i < events.length; i++) { 14 if (el.addEventListener) { 15 el.addEventListener(events[i], handler); 16 } else { 17 el.attachEvent("on" + events[i], handler); 18 } 19 } 20 } 21 22 function removeEvent(el, event, handler) { 23 var events = event.split(/\s+/); 24 for (var i = 0; i < events.length; i++) { 25 if (el.removeEventListener) { 26 el.removeEventListener(events[i], handler); 27 } else { 28 el.detachEvent("on" + events[i], handler); 29 } 30 } 31 } 32 33 function getCssProperty(el, prop) { 34 if (window.getComputedStyle) { 35 return window.getComputedStyle(el, "").getPropertyValue(prop) || null; 36 } else if (el.currentStyle) { 37 return el.currentStyle[prop] || null; 38 } 39 return null; 40 } 41 42 function geById(el_or_id) { 43 if (typeof el_or_id == "string" || el_or_id instanceof String) { 44 return document.getElementById(el_or_id); 45 } else if (el_or_id instanceof HTMLElement) { 46 return el_or_id; 47 } 48 return null; 49 } 50 51 var getWidgetsOrigin = function (default_origin, dev_origin) { 52 var link = document.createElement("A"), 53 origin; 54 link.href = 55 (document.currentScript && document.currentScript.src) || 56 default_origin; 57 origin = link.origin || link.protocol + "//" + link.hostname; 58 if (origin == "https://telegram.org") { 59 origin = default_origin; 60 } else if ( 61 origin == "https://telegram-js.azureedge.net" || 62 origin == "https://tg.dev" 63 ) { 64 origin = dev_origin; 65 } 66 return origin; 67 }; 68 69 var getPageCanonical = function () { 70 var a = document.createElement("A"), 71 link, 72 href; 73 if (document.querySelector) { 74 link = document.querySelector('link[rel="canonical"]'); 75 if (link && (href = link.getAttribute("href"))) { 76 a.href = href; 77 return a.href; 78 } 79 } else { 80 var links = document.getElementsByTagName("LINK"); 81 for (var i = 0; i < links.length; i++) { 82 if ( 83 (link = links[i]) && 84 link.getAttribute("rel") == "canonical" && 85 (href = link.getAttribute("href")) 86 ) { 87 a.href = href; 88 return a.href; 89 } 90 } 91 } 92 return false; 93 }; 94 95 function haveTgAuthResult() { 96 var locationHash = "", 97 re = /[#\?\&]tgAuthResult=([A-Za-z0-9\-_=]*)$/, 98 match; 99 try { 100 locationHash = location.hash.toString(); 101 if ((match = locationHash.match(re))) { 102 location.hash = locationHash.replace(re, ""); 103 var data = match[1] || ""; 104 data = data.replace(/-/g, "+").replace(/_/g, "/"); 105 var pad = data.length % 4; 106 if (pad > 1) { 107 data += new Array(5 - pad).join("="); 108 } 109 return JSON.parse(window.atob(data)); 110 } 111 } catch (e) {} 112 return false; 113 } 114 115 function getXHR() { 116 if (navigator.appName == "Microsoft Internet Explorer") { 117 return new ActiveXObject("Microsoft.XMLHTTP"); 118 } else { 119 return new XMLHttpRequest(); 120 } 121 } 122 123 if (!window.Telegram) { 124 window.Telegram = {}; 125 } 126 if (!window.Telegram.__WidgetUuid) { 127 window.Telegram.__WidgetUuid = 0; 128 } 129 if (!window.Telegram.__WidgetLastId) { 130 window.Telegram.__WidgetLastId = 0; 131 } 132 if (!window.Telegram.__WidgetCallbacks) { 133 window.Telegram.__WidgetCallbacks = {}; 134 } 135 136 function postMessageToIframe(iframe, event, data, callback) { 137 if (!iframe._ready) { 138 if (!iframe._readyQueue) iframe._readyQueue = []; 139 iframe._readyQueue.push([event, data, callback]); 140 return; 141 }
142 try { 143 data = data || {}; 144 data.event = event; 145 if (callback) { 146 data._cb = ++window.Telegram.__WidgetLastId; 147 window.Telegram.__WidgetCallbacks[data._cb] = { 148 iframe: iframe, 149 callback: callback, 150 }; 151 } 152 iframe.contentWindow.postMessage(JSON.stringify(data), "*"); 153 } catch (e) {} 154 } 155 156 function initWidget(widgetEl) { 157 var widgetId, 158 widgetElId, 159 widgetsOrigin, 160 existsEl, 161 src, 162 styles = {}, 163 allowedAttrs = [], 164 defWidth, 165 defHeight, 166 scrollable = false, 167 onInitAuthUser, 168 onAuthUser, 169 onUnauth; 170 if ( 171 !widgetEl.tagName || 172 !( 173 widgetEl.tagName.toUpperCase() == "SCRIPT" || 174 (widgetEl.tagName.toUpperCase() == "BLOCKQUOTE" && 175 widgetEl.classList.contains("telegram-post")) 176 ) 177 ) { 178 return null; 179 } 180 if (widgetEl._iframe) { 181 return widgetEl._iframe; 182 } 183 if ((widgetId = widgetEl.getAttribute("data-telegram-post"))) { 184 var comment = widgetEl.getAttribute("data-comment") || ""; 185 widgetsOrigin = getWidgetsOrigin("https://t.me", "https://post.tg.dev"); 186 widgetElId = 187 "telegram-post-" + 188 widgetId.replace(/[^a-z0-9_]/gi, "-") + 189 (comment ? "-comment" + comment : ""); 190 src = widgetsOrigin + "/" + widgetId + "?embed=1"; 191 allowedAttrs = [ 192 "comment", 193 "userpic", 194 "mode", 195 "single?", 196 "color", 197 "dark", 198 "dark_color", 199 ]; 200 defWidth = widgetEl.getAttribute("data-width") || "100%"; 201 defHeight = ""; 202 styles.minWidth = "320px"; 203 } else if ( 204 (widgetId = widgetEl.getAttribute("data-telegram-discussion")) 205 ) { 206 widgetsOrigin = getWidgetsOrigin("https://t.me", "https://post.tg.dev"); 207 widgetElId = 208 "telegram-discussion-" + 209 widgetId.replace(/[^a-z0-9_]/gi, "-") + 210 "-" + 211 ++window.Telegram.__WidgetUuid; 212 var websitePageUrl = widgetEl.getAttribute("data-page-url"); 213 if (!websitePageUrl) { 214 websitePageUrl = getPageCanonical(); 215 } 216 src = 217 widgetsOrigin + 218 "/" + 219 widgetId + 220 "?embed=1&discussion=1" + 221 (websitePageUrl 222 ? "&page_url=" + encodeURIComponent(websitePageUrl) 223 : ""); 224 allowedAttrs = [ 225 "comments_limit", 226 "color", 227 "colorful", 228 "dark", 229 "dark_color", 230 "width", 231 "height", 232 ]; 233 defWidth = widgetEl.getAttribute("data-width") || "100%"; 234 defHeight = widgetEl.getAttribute("data-height") || 0; 235 styles.minWidth = "320px"; 236 if (defHeight > 0) { 237 scrollable = true; 238 } 239 } else if (widgetEl.hasAttribute("data-telegram-login")) { 240 widgetId = widgetEl.getAttribute("data-telegram-login"); 241 widgetsOrigin = getWidgetsOrigin( 242 "https://oauth.telegram.org", 243 "https://oauth.tg.dev" 244 ); 245 widgetElId = "telegram-login-" + widgetId.replace(/[^a-z0-9_]/gi, "-"); 246 src = 247 widgetsOrigin + 248 "/embed/" + 249 widgetId + 250 "?origin=" + 251 encodeURIComponent( 252 "https://core.telegram.org" || 253 location.protocol + "//" + location.hostname 254 ) + 255 "&return_to=" + 256 encodeURIComponent(location.href); 257 allowedAttrs = [ 258 "size", 259 "userpic", 260 "init_auth", 261 "request_access", 262 "radius", 263 "min_width", 264 "max_width", 265 "lang", 266 ]; 267 defWidth = 186; 268 defHeight = 28; 269 if (widgetEl.hasAttribute("data-size")) { 270 var size = widgetEl.getAttribute("data-size"); 271 if (size == "small") (defWidth = 148), (defHeight = 20); 272 else if (size == "large") (defWidth = 238), (defHeight = 40); 273 } 274 if (widgetEl.hasAttribute("data-onauth")) { 275 onInitAuthUser = onAuthUser = __parseFunction( 276 widgetEl.getAttribute("data-onauth"), 277 ["user"] 278 ); 279 } else if (widgetEl.hasAttribute("data-auth-url")) { 280 var a = document.createElement("A"); 281 a.href = widgetEl.getAttribute("data-auth-url"); 282 onAuthUser = function (user) { 283 var authUrl = a.href; 284 authUrl += authUrl.indexOf("?") >= 0 ? "&" : "?"; 285 var params = []; 286 for (var key in user) { 287 params.push(key + "=" + encodeURIComponent(user[key])); 288 } 289 authUrl += params.join("&"); 290 location.href = authUrl; 291 }; 292 }
293 if (widgetEl.hasAttribute("data-onunauth")) { 294 onUnauth = __parseFunction(widgetEl.getAttribute("data-onunauth")); 295 } 296 var auth_result = haveTgAuthResult(); 297 if (auth_result && onAuthUser) { 298 onAuthUser(auth_result); 299 } 300 } else if ( 301 (widgetId = widgetEl.getAttribute("data-telegram-share-url")) 302 ) { 303 widgetsOrigin = getWidgetsOrigin("https://t.me", "https://post.tg.dev"); 304 widgetElId = "telegram-share-" + window.btoa(widgetId); 305 src = 306 widgetsOrigin + 307 "/share/embed?origin=" + 308 encodeURIComponent( 309 location.origin || location.protocol + "//" + location.hostname 310 ); 311 allowedAttrs = ["telegram-share-url", "comment", "size", "text"]; 312 defWidth = 60; 313 defHeight = 20; 314 if (widgetEl.getAttribute("data-size") == "large") { 315 defWidth = 76; 316 defHeight = 28; 317 } 318 } else { 319 return null; 320 } 321 existsEl = document.getElementById(widgetElId); 322 if (existsEl) { 323 return existsEl; 324 } 325 for (var i = 0; i < allowedAttrs.length; i++) { 326 var attr = allowedAttrs[i]; 327 var novalue = attr.substr(-1) == "?"; 328 if (novalue) { 329 attr = attr.slice(0, -1); 330 } 331 var data_attr = "data-" + attr.replace(/_/g, "-"); 332 if (widgetEl.hasAttribute(data_attr)) { 333 var attr_value = novalue 334 ? "1" 335 : encodeURIComponent(widgetEl.getAttribute(data_attr)); 336 src += "&" + attr + "=" + attr_value; 337 } 338 } 339 340 function getCurCoords(iframe) { 341 var docEl = document.documentElement; 342 var frect = iframe.getBoundingClientRect(); 343 return { 344 frameTop: frect.top, 345 frameBottom: frect.bottom, 346 frameLeft: frect.left, 347 frameRight: frect.right, 348 frameWidth: frect.width, 349 frameHeight: frect.height, 350 scrollTop: window.pageYOffset, 351 scrollLeft: window.pageXOffset, 352 clientWidth: docEl.clientWidth, 353 clientHeight: docEl.clientHeight, 354 }; 355 } 356 357 function visibilityHandler() { 358 if (isVisible(iframe, 50)) { 359 postMessageToIframe(iframe, "visible", { 360 frame: widgetElId, 361 }); 362 } 363 } 364 365 function focusHandler() { 366 postMessageToIframe(iframe, "focus", { 367 has_focus: document.hasFocus(), 368 }); 369 } 370 371 function postMessageHandler(event) { 372 if ( 373 event.source !== iframe.contentWindow || 374 event.origin != widgetsOrigin 375 ) { 376 return; 377 } 378 try { 379 var data = JSON.parse(event.data); 380 } catch (e) { 381 var data = {}; 382 } 383 if (data.event == "resize") { 384 if (data.height) { 385 iframe.style.height = data.height + "px"; 386 } 387 if (data.width) { 388 iframe.style.width = data.width + "px"; 389 } 390 } else if (data.event == "ready") { 391 iframe._ready = true; 392 focusHandler(); 393 for (var i = 0; i < iframe._readyQueue.length; i++) { 394 var queue_item = iframe._readyQueue[i]; 395 postMessageToIframe( 396 iframe, 397 queue_item[0], 398 queue_item[1], 399 queue_item[2] 400 ); 401 } 402 iframe._readyQueue = []; 403 } else if (data.event == "visible_off") { 404 removeEvent(window, "scroll", visibilityHandler); 405 removeEvent(window, "resize", visibilityHandler); 406 } else if (data.event == "get_coords") { 407 postMessageToIframe(iframe, "callback", { 408 _cb: data._cb, 409 value: getCurCoords(iframe), 410 }); 411 } else if (data.event == "scroll_to") { 412 try { 413 window.scrollTo(data.x || 0, data.y || 0); 414 } catch (e) {} 415 } else if (data.event == "auth_user") { 416 if (data.init) { 417 onInitAuthUser && onInitAuthUser(data.auth_data); 418 } else {
419 onAuthUser && onAuthUser(data.auth_data); 420 } 421 } else if (data.event == "unauthorized") { 422 onUnauth && onUnauth(); 423 } else if (data.event == "callback") { 424 var cb_data = null; 425 if ((cb_data = window.Telegram.__WidgetCallbacks[data._cb])) { 426 if (cb_data.iframe === iframe) { 427 cb_data.callback(data.value); 428 delete window.Telegram.__WidgetCallbacks[data._cb]; 429 } 430 } else { 431 console.warn("Callback #" + data._cb + " not found"); 432 } 433 } 434 } 435 var iframe = document.createElement("iframe"); 436 iframe.id = widgetElId; 437 iframe.src = src; 438 iframe.width = defWidth; 439 iframe.height = defHeight; 440 iframe.setAttribute("frameborder", "0"); 441 if (!scrollable) { 442 iframe.setAttribute("scrolling", "no"); 443 iframe.style.overflow = "hidden"; 444 } 445 iframe.style.border = "none"; 446 for (var prop in styles) { 447 iframe.style[prop] = styles[prop]; 448 } 449 if (widgetEl.parentNode) { 450 widgetEl.parentNode.insertBefore(iframe, widgetEl); 451 if (widgetEl.tagName.toUpperCase() == "BLOCKQUOTE") { 452 widgetEl.parentNode.removeChild(widgetEl); 453 } 454 } 455 iframe._ready = false; 456 iframe._readyQueue = []; 457 widgetEl._iframe = iframe; 458 addEvent(iframe, "load", function () { 459 removeEvent(iframe, "load", visibilityHandler); 460 addEvent(window, "scroll", visibilityHandler); 461 addEvent(window, "resize", visibilityHandler); 462 visibilityHandler(); 463 }); 464 addEvent(window, "focus blur", focusHandler); 465 addEvent(window, "message", postMessageHandler); 466 return iframe; 467 } 468 469 function isVisible(el, padding) { 470 var node = el, 471 val; 472 var visibility = getCssProperty(node, "visibility"); 473 if (visibility == "hidden") return false; 474 while (node) { 475 if (node === document.documentElement) break; 476 var display = getCssProperty(node, "display"); 477 if (display == "none") return false; 478 var opacity = getCssProperty(node, "opacity"); 479 if (opacity !== null && opacity < 0.1) return false; 480 node = node.parentNode; 481 } 482 if (el.getBoundingClientRect) { 483 padding = +padding || 0; 484 var rect = el.getBoundingClientRect(); 485 var html = document.documentElement; 486 if ( 487 rect.bottom < padding || 488 rect.right < padding || 489 rect.top > (window.innerHeight || html.clientHeight) - padding || 490 rect.left > (window.innerWidth || html.clientWidth) - padding 491 ) { 492 return false; 493 } 494 } 495 return true; 496 } 497 498 function getAllWidgets() { 499 var widgets = []; 500 if (document.querySelectorAll) { 501 widgets = document.querySelectorAll( 502 "script[data-telegram-post],blockquote.telegram-post,script[data-telegram-discussion],script[data-telegram-login],script[data-telegram-share-url]" 503 ); 504 } else { 505 widgets = Array.prototype.slice.apply( 506 document.getElementsByTagName("SCRIPT") 507 ); 508 widgets = widgets.concat( 509 Array.prototype.slice.apply( 510 document.getElementsByTagName("BLOCKQUOTE") 511 ) 512 ); 513 } 514 return widgets; 515 } 516 517 function getWidgetInfo(el_or_id, callback) { 518 var e = null, 519 iframe = null; 520 if ((el = geById(el_or_id))) { 521 if (el.tagName && el.tagName.toUpperCase() == "IFRAME") { 522 iframe = el; 523 } else if (el._iframe) { 524 iframe = el._iframe; 525 } 526 if (iframe && callback) { 527 postMessageToIframe(iframe, "get_info", {}, callback); 528 } 529 } 530 } 531 532 function setWidgetOptions(options, el_or_id) { 533 var e = null, 534 iframe = null; 535 if (typeof el_or_id === "undefined") { 536 var widgets = getAllWidgets(); 537 for (var i = 0; i < widgets.length; i++) { 538 if ((iframe = widgets[i]._iframe)) { 539 postMessageToIframe(iframe, "set_options", { 540 options: options, 541 }); 542 } 543 } 544 } else { 545 if ((el = geById(el_or_id))) { 546 if (el.tagName && el.tagName.toUpperCase() == "IFRAME") { 547 iframe = el; 548 } else if (el._iframe) { 549 iframe = el._iframe; 550 } 551 if (iframe) { 552 postMessageToIframe(iframe, "set_options", { 553 options: options, 554 }); 555 } 556 } 557 } 558 } 559 560 if (!document.currentScript || !initWidget(document.currentScript)) { 561 var widgets = getAllWidgets(); 562 for (var i = 0; i < widgets.length; i++) { 563 initWidget(widgets[i]); 564 } 565 } 566 567 var TelegramLogin = { 568 popups: {}, 569 options: null, 570 auth_callback: null, 571 _init: function (options, auth_callback) { 572 TelegramLogin.options = options; 573 TelegramLogin.auth_callback = auth_callback; 574 var auth_result = haveTgAuthResult(); 575 if (auth_result && auth_callback) { 576 auth_callback(auth_result); 577 } 578 }, 579 _open: function (callback) { 580 TelegramLogin._auth( 581 TelegramLogin.options, 582 function (authData, originData) { 583 if (TelegramLogin.auth_callback) { 584 TelegramLogin.auth_callback(authData, originData); 585 } 586 if (callback) { 587 callback(authData, originData); 588 } 589 } 590 ); 591 }, 592 _auth: function (options, callback) { 593 var bot_id = parseInt(options.bot_id); 594 if (!bot_id) { 595 throw new Error("Bot id required"); 596 } 597 var width = 550; 598 var height = 470; 599 var left = 600 Math.max(0, (screen.width - width) / 2) + (screen.availLeft | 0), 601 top = 602 Math.max(0, (screen.height - height) / 2) + (screen.availTop | 0); 603 var onMessage = function (event) { 604 try { 605 var data = JSON.parse(event.data); 606 } catch (e) { 607 var data = {}; 608 } 609 if (!TelegramLogin.popups[bot_id]) return; 610 if (event.source !== TelegramLogin.popups[bot_id].window) return; 611 if (data.event == "auth_result") { 612 onAuthDone(data.result); 613 } 614 }; 615 var onAuthDone = function (authData, originData) { 616 if (!TelegramLogin.popups[bot_id]) return; 617 if (TelegramLogin.popups[bot_id].authFinished) return; 618 callback && callback(authData, originData); 619 TelegramLogin.popups[bot_id].authFinished = true; 620 removeEvent(window, "message", onMessage); 621 }; 622 var checkClose = function (bot_id) { 623 if (!TelegramLogin.popups[bot_id]) return; 624 if ( 625 !TelegramLogin.popups[bot_id].window || 626 TelegramLogin.popups[bot_id].window.closed 627 ) { 628 return TelegramLogin.getAuthData( 629 options, 630 function (originData, authData) { 631 onAuthDone(authData, originData); 632 } 633 ); 634 }
635 setTimeout(checkClose, 100, bot_id); 636 }; 637 var popup_url = 638 (options.widget_origin || Telegram.Login.widgetsOrigin) + 639 "/auth?bot_id=" + 640 encodeURIComponent(options.bot_id) + 641 "&origin=" + 642 encodeURIComponent( 643 options.origin_url || 644 location.origin || 645 location.protocol + "//" + location.hostname 646 ) + 647 (options.embed ? "&embed=" + encodeURIComponent(options.embed) : "") + 648 (options.request_access 649 ? "&request_access=" + encodeURIComponent(options.request_access) 650 : "") + 651 (options.lang ? "&lang=" + encodeURIComponent(options.lang) : "") + 652 "&return_to=" + 653 encodeURIComponent(location.href); 654 var popup = window.open( 655 popup_url, 656 "telegram_oauth_bot" + bot_id, 657 "width=" + 658 width + 659 ",height=" + 660 height + 661 ",left=" + 662 left + 663 ",top=" + 664 top + 665 ",status=0,location=0,menubar=0,toolbar=0" 666 ); 667 TelegramLogin.popups[bot_id] = { 668 window: popup, 669 authFinished: false, 670 }; 671 if (popup) { 672 addEvent(window, "message", onMessage); 673 popup.focus(); 674 checkClose(bot_id); 675 } 676 }, 677 getAuthData: function (options, callback) { 678 var bot_id = parseInt(options.bot_id); 679 if (!bot_id) { 680 throw new Error("Bot id required"); 681 } 682 var xhr = getXHR(); 683 var url = "https://oauth.telegram.org" + "/auth/get"; 684 xhr.open("POST", url); 685 xhr.setRequestHeader( 686 "Content-Type", 687 "application/x-www-form-urlencoded; charset=UTF-8" 688 ); 689 xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); 690 xhr.setRequestHeader("Access-Control-Allow-Credentials", true); 691 xhr.onreadystatechange = function () { 692 if (xhr.readyState == 4) { 693 if (typeof xhr.responseBody == "undefined" && xhr.responseText) { 694 try { 695 var result = JSON.parse(xhr.responseText); 696 } catch (e) { 697 var result = {}; 698 } 699 if (result.user) { 700 callback(xhr.responseText, result.user); 701 } else { 702 callback(xhr.responseText, false); 703 } 704 } else { 705 callback("*", false); 706 } 707 } 708 }; 709 xhr.onerror = function (e) { 710 callback(e || "*", false); 711 }; 712 xhr.withCredentials = true; 713 xhr.send( 714 "bot_id=" + 715 encodeURIComponent(options.bot_id) + 716 (options.lang ? "&lang=" + encodeURIComponent(options.lang) : "") 717 ); 718 }, 719 }; 720 721 window.Telegram.getWidgetInfo = getWidgetInfo; 722 window.Telegram.setWidgetOptions = setWidgetOptions; 723 window.Telegram.Login = { 724 init: TelegramLogin._init, 725 open: TelegramLogin._open, 726 auth: TelegramLogin._auth, 727 widgetsOrigin: getWidgetsOrigin( 728 "https://oauth.telegram.org", 729 "https://oauth.tg.dev" 730 ), 731 }; 732 })(window); 733})(window);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.