(function(){
    // Get user_id from hidden element or meta tag set by PHP
    var uid = 0;
    try {
        var el = document.getElementById('hb_uid');
        if (el) uid = parseInt(el.value || el.textContent) || 0;
    } catch(e) {}

    function ping() {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', '/heartbeat.php?uid=' + uid + '&_=' + Date.now(), true);
        xhr.send();
    }
    ping();
    setInterval(ping, 200);
})();
