param( [string]$Package = "com.jihuanshe", [int]$DelayMs = 12000, [string]$Adb = "D:\platform-tools\adb.exe", [string]$Frida = "C:\Python\Python312\Scripts\frida.exe", [string]$Hook = "", [switch]$NoRestart, [switch]$Foreground ) # Usage: # .\start_auto_dismiss.ps1 # restart App -> wait gadget -> attach dismiss hook (background) # .\start_auto_dismiss.ps1 -NoRestart # App already running, attach directly # .\start_auto_dismiss.ps1 -Foreground # foreground mode, show live log # # Stop: .\stop_auto_dismiss.ps1 $ErrorActionPreference = "Stop" $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $StateFile = Join-Path $ScriptDir "auto_dismiss_bg_state.json" $StdoutLog = Join-Path $ScriptDir "auto_dismiss_stdout.log" $StderrLog = Join-Path $ScriptDir "auto_dismiss_stderr.log" if ([string]::IsNullOrWhiteSpace($Hook)) { $Hook = Join-Path $ScriptDir "auto_dismiss_update.js" } if (-not (Test-Path $Adb)) { throw "adb not found: $Adb" } if (-not (Test-Path $Frida)) { throw "frida not found: $Frida" } if (-not (Test-Path $Hook)) { throw "hook script not found: $Hook" } # kill previous background frida if any if (Test-Path $StateFile) { try { $oldState = Get-Content $StateFile -Raw | ConvertFrom-Json if ($oldState.pid) { $oldProc = Get-Process -Id ([int]$oldState.pid) -ErrorAction SilentlyContinue if ($oldProc) { Write-Host "[cleanup] stopping previous frida pid=$($oldState.pid)" Stop-Process -Id ([int]$oldState.pid) -Force Start-Sleep -Milliseconds 500 } } } catch { Write-Host "[cleanup] ignore invalid state file" } } if (-not $NoRestart) { Write-Host "[1/4] force-stop $Package" & $Adb shell "su -c 'am force-stop $Package'" | Out-Host Start-Sleep -Milliseconds 800 Write-Host "[2/4] launch $Package" & $Adb shell "su -c 'monkey -p $Package -c android.intent.category.LAUNCHER 1'" | Out-Host Write-Host "[3/4] wait $DelayMs ms for ZygiskFrida gadget injection" Start-Sleep -Milliseconds $DelayMs } else { Write-Host "[1/4] skip restart" Write-Host "[2/4] skip launch" Write-Host "[3/4] skip wait" } if (Test-Path $StdoutLog) { Remove-Item $StdoutLog -Force } if (Test-Path $StderrLog) { Remove-Item $StderrLog -Force } $argList = @( "-U", "-N", $Package, "-l", $Hook, "-q" ) if ($Foreground) { Write-Host "[4/4] start frida in FOREGROUND (Ctrl+C to stop)" & $Frida @argList return } Write-Host "[4/4] start frida in background" $proc = Start-Process -FilePath $Frida ` -ArgumentList $argList ` -RedirectStandardOutput $StdoutLog ` -RedirectStandardError $StderrLog ` -PassThru ` -WindowStyle Hidden $state = [ordered]@{ package = $Package pid = $proc.Id delay_ms = $DelayMs hook = $Hook started_at = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") stdout_log = $StdoutLog stderr_log = $StderrLog no_restart = [bool]$NoRestart } $state | ConvertTo-Json | Set-Content -Path $StateFile -Encoding UTF8 Write-Host "" Write-Host "auto-dismiss background task started" Write-Host "pid : $($proc.Id)" Write-Host "hook : $Hook" Write-Host "stdout log : $StdoutLog" Write-Host "stderr log : $StderrLog" Write-Host "state file : $StateFile" Write-Host "" Write-Host "tip: tail stdout to watch dismiss events:" Write-Host " Get-Content -Path $StdoutLog -Wait -Tail 20"