lerobot-grader / usage.js
kalanosdev's picture
Measure anonymous Space activity and successful card copies
4ee7c6a
Raw History Blame Contribute Delete
2.11 kB
// Executed inside Gradio's load hook. No cookies, IDs, dataset names or card content
// leave the page. Clipboard text is used locally and never sent as telemetry.
if (!window.kalanosUsage) {
const enabled = location.origin === 'https://kalanosdev-lerobot-grader.hf.space';
const emit = (event) => {
if (!enabled) return;
fetch('https://hub.kalanos.ai/api/events/hf-space', {
method: 'POST', credentials: 'omit', referrerPolicy: 'no-referrer',
headers: {'Content-Type': 'text/plain'}, body: JSON.stringify({event}), keepalive: true
}).catch(() => {});
};
const copy = async (text, button, status) => {
if (!text) return;
try {
await navigator.clipboard.writeText(text);
emit('hf_card_copied');
if (status) status.textContent = 'Copied';
if (button) { button.title = 'Copied'; button.setAttribute('aria-label', 'Copied'); }
} catch {
if (status) status.textContent = 'Clipboard unavailable. Select the Markdown and copy manually.';
if (button) { button.title = 'Copy unavailable — select the Markdown and copy manually'; button.setAttribute('aria-label', button.title); }
}
};
window.kalanosUsage = {emit, copy};
emit('hf_space_viewed');
document.addEventListener('toggle', (e) => {
if (e.target.matches?.('details.k-hf-card') && e.target.open) emit('hf_card_previewed');
}, true);
document.addEventListener('click', (e) => {
const button = e.target.closest?.('.k-copy-card');
if (button) {
const panel = button.closest('.k-hf-card');
copy(panel.querySelector('textarea').value, button, panel.querySelector('.k-copy-status'));
}
const link = e.target.closest?.('.k-card a[href]');
if (link) {
const url = new URL(link.href);
if (url.origin === 'https://hub.kalanos.ai' && url.pathname.startsWith('/d/')) emit('hf_report_opened');
}
});
document.addEventListener('copy', (e) => {
const input = e.target.closest?.('.k-hf-card textarea');
if (input && input.selectionStart !== input.selectionEnd && !e.defaultPrevented) emit('hf_card_copied');
});
}