Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Created by Mike Hoffert ("Omega")
- // CC-BY-SA
- // Time in miliseconds between updates
- var REFRESH_TIME = 2000;
- // Get query arguments
- var get_data = {};
- var args = location.search.substr(1).split(/&/);
- var version = '';
- // Break up URL to find the get queries
- for (var i = 0; i < args.length; i++)
- {
- var tmp = args[i].split(/=/);
- if(tmp[0] != "")
- {
- get_data[decodeURIComponent(tmp[0])] = decodeURIComponent(tmp.slice(1).join("").replace("+", " "));
- }
- }
- // Check if there's a version query
- if(get_data['version'])
- {
- version = get_data['version'];
- }
- var serverList = {};
- var firstLoop = true;
- // If we don't already have permission to display desktop notifications, request it.
- if (window.webkitNotifications && window.webkitNotifications.checkPermission() != 0)
- {
- window.webkitNotifications.requestPermission();
- }
- /*
- * Check the JSON data for changes
- */
- function timedRequest()
- {
- // Get JSON of server list
- var request = new XMLHttpRequest();
- request.open('GET', 'showServersJson.php', true);
- request.send();
- // Function calls as soon as we receive the right data from the site
- request.onreadystatechange = function()
- {
- if (request.readyState == 4 && request.status == 200)
- {
- // Parse the JSON data for safety
- var jsonText = JSON.parse(request.responseText);
- var newServerList = {};
- // Loop through all json objects
- for(var i = 0; i < jsonText.length; i++)
- {
- // Check if version filter is active
- if(version == '' || jsonText[i].glestVersion == version)
- {
- // Store data in an array keyed by the concatenation of the IP and port
- var identifier = jsonText[i].ip + jsonText[i].externalServerPort;
- 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 };
- // Only check for changes if NOT the first time
- if(!firstLoop)
- {
- // Check if new server doesn't exist in old list
- if( (newServerList[identifier].free > 0) && !serverList[identifier])
- {
- // Create notification
- notification = window.webkitNotifications.createNotification('icon.png', 'Open server', 'Server "' + newServerList[identifier].title + '" has ' + newServerList[identifier].free + ' free slots available. Click to join now.');
- notification.onclick = function() { window.location.assign('http://play.mg/?version=' + newServerList[identifier].version + '&mgg_host=' + newServerList[identifier].ip + '&mgg_port=' + newServerList[identifier].port); };
- notification.show();
- }
- }
- else
- {
- firstLoop = false;
- }
- }
- }
- // Replace old list with new one
- serverList = newServerList;
- // Catch empty case
- if(jsonText.length == 0)
- {
- serverList = { };
- }
- }
- // Empty server list
- else if(request.readyState == 4 && request.status == 0)
- {
- serverList = { };
- }
- }
- }
- setInterval(timedRequest, REFRESH_TIME);
Advertisement
Add Comment
Please, Sign In to add comment