Advertisement
gjnt1m

internetconnection - v2

Mar 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var InternetConnection = function() {
  2.  
  3.     var isRunning = false;
  4.    
  5.     this.connected = true;
  6.  
  7.     var isShowedDialog = false;
  8.  
  9.     //init popup
  10.     var initPopup = setInterval(function() {
  11.         if (document.body) {
  12.             clearInterval(initPopup);
  13.  
  14.             var popup_style = document.createElement('style');
  15.             popup_style.innerHTML = '#no_internet_popup { z-index: 10000001; top: 0%; width: 100%; height: 100%; -webkit-transform: translateZ(0); font-size: 8px; background-color: rgba(0,0,0,0.5) } ' +
  16.                             '#no_internet_popup { position: fixed; left: 0; right: 0; bottom: 0; text-align: center } ' +
  17.                             '#no_internet_popup > div { display: inline-block; vertical-align: middle } ' +
  18.                             '#no_internet_popup .info_cont { margin-top: -8em; top: 50% } ' +
  19.                             '#no_internet_popup { -webkit-transform: translateZ(0); position: absolute; left: 0; color: #000; z-index: 9999 } ' +
  20.                             '#no_internet_popup .info_cont { width: 36em; background: rgba(255, 255, 255, 0.9); text-align: center; position: relative } ' +
  21.                             '#no_internet_popup .title { font-weight: bold; padding: 1em 1em 0 1em } ' +
  22.                             '#no_internet_popup .title span { font-size: 3em } ' +
  23.                             '#no_internet_popup .body { padding: 1em } ' +
  24.                             '#no_internet_popup .body span { font-size: 2em } ' +
  25.                             '#no_internet_popup .actions { color: #60A5F2 } ' +
  26.                             '#no_internet_popup .actions div { padding: 1em; width: 15.9em } ' +
  27.                             '#no_internet_popup .actions span { font-size: 2.5em } ' +
  28.                             '.popup_yes { float: left; font-weight: bold; border-top: 1px solid #DCDCDC; border-right: 1px solid #DCDCDC } ' +
  29.                             '.popup_no { float: left; font-weight: bold; border-top: 1px solid #DCDCDC }';
  30.             document.head.appendChild(popup_style);
  31.            
  32.             var no_internet_popup = document.createElement('div');
  33.             no_internet_popup.id = 'no_internet_popup';
  34.             no_internet_popup.style.cssText = 'display: none;';
  35.             no_internet_popup.innerHTML = '<div class="info_cont radius5">\
  36.                     <div class="title" id="no_internet_popup_header"><span>No internet connection</span></div>\
  37.                     <div class="body" id="no_internet_popup_body"><span>Please check your internet connection</span></div>\
  38.                     <div class="actions">\
  39.                             <div id="cancel_yes" class="popup_yes" onclick="no_internet_popup_close();" tabindex="0">\
  40.                                     <span>Close</span>\
  41.                             </div>\
  42.                             <div id="cancel_no" class="popup_no" onclick="no_internet_popup_resume();" tabindex="0">\
  43.                                     <span>Retry</span>\
  44.                             </div>\
  45.                             <div class="clear"></div>\
  46.                     </div>\
  47.                 </div>';
  48.             document.body.appendChild(no_internet_popup);
  49.         }
  50.     }, 500);
  51.    
  52.     function showDialog(dialog) {
  53.         isShowedDialog = true;
  54.         var ele = document.getElementById(dialog);
  55.         ele.style.display = '';
  56.     }
  57.  
  58.     function hideDialog(dialog) {
  59.         isShowedDialog = false;
  60.         var ele = document.getElementById(dialog);
  61.         ele.style.display = 'none';
  62.     }
  63.  
  64.     function checkConnection() {
  65.         if (!isShowedDialog)
  66.             window.InternetConnection.check();
  67.     }
  68.  
  69.     window.no_internet_popup_close = function () {
  70.         send_tracking(tracking_actions.ads_close);
  71.     }
  72.  
  73.     window.no_internet_popup_resume = function() {
  74.         hideDialog('no_internet_popup');
  75.     }
  76.    
  77.     this.canConnectInternet = function() {
  78.         return window.InternetConnection.connected;
  79.     }
  80.  
  81.     this.check = function() {
  82.         if(isRunning) return;
  83.        
  84.         var request = new XMLHttpRequest();
  85.         request.open("GET", resource.get_src('asset/ping.txt'), true);
  86.         request.timeout = 10000; //10sec
  87.         request.overrideMimeType('text/plain; charset=x-user-defined');
  88.        
  89.         request.onload = function(){
  90.             window.InternetConnection.connected = request.response.startsWith('OK');
  91.             isRunning = false;
  92.         };
  93.        
  94.         request.onerror = function(){
  95.             window.InternetConnection.connected = false;
  96.             showDialog('no_internet_popup');
  97.             isRunning = false;
  98.         };
  99.        
  100.         request.ontimeout = function(){
  101.             window.InternetConnection.connected = false;
  102.             showDialog('no_internet_popup');
  103.             isRunning = false;
  104.         };
  105.        
  106.         isRunning = true;
  107.         request.send();
  108.     }
  109. }
  110.  
  111. var InternetConnection = new InternetConnection();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement