MCPcopy
hub / github.com/ChinaGodMan/UserScripts / pollGrid

Function pollGrid

youtube-dynamic-grid/youtube-dynamic-grid.user.js:574–618  ·  view source on GitHub ↗

* Polls the grid for changes until stable. * @param {'early' | 'regular'} mode - Polling mode

(mode = 'early')

Source from the content-addressed store, hash-verified

572 * @param {'early' | 'regular'} mode - Polling mode
573 */
574 pollGrid(mode = 'early') {
575 if (isPlaylistPage()) {
576 logger(`Skipping ${mode} polling on playlist page`)
577 this.removeCSS()
578 return
579 }
580 if (this.pollInterval) clearInterval(this.pollInterval)
581 let stableCount = 0
582 const interval = CONFIG.GRID_POLL_INTERVALS[mode]
583 const duration = CONFIG.POLLING_DURATIONS[mode]
584 this.pollInterval = setInterval(() => {
585 const grid = document.querySelector(
586 'ytd-rich-grid-renderer:not([hidden])'
587 )
588 if (grid) {
589 const currentItems = getComputedStyle(grid)
590 .getPropertyValue('--ytd-rich-grid-items-per-row')
591 .trim()
592 const currentAttr =
593 grid.getAttribute('elements-per-row') || 'unknown'
594 const itemsPerRow = this.calculateItemsPerRow()
595 if (
596 currentItems !== String(itemsPerRow) ||
597 currentAttr !== String(itemsPerRow)
598 ) {
599 this.updateGrid()
600 stableCount = 0
601 } else {
602 stableCount++
603 if (stableCount >= CONFIG.STABLE_COUNT) {
604 clearInterval(this.pollInterval)
605 logger(`Stopped ${mode} polling: grid stable`)
606 if (mode === 'early') this.pollGrid('regular')
607 else this.updateGrid()
608 }
609 }
610 }
611 }, interval)
612 setTimeout(() => {
613 clearInterval(this.pollInterval)
614 logger(`Stopped ${mode} polling`)
615 if (mode === 'early') this.pollGrid('regular')
616 else this.updateGrid()
617 }, duration)
618 },
619
620 // Internal state
621 updateTimeout: null,

Callers

nothing calls this directly

Calls 2

isPlaylistPageFunction · 0.85
loggerFunction · 0.85

Tested by

no test coverage detected