()
| 468 | }()) |
| 469 | |
| 470 | function init() { |
| 471 | // Add style to dom |
| 472 | let $styleEL = document.createElement('style'); |
| 473 | |
| 474 | if ($styleEL.styleSheet) { |
| 475 | $styleEL.styleSheet.cssText = customCSS; |
| 476 | } else { |
| 477 | $styleEL.appendChild(document.createTextNode(customCSS)); |
| 478 | } |
| 479 | gradioApp().appendChild($styleEL); |
| 480 | |
| 481 | let loaded = false |
| 482 | let _count = 0 |
| 483 | |
| 484 | const observer = new MutationObserver(mutations => { |
| 485 | if (window.localization && Object.keys(window.localization).length) return; // disabled if original translation enabled |
| 486 | if (Object.keys(opts).length === 0) return; // does nothing if opts is not loaded |
| 487 | |
| 488 | let _nodesCount = 0, _now = performance.now() |
| 489 | |
| 490 | for (const mutation of mutations) { |
| 491 | if (mutation.type === 'characterData') { |
| 492 | if (mutation.target?.parentElement?.parentElement?.tagName === 'LABEL') { |
| 493 | translateEl(mutation.target) |
| 494 | } |
| 495 | } else if (mutation.type === 'attributes') { |
| 496 | _nodesCount++ |
| 497 | translateEl(mutation.target) |
| 498 | } else { |
| 499 | mutation.addedNodes.forEach(node => { |
| 500 | if (node.className === 'bilingual__trans_wrapper') return |
| 501 | |
| 502 | _nodesCount++ |
| 503 | if (node.nodeType === 1 && /(output|gradio)-(html|markdown)/.test(node.className)) { |
| 504 | translateEl(node, { rich: true }) |
| 505 | } else if (node.nodeType === 3) { |
| 506 | doTranslate(node, node.textContent, 'text') |
| 507 | } else { |
| 508 | translateEl(node, { deep: true }) |
| 509 | } |
| 510 | }) |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | if (_nodesCount > 0) { |
| 515 | logger.info(`UI Update #${_count++}: ${performance.now() - _now} ms, ${_nodesCount} nodes`, mutations) |
| 516 | } |
| 517 | |
| 518 | if (loaded) return; |
| 519 | if (i18n) return; |
| 520 | |
| 521 | loaded = true |
| 522 | setup() |
| 523 | }) |
| 524 | |
| 525 | observer.observe(gradioApp(), { |
| 526 | characterData: true, |
| 527 | childList: true, |
nothing calls this directly
no test coverage detected