()
| 387 | |
| 388 | // Initialize MutationObserver for immediate hint application |
| 389 | function initializeDOMObserver() { |
| 390 | if (hintsObserver) { |
| 391 | hintsObserver.disconnect(); |
| 392 | } |
| 393 | |
| 394 | hintsObserver = new MutationObserver((mutations) => { |
| 395 | // Process added nodes immediately |
| 396 | for (const mutation of mutations) { |
| 397 | if (mutation.type === 'childList') { |
| 398 | for (const node of mutation.addedNodes) { |
| 399 | if (node.nodeType === Node.ELEMENT_NODE) { |
| 400 | // Apply hints to the node itself |
| 401 | applyHintToElement(node); |
| 402 | |
| 403 | // Apply hints to all relevant children |
| 404 | const elements = [ |
| 405 | ...Array.from<any>(node.querySelectorAll('button')), |
| 406 | ...Array.from<any>(node.querySelectorAll('h2')), |
| 407 | ...Array.from<any>(node.querySelectorAll('label > span')), |
| 408 | ...Array.from<any>(node.querySelectorAll('.label-wrap > span')), |
| 409 | ...Array.from<any>(node.querySelectorAll('span[data-testid="block-info"]')), |
| 410 | ]; |
| 411 | |
| 412 | // Include the node itself if it matches |
| 413 | if (node.matches && ( |
| 414 | node.matches('button') |
| 415 | || node.matches('h2') |
| 416 | || node.matches('label > span') |
| 417 | || node.matches('.label-wrap > span') |
| 418 | || node.matches('span[data-testid="block-info"]') |
| 419 | )) { |
| 420 | elements.push(node); |
| 421 | } |
| 422 | |
| 423 | // Apply hints immediately to all found elements |
| 424 | elements.forEach((el) => applyHintToElement(el)); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | }); |
| 430 | |
| 431 | // Start observing the entire gradio app for changes |
| 432 | const targetNode = gradioApp(); |
| 433 | if (targetNode) { |
| 434 | hintsObserver.observe(targetNode, { |
| 435 | childList: true, |
| 436 | subtree: true, |
| 437 | }); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | export function disconnectHintsObserver() { |
| 442 | if (hintsObserver) { |
no test coverage detected