Spaces:
Paused
Paused
Download static/js/telemetry.js from igardner/StockAnalysis: direct link, hf CLI and curl.
- Browser
- Download file 4.84 kB
-
https://huggingface.co/spaces/igardner/StockAnalysis/resolve/main/static/js/telemetry.js
- Command line
-
hf download hf://spaces/igardner/StockAnalysis/static/js/telemetry.js
-
curl -L -o telemetry.js https://huggingface.co/spaces/igardner/StockAnalysis/resolve/main/static/js/telemetry.js
4.84 kB
| /* Product telemetry: broad interaction coverage, no keystroke text capture. */ | |
| (function () { | |
| const page = location.pathname; | |
| const roomId = window.ROOM_ID || null; | |
| let lastMove = 0; | |
| let scrollTimer = null; | |
| const inputTimers = new WeakMap(); | |
| let eventQueue = []; | |
| let flushPending = false; | |
| const MAX_BATCH_SIZE = 50; | |
| const MAX_BUFFER_SIZE = 500; | |
| function send(eventType, extra) { | |
| const payload = Object.assign({ | |
| event_type: eventType, | |
| page: page, | |
| room_id: roomId, | |
| }, extra || {}); | |
| if (eventQueue.length >= MAX_BUFFER_SIZE) eventQueue.shift(); | |
| eventQueue.push(payload); | |
| if (eventQueue.length >= 20) flush(); | |
| } | |
| function flush() { | |
| if (flushPending || !eventQueue.length) return; | |
| const batch = eventQueue.splice(0, MAX_BATCH_SIZE); | |
| flushPending = true; | |
| fetch('/api/telemetry', { | |
| method: 'POST', | |
| credentials: 'same-origin', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ events: batch }), | |
| keepalive: true, | |
| }).then(function (response) { | |
| if (!response.ok) throw new Error('telemetry batch rejected'); | |
| }).catch(function () { | |
| eventQueue = batch.concat(eventQueue).slice(0, MAX_BUFFER_SIZE); | |
| }).finally(function () { | |
| flushPending = false; | |
| if (eventQueue.length >= 20) flush(); | |
| }); | |
| } | |
| function describe(target) { | |
| if (!target) return 'unknown'; | |
| const element = target.closest ? target.closest('a,button,input,textarea,select,summary,[role="button"]') : target; | |
| if (!element || !element.tagName) return 'unknown'; | |
| const id = element.id ? '#' + element.id : ''; | |
| const name = element.getAttribute && element.getAttribute('name'); | |
| const field = name ? '[name="' + name.slice(0, 48) + '"]' : ''; | |
| return element.tagName.toLowerCase() + id + field; | |
| } | |
| function fieldMeta(target) { | |
| return { | |
| element: describe(target), | |
| payload: { | |
| field_len: typeof target.value === 'string' ? target.value.length : 0, | |
| input_type: target.type || target.tagName.toLowerCase(), | |
| }, | |
| }; | |
| } | |
| send('pageview', { | |
| payload: { | |
| width: window.innerWidth, | |
| height: window.innerHeight, | |
| screen: screen.width + 'x' + screen.height, | |
| language: navigator.language, | |
| referrer: document.referrer || '', | |
| }, | |
| }); | |
| document.addEventListener('click', function (event) { | |
| send('click', { | |
| element: describe(event.target), | |
| coords: { x: Math.round(event.clientX), y: Math.round(event.clientY) }, | |
| }); | |
| }, true); | |
| document.addEventListener('focusin', function (event) { | |
| if (event.target && /^(INPUT|TEXTAREA|SELECT)$/.test(event.target.tagName)) send('focus', fieldMeta(event.target)); | |
| }); | |
| document.addEventListener('focusout', function (event) { | |
| if (event.target && /^(INPUT|TEXTAREA|SELECT)$/.test(event.target.tagName)) send('blur', fieldMeta(event.target)); | |
| }); | |
| document.addEventListener('input', function (event) { | |
| const target = event.target; | |
| if (!target || !/^(INPUT|TEXTAREA)$/.test(target.tagName)) return; | |
| window.clearTimeout(inputTimers.get(target)); | |
| inputTimers.set(target, window.setTimeout(function () { | |
| send('typing', fieldMeta(target)); | |
| }, 350)); | |
| }, true); | |
| document.addEventListener('keydown', function (event) { | |
| const target = event.target; | |
| const editable = target && /^(INPUT|TEXTAREA)$/.test(target.tagName); | |
| send('keypress', { | |
| element: describe(target), | |
| payload: editable ? { field_len: target.value.length } : { key: event.key }, | |
| }); | |
| }, true); | |
| document.addEventListener('change', function (event) { | |
| if (!event.target || !/^(INPUT|TEXTAREA|SELECT)$/.test(event.target.tagName)) return; | |
| send('field_change', fieldMeta(event.target)); | |
| }, true); | |
| document.addEventListener('submit', function (event) { | |
| send('form_submit', { element: describe(event.target) }); | |
| }, true); | |
| document.addEventListener('mousemove', function (event) { | |
| const now = Date.now(); | |
| if (now - lastMove < 1200) return; | |
| lastMove = now; | |
| send('mousemove', { coords: { x: Math.round(event.clientX), y: Math.round(event.clientY) } }); | |
| }, { passive: true }); | |
| document.addEventListener('scroll', function () { | |
| window.clearTimeout(scrollTimer); | |
| scrollTimer = window.setTimeout(function () { | |
| send('scroll', { payload: { y: Math.round(window.scrollY) } }); | |
| }, 600); | |
| }, { passive: true, capture: true }); | |
| document.addEventListener('visibilitychange', function () { | |
| send('visibility', { payload: { state: document.visibilityState } }); | |
| if (document.hidden) flush(); | |
| }); | |
| window.setInterval(function () { | |
| flush(); | |
| }, 5000); | |
| window.setInterval(function () { | |
| if (!document.hidden) send('heartbeat', { payload: { visible: true } }); | |
| }, 20000); | |
| })(); | |