daily pastebin goal
49%
SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(window) {
  2.   (function(window){
  3.     window.__parseFunction = function(__func, __attrs) {
  4.       __attrs = __attrs || [];
  5.       __func = '(function(' + __attrs.join(',') + '){' + __func + '})';
  6.       return window.execScript ? window.execScript(__func) : eval(__func);
  7.     }
  8.   }(window));
  9.   (function(window){
  10.  
  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.   function removeEvent(el, event, handler) {
  22.     var events = event.split(/\s+/);
  23.     for (var i = 0; i < events.length; i++) {
  24.       if (el.removeEventListener) {
  25.         el.removeEventListener(events[i], handler);
  26.       } else {
  27.         el.detachEvent('on' + events[i], handler);
  28.       }
  29.     }
  30.   }
  31.   function getCssProperty(el, prop) {
  32.     if (window.getComputedStyle) {
  33.       return window.getComputedStyle(el, '').getPropertyValue(prop) || null;
  34.     } else if (el.currentStyle) {
  35.       return el.currentStyle[prop] || null;
  36.     }
  37.     return null;
  38.   }
  39.  
  40.   var getWidgetsOrigin = function(default_origin) {
  41.     var link = document.createElement('A');
  42.     link.href = document.currentScript && document.currentScript.src || 'https://t.me';
  43.     var origin = link.origin || link.protocol + '//' + link.hostname;
  44.     if (origin == 'https://telegram.org') {
  45.       origin = default_origin;
  46.     }
  47.     return origin;
  48.   };
  49.  
  50.   function initWidget(widgetEl) {
  51.     var widgetId, widgetElId, widgetsOrigin, existsEl,
  52.         src, styles = {}, allowedAttrs = [],
  53.         defWidth, defHeight, onInitAuthUser, onAuthUser, onUnauth;
  54.     if (!widgetEl.tagName ||
  55.         !(widgetEl.tagName.toUpperCase() == 'SCRIPT' ||
  56.           widgetEl.tagName.toUpperCase() == 'BLOCKQUOTE' &&
  57.           widgetEl.classList.contains('telegram-post'))) {
  58.       return null;
  59.     }
  60.     if (widgetId = widgetEl.getAttribute('data-telegram-post')) {
  61.       widgetsOrigin = getWidgetsOrigin('https://t.me');
  62.       widgetElId = 'telegram-post-' + widgetId.replace(/[^a-z0-9_]/ig, '-');
  63.       src = widgetsOrigin + '/' + widgetId + '?embed=1';
  64.       allowedAttrs = ['userpic', 'single?'];
  65.       defWidth = widgetEl.getAttribute('data-width') || '100%';
  66.       defHeight = '';
  67.       styles.minWidth = '320px';
  68.     }
  69.     else if (widgetEl.hasAttribute('data-telegram-login')) {
  70.       widgetId = widgetEl.getAttribute('data-telegram-login');
  71.       widgetsOrigin = getWidgetsOrigin('https://oauth.telegram.org');
  72.       widgetElId = 'telegram-login-' + widgetId.replace(/[^a-z0-9_]/ig, '-');
  73.       src = widgetsOrigin + '/embed/' + widgetId + '?origin=' + encodeURIComponent(location.origin || location.protocol + '//' + location.hostname);
  74.       allowedAttrs = ['size', 'userpic', 'init_auth', 'request_access', 'radius', 'min_width', 'max_width', 'lang'];
  75.       defWidth = 186;
  76.       defHeight = 28;
  77.       if (widgetEl.hasAttribute('data-size')) {
  78.         var size = widgetEl.getAttribute('data-size');
  79.         if (size == 'small') defWidth = 148, defHeight = 20;
  80.         else if (size == 'large') defWidth = 238, defHeight = 40;
  81.       }
  82.       if (widgetEl.hasAttribute('data-onauth')) {
  83.         onInitAuthUser = onAuthUser = __parseFunction(widgetEl.getAttribute('data-onauth'), ['user']);
  84.       }
  85.       else if (widgetEl.hasAttribute('data-auth-url')) {
  86.         var a = document.createElement('A');
  87.         a.href = widgetEl.getAttribute('data-auth-url');
  88.         onAuthUser = function(user) {
  89.           var authUrl = a.href;
  90.           authUrl += (authUrl.indexOf('?') >= 0) ? '&' : '?';
  91.           var params = [];
  92.           for (var key in user) {
  93.             params.push(key + '=' + encodeURIComponent(user[key]));
  94.           }
  95.           authUrl += params.join('&');
  96.           location.href = authUrl;
  97.         };
  98.       }
  99.       if (widgetEl.hasAttribute('data-onunauth')) {
  100.         onUnauth = __parseFunction(widgetEl.getAttribute('data-onunauth'));
  101.       }
  102.     }
  103.     else {
  104.       return null;
  105.     }
  106.     existsEl = document.getElementById(widgetElId);
  107.     if (existsEl) {
  108.       return existsEl;
  109.     }
  110.     for (var i = 0; i < allowedAttrs.length; i++) {
  111.       var attr = allowedAttrs[i];
  112.       var novalue = attr.substr(-1) == '?';
  113.       if (novalue) {
  114.         attr = attr.slice(0, -1);
  115.       }
  116.       var data_attr = 'data-' + attr.replace(/_/g, '-');
  117.       if (widgetEl.hasAttribute(data_attr)) {
  118.         var attr_value = novalue ? '1' : encodeURIComponent(widgetEl.getAttribute(data_attr));
  119.         src += '&' + attr + '=' + attr_value;
  120.       }
  121.     }
  122.     function visibilityHandler() {
  123.       try {
  124.         if (isVisible(iframe, 50)) {
  125.           var data = {event: 'visible', frame: widgetElId};
  126.           iframe.contentWindow.postMessage(JSON.stringify(data), '*');
  127.           // console.log('send', data);
  128.         }
  129.       } catch(e) {}
  130.     }
  131.     function postMessageHandler(event) {
  132.       if (event.source !== iframe.contentWindow ||
  133.           event.origin != widgetsOrigin) {
  134.         return;
  135.       }
  136.       try {
  137.         var data = JSON.parse(event.data);
  138.       } catch(e) {
  139.         var data = {};
  140.       }
  141.       if (data.event == 'resize') {
  142.         if (data.height) {
  143.           iframe.style.height = data.height + 'px';
  144.         }
  145.         if (data.width) {
  146.           iframe.style.width = data.width + 'px';
  147.         }
  148.       }
  149.       else if (data.event == 'visible_off') {
  150.         removeEvent(window, 'scroll', visibilityHandler);
  151.         removeEvent(window, 'resize', visibilityHandler);
  152.       }
  153.       else if (data.event == 'auth_user') {
  154.         if (data.init) {
  155.           onInitAuthUser && onInitAuthUser(data.auth_data);
  156.         } else {
  157.           onAuthUser && onAuthUser(data.auth_data);
  158.         }
  159.       }
  160.       else if (data.event == 'unauthorized') {
  161.         onUnauth && onUnauth();
  162.       }
  163.     }
  164.     var iframe = document.createElement('iframe');
  165.     iframe.id = widgetElId;
  166.     iframe.src = src;
  167.     iframe.width = defWidth;
  168.     iframe.height = defHeight;
  169.     iframe.setAttribute('frameborder', '0');
  170.     iframe.setAttribute('scrolling', 'no');
  171.     iframe.style.border = 'none';
  172.     iframe.style.overflow = 'hidden';
  173.     for (var prop in styles) {
  174.       iframe.style[prop] = styles[prop];
  175.     }
  176.     if (widgetEl.parentNode) {
  177.       widgetEl.parentNode.insertBefore(iframe, widgetEl);
  178.       if (widgetEl.tagName.toUpperCase() == 'BLOCKQUOTE') {
  179.         widgetEl.parentNode.removeChild(widgetEl);
  180.       }
  181.     }
  182.     addEvent(iframe, 'load', function() {
  183.       removeEvent(iframe, 'load', visibilityHandler);
  184.       addEvent(window, 'scroll', visibilityHandler);
  185.       addEvent(window, 'resize', visibilityHandler);
  186.       visibilityHandler();
  187.     });
  188.     addEvent(window, 'message', postMessageHandler);
  189.     return iframe;
  190.   }
  191.   function isVisible(el, padding) {
  192.     var node = el, val;
  193.     var visibility = getCssProperty(node, 'visibility');
  194.     if (visibility == 'hidden') return false;
  195.     while (node) {
  196.       if (node === document.documentElement) break;
  197.       var display = getCssProperty(node, 'display');
  198.       if (display == 'none') return false;
  199.       var opacity = getCssProperty(node, 'opacity');
  200.       if (opacity !== null && opacity < 0.1) return false;
  201.       node = node.parentNode;
  202.     }
  203.     if (el.getBoundingClientRect) {
  204.       padding = +padding || 0;
  205.       var rect = el.getBoundingClientRect();
  206.       var html = document.documentElement;
  207.       if (rect.bottom < padding ||
  208.           rect.right < padding ||
  209.           rect.top > (window.innerHeight || html.clientHeight) - padding ||
  210.           rect.left > (window.innerWidth || html.clientWidth) - padding) {
  211.         return false;
  212.       }
  213.     }
  214.     return true;
  215.   }
  216.   if (!document.currentScript ||
  217.       !initWidget(document.currentScript)) {
  218.     var widgets;
  219.     if (document.querySelectorAll) {
  220.       widgets = document.querySelectorAll('script[data-telegram-post],blockquote.telegram-post,script[data-telegram-login]');
  221.     } else {
  222.       widgets = Array.prototype.slice.apply(document.getElementsByTagName('SCRIPT'));
  223.       widgets = widgets.concat(Array.prototype.slice.apply(document.getElementsByTagName('BLOCKQUOTE')));
  224.     }
  225.     for (var i = 0; i < widgets.length; i++) {
  226.       initWidget(widgets[i]);
  227.     }
  228.   }
  229.  
  230.   }(window));
  231. })(window);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top