Prefer location.assign for endscreen leave, and ship hub analytics/charts plus host deploy helpers. Co-authored-by: Cursor <cursoragent@cursor.com>
126 lines
5.0 KiB
PowerShell
126 lines
5.0 KiB
PowerShell
# YouTube Studio Auto-Dismiss via Windows UI Automation (trusted OS mouse click).
|
|
# Use this instead of Tampermonkey synthetic MouseEvent clicks (isTrusted=false triggers player "Something went wrong").
|
|
#
|
|
# Usage:
|
|
# powershell -ExecutionPolicy Bypass -File tools/youtube_studio_dismiss_uia.ps1
|
|
# powershell -ExecutionPolicy Bypass -File tools/youtube_studio_dismiss_uia.ps1 -Once
|
|
# powershell -ExecutionPolicy Bypass -File tools/youtube_studio_dismiss_uia.ps1 -PollSeconds 2
|
|
|
|
param(
|
|
[double]$PollSeconds = 1.5,
|
|
[switch]$Once,
|
|
[int]$MaxClicks = 0
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
Add-Type -AssemblyName UIAutomationClient
|
|
Add-Type -AssemblyName UIAutomationTypes
|
|
|
|
Add-Type @"
|
|
using System;
|
|
using System.Runtime.InteropServices;
|
|
public static class YtDismissMouse {
|
|
[DllImport("user32.dll")] public static extern bool SetCursorPos(int X, int Y);
|
|
[DllImport("user32.dll")] public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
|
|
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
public const uint LEFTDOWN = 0x0002;
|
|
public const uint LEFTUP = 0x0004;
|
|
public static void Click(int x, int y) {
|
|
SetCursorPos(x, y);
|
|
System.Threading.Thread.Sleep(60 + (new Random().Next(0, 80)));
|
|
mouse_event(LEFTDOWN, 0, 0, 0, UIntPtr.Zero);
|
|
System.Threading.Thread.Sleep(35 + (new Random().Next(0, 40)));
|
|
mouse_event(LEFTUP, 0, 0, 0, UIntPtr.Zero);
|
|
}
|
|
}
|
|
"@
|
|
|
|
function Get-StudioWindow {
|
|
$root = [System.Windows.Automation.AutomationElement]::RootElement
|
|
$winCond = New-Object System.Windows.Automation.PropertyCondition(
|
|
[System.Windows.Automation.AutomationElement]::ControlTypeProperty,
|
|
[System.Windows.Automation.ControlType]::Window
|
|
)
|
|
$wins = $root.FindAll([System.Windows.Automation.TreeScope]::Children, $winCond)
|
|
foreach ($w in $wins) {
|
|
$name = $w.Current.Name
|
|
if ($name -match 'YouTube Studio') { return $w }
|
|
}
|
|
foreach ($w in $wins) {
|
|
if ($w.Current.Name -notmatch 'Google Chrome') { continue }
|
|
$c = New-Object System.Windows.Automation.PropertyCondition(
|
|
[System.Windows.Automation.AutomationElement]::NameProperty, 'Dismiss'
|
|
)
|
|
if ($w.FindFirst([System.Windows.Automation.TreeScope]::Descendants, $c)) { return $w }
|
|
}
|
|
return $null
|
|
}
|
|
|
|
function Find-DismissButton([System.Windows.Automation.AutomationElement]$win) {
|
|
$and = New-Object System.Windows.Automation.AndCondition(
|
|
(New-Object System.Windows.Automation.PropertyCondition(
|
|
[System.Windows.Automation.AutomationElement]::NameProperty, 'Dismiss')),
|
|
(New-Object System.Windows.Automation.PropertyCondition(
|
|
[System.Windows.Automation.AutomationElement]::ControlTypeProperty,
|
|
[System.Windows.Automation.ControlType]::Button))
|
|
)
|
|
return $win.FindFirst([System.Windows.Automation.TreeScope]::Descendants, $and)
|
|
}
|
|
|
|
function Has-RiskOverlay([System.Windows.Automation.AutomationElement]$win) {
|
|
foreach ($name in @('Something went wrong', 'Try again')) {
|
|
$c = New-Object System.Windows.Automation.PropertyCondition(
|
|
[System.Windows.Automation.AutomationElement]::NameProperty, $name
|
|
)
|
|
if ($win.FindFirst([System.Windows.Automation.TreeScope]::Descendants, $c)) { return $true }
|
|
}
|
|
return $false
|
|
}
|
|
|
|
function Click-DismissTrusted([System.Windows.Automation.AutomationElement]$btn) {
|
|
$r = $btn.Current.BoundingRectangle
|
|
if ($r.Width -le 0 -or $r.Height -le 0) { return $false }
|
|
$jitterX = Get-Random -Minimum -3 -Maximum 4
|
|
$jitterY = Get-Random -Minimum -2 -Maximum 3
|
|
$x = [int]($r.X + ($r.Width / 2) + $jitterX)
|
|
$y = [int]($r.Y + ($r.Height / 2) + $jitterY)
|
|
Write-Host ("[{0}] Trusted mouse click Dismiss at ({1},{2}) class={3}" -f (Get-Date -Format 'HH:mm:ss'), $x, $y, $btn.Current.ClassName)
|
|
[YtDismissMouse]::Click($x, $y)
|
|
return $true
|
|
}
|
|
|
|
Write-Host "YouTube Studio Dismiss UIA watcher started (trusted OS clicks)."
|
|
Write-Host "Keep Studio livestream tab open. Ctrl+C to stop."
|
|
$clicks = 0
|
|
|
|
while ($true) {
|
|
try {
|
|
$win = Get-StudioWindow
|
|
if ($win) {
|
|
if (Has-RiskOverlay $win) {
|
|
Write-Host ("[{0}] Risk overlay detected (Something went wrong / Try again)." -f (Get-Date -Format 'HH:mm:ss'))
|
|
}
|
|
$btn = Find-DismissButton $win
|
|
if ($btn -and $btn.Current.IsEnabled -and -not $btn.Current.IsOffscreen) {
|
|
Start-Sleep -Milliseconds (Get-Random -Minimum 400 -Maximum 1200)
|
|
if (Click-DismissTrusted $btn) {
|
|
$clicks++
|
|
Start-Sleep -Seconds 2
|
|
$win2 = Get-StudioWindow
|
|
if ($win2 -and (Has-RiskOverlay $win2)) {
|
|
Write-Host "WARNING: risk overlay appeared after trusted click."
|
|
} elseif ($win2 -and -not (Find-DismissButton $win2)) {
|
|
Write-Host ("[{0}] Dismiss cleared OK." -f (Get-Date -Format 'HH:mm:ss'))
|
|
}
|
|
if ($Once) { break }
|
|
if ($MaxClicks -gt 0 -and $clicks -ge $MaxClicks) { break }
|
|
}
|
|
}
|
|
}
|
|
} catch {
|
|
Write-Host ("[{0}] watch error: {1}" -f (Get-Date -Format 'HH:mm:ss'), $_.Exception.Message)
|
|
}
|
|
if ($Once) { break }
|
|
Start-Sleep -Seconds $PollSeconds
|
|
}
|