$adb = "$env:LOCALAPPDATA\Android\Sdk\platform-tools\adb.exe"
$outDir = "$env:TEMP\aio_dumps"
New-Item -ItemType Directory -Force -Path $outDir | Out-Null

$allSites = New-Object 'System.Collections.Generic.HashSet[string]'
$prevSig = ""
$stableCount = 0
$maxIters = 50

for ($i = 1; $i -le $maxIters; $i++) {
    # Dump UI hierarchy
    & $adb shell uiautomator dump /sdcard/ui.xml 2>&1 | Out-Null
    & $adb pull /sdcard/ui.xml "$outDir\ui_$i.xml" 2>&1 | Out-Null

    [xml]$x = Get-Content "$outDir\ui_$i.xml" -Raw
    # Collect text nodes that look like tube names (non-empty, not generic UI labels)
    $nodes = $x.SelectNodes("//node[@text!='']")
    $skip = @("All Sites","Search site","Top Porn Sites (HD + 4K)","Gay Porn","Trans Porn","VR Porn",
              "Promo Offer","Paysites","Search","NSFWSwipe.com")
    $newOnPage = @()
    foreach ($n in $nodes) {
        $t = $n.text.Trim()
        if ($t.Length -lt 2 -or $t.Length -gt 40) { continue }
        if ($skip -contains $t) { continue }
        if ($t -match '^[\d\W]+$') { continue }  # numeric / punctuation only
        if ($allSites.Add($t)) { $newOnPage += $t }
    }

    # Compute signature for "page didn't change"
    $sig = ($nodes | ForEach-Object { $_.text }) -join '|'
    if ($sig -eq $prevSig) {
        $stableCount++
        if ($stableCount -ge 2) {
            Write-Output "iter $i — page stable, stop"
            break
        }
    } else {
        $stableCount = 0
        $prevSig = $sig
    }

    Write-Output ("iter " + $i + ": +" + $newOnPage.Count + " new, total=" + $allSites.Count)

    # Scroll down (drag top of list toward bottom = content scrolls up = next page)
    & $adb shell input swipe 540 2000 540 400 250
    Start-Sleep -Milliseconds 600
}

Write-Output ""
Write-Output "=== TOTAL UNIQUE TUBES: $($allSites.Count) ==="
$allSites | Sort-Object | ForEach-Object { Write-Output $_ }
$allSites | Sort-Object | Out-File -Encoding utf8 "$outDir\all_tubes.txt"
Write-Output ""
Write-Output "saved to: $outDir\all_tubes.txt"
