Guest User

desktopNotifications.js

a guest
Jul 30th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Created by Mike Hoffert ("Omega")
  2. // CC-BY-SA
  3.  
  4. // Time in miliseconds between updates
  5. var REFRESH_TIME = 2000;
  6.  
  7. // Get query arguments
  8. var get_data = {};
  9. var args = location.search.substr(1).split(/&/);
  10. var version = '';
  11.  
  12. // Break up URL to find the get queries
  13. for (var i = 0; i < args.length; i++)
  14. {
  15.     var tmp = args[i].split(/=/);
  16.     if(tmp[0] != "")
  17.     {
  18.         get_data[decodeURIComponent(tmp[0])] = decodeURIComponent(tmp.slice(1).join("").replace("+", " "));
  19.     }
  20. }
  21.  
  22. // Check if there's a version query
  23. if(get_data['version'])
  24. {
  25.     version = get_data['version'];
  26. }
  27.  
  28. var serverList = {};
  29. var firstLoop = true;
  30.  
  31. // If we don't already have permission to display desktop notifications, request it.
  32. if (window.webkitNotifications && window.webkitNotifications.checkPermission() != 0)
  33. {
  34.     window.webkitNotifications.requestPermission();
  35. }
  36.  
  37. /*
  38.  * Check the JSON data for changes
  39.  */
  40. function timedRequest()
  41. {
  42.     // Get JSON of server list
  43.     var request = new XMLHttpRequest();
  44.     request.open('GET', 'showServersJson.php', true);
  45.     request.send();
  46.  
  47.     // Function calls as soon as we receive the right data from the site
  48.     request.onreadystatechange = function()
  49.     {
  50.         if (request.readyState == 4 && request.status == 200)
  51.         {
  52.             // Parse the JSON data for safety
  53.             var jsonText = JSON.parse(request.responseText);
  54.  
  55.             var newServerList = {};
  56.  
  57.             // Loop through all json objects
  58.             for(var i = 0; i < jsonText.length; i++)
  59.             {
  60.                 // Check if version filter is active
  61.                 if(version == '' || jsonText[i].glestVersion == version)
  62.                 {
  63.                     // Store data in an array keyed by the concatenation of the IP and port
  64.                     var identifier = jsonText[i].ip + jsonText[i].externalServerPort;
  65.                     newServerList[identifier] = { 'ip': jsonText[i].ip, 'port': jsonText[i].externalServerPort, 'title': jsonText[i].serverTitle, 'free': (jsonText[i].networkSlots - jsonText[i].connectedClients), 'version': jsonText[i].glestVersion };
  66.  
  67.                     // Only check for changes if NOT the first time
  68.                     if(!firstLoop)
  69.                     {
  70.                         // Check if new server doesn't exist in old list
  71.                         if( (newServerList[identifier].free > 0) && !serverList[identifier])
  72.                         {
  73.                             // Create notification
  74.                             notification = window.webkitNotifications.createNotification('icon.png', 'Open server', 'Server "' + newServerList[identifier].title + '" has ' + newServerList[identifier].free + ' free slots available. Click to join now.');
  75.                             notification.onclick = function() { window.location.assign('http://play.mg/?version=' + newServerList[identifier].version + '&mgg_host=' + newServerList[identifier].ip + '&mgg_port=' + newServerList[identifier].port); };
  76.                             notification.show();
  77.                         }
  78.                     }
  79.                     else
  80.                     {
  81.                         firstLoop = false;
  82.                     }
  83.                 }
  84.             }
  85.             // Replace old list with new one
  86.             serverList = newServerList;
  87.  
  88.             // Catch empty case
  89.             if(jsonText.length == 0)
  90.             {
  91.                 serverList = { };
  92.             }
  93.         }
  94.         // Empty server list
  95.         else if(request.readyState == 4 && request.status == 0)
  96.         {
  97.             serverList = { };
  98.         }
  99.     }
  100. }
  101.  
  102. setInterval(timedRequest, REFRESH_TIME);
Advertisement
Add Comment
Please, Sign In to add comment