Template:Playercount: Difference between revisions
Jump to navigation
Jump to search
test for player count without modules |
Copy from intellij to media wiki, simplify to just number |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
< | <span id="playercount">...</span> | ||
<script> | |||
(function() { | |||
window.playerCountCallback = function(data) { | |||
try { | |||
var count = 0; | |||
if (typeof data === 'number') { | |||
count = data; | |||
} else if (data && typeof data === 'object') { | |||
count = data.players || data.online || data.count || (Array.isArray(data) ? data.length : 0); | |||
} | |||
document.getElementById('playercount').textContent = count; | |||
} catch (e) { | |||
console.error('Player count callback error:', e); | |||
} | |||
// Cleanup | |||
setTimeout(function() { | |||
var script = document.getElementById('playercount-script'); | |||
if (script && script.parentNode) { | |||
script.parentNode.removeChild(script); | |||
} | |||
}, 1000); | |||
}; | |||
// Error handler | |||
var handleError = function() { | |||
document.getElementById('playercount').textContent = '0'; | |||
}; | |||
// Create script tag | |||
var script = document.createElement('script'); | |||
script.id = 'playercount-script'; | |||
script.src = 'https://betamc.org:8080/api/players?callback=playerCountCallback'; | |||
script.onerror = handleError; | |||
script.onload = function() { | |||
// If callback wasn't called within 5 seconds, assume error | |||
setTimeout(function() { | |||
if (document.getElementById('playercount').textContent === '...') { | |||
handleError(); | |||
} | |||
}, 5000); | |||
}; | |||
document.head.appendChild(script); | |||
})(); | |||
</script> |
Latest revision as of 06:09, 17 June 2025
... <script> (function() {
window.playerCountCallback = function(data) { try { var count = 0; if (typeof data === 'number') { count = data; } else if (data && typeof data === 'object') { count = data.players || data.online || data.count || (Array.isArray(data) ? data.length : 0); } document.getElementById('playercount').textContent = count; } catch (e) { console.error('Player count callback error:', e); } // Cleanup setTimeout(function() { var script = document.getElementById('playercount-script'); if (script && script.parentNode) { script.parentNode.removeChild(script); } }, 1000); }; // Error handler var handleError = function() { document.getElementById('playercount').textContent = '0'; }; // Create script tag var script = document.createElement('script'); script.id = 'playercount-script'; script.src = 'https://betamc.org:8080/api/players?callback=playerCountCallback'; script.onerror = handleError; script.onload = function() { // If callback wasn't called within 5 seconds, assume error setTimeout(function() { if (document.getElementById('playercount').textContent === '...') { handleError(); } }, 5000); }; document.head.appendChild(script);
})(); </script>