Add brand visual style to page profile and logo palette to image prompts

Adds PageProfile.visualStyle (with migration), feeds it into every AI
prompt route, and samples a logo palette for image generation. Also
hardens Chrome focus handling and image copy in the ChatGPT driver.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Loma Bot Ops
2026-11-23 20:58:06 +07:00
parent dc6ee09a55
commit 4c93bf0a11
18 changed files with 166 additions and 49 deletions
+86 -32
View File
@@ -19,11 +19,19 @@ $deadline = [DateTime]::UtcNow.AddSeconds(215)
$script:panelMode = $false
function Focus-Chrome {
[ChromeKeyboard]::ActivateWindow($browser.MainWindowHandle)
Start-Sleep -Milliseconds 300
$focusDeadline = [DateTime]::UtcNow.AddSeconds(3)
do {
[ChatGptWindow]::ShowWindow($browser.MainWindowHandle,3) | Out-Null
$shellControl.AppActivate($browser.Id) | Out-Null
[ChromeKeyboard]::ActivateWindow($browser.MainWindowHandle)
Start-Sleep -Milliseconds 150
$foregroundWindow = [ChatGptWindow]::GetForegroundWindow()
if ($foregroundWindow -eq $browser.MainWindowHandle) { return }
} while ([DateTime]::UtcNow -lt $focusDeadline)
[uint32]$foregroundProcessId = 0
[ChatGptWindow]::GetWindowThreadProcessId([ChatGptWindow]::GetForegroundWindow(),[ref]$foregroundProcessId) | Out-Null
if ($foregroundProcessId -ne $browser.Id) { throw "Chrome could not take foreground focus (target=$($browser.Id), foreground=$foregroundProcessId); refusing to send keystrokes to another application." }
[ChatGptWindow]::GetWindowThreadProcessId($foregroundWindow,[ref]$foregroundProcessId) | Out-Null
throw "Chrome could not take foreground focus (target=$($browser.Id), foreground=$foregroundProcessId); refusing to send keystrokes to another application."
}
function Get-Controls {
$root = [System.Windows.Automation.AutomationElement]::FromHandle($browser.MainWindowHandle)
@@ -43,6 +51,13 @@ function Find-Control($type, $pattern) {
}
return $null
}
function Find-GlobalControl($type, $pattern) {
$root = [System.Windows.Automation.AutomationElement]::FromHandle($browser.MainWindowHandle)
foreach ($control in $root.FindAll([System.Windows.Automation.TreeScope]::Descendants,[System.Windows.Automation.Condition]::TrueCondition)) {
if ($control.Current.ControlType -eq $type -and $control.Current.Name -match $pattern) { return $control }
}
return $null
}
function Invoke-Control($control) {
$invoke = $null
if ($control.TryGetCurrentPattern([System.Windows.Automation.InvokePattern]::Pattern,[ref]$invoke)) {
@@ -57,8 +72,7 @@ function Invoke-Control($control) {
}
try {
[ChatGptWindow]::ShowWindow($browser.MainWindowHandle,3) | Out-Null
Focus-Chrome
if ($ProbeFocus) { Write-Output 'Interactive Chrome foreground focus verified.'; exit 0 }
if ($ProbeFocus) { Focus-Chrome; Write-Output 'Interactive Chrome foreground focus verified.'; exit 0 }
if (($env:CHATGPT_SURFACE -ne 'tab')) {
$extension = Find-Control ([System.Windows.Automation.ControlType]::Button) '^ChatGPT(?:[.]|\r|\n|$)'
if (-not $extension) { throw 'Open the ChatGPT Chrome extension side panel first.' }
@@ -69,7 +83,11 @@ try {
[ChatGptWindow]::mouse_event(2,0,0,0,[UIntPtr]::Zero)
[ChatGptWindow]::mouse_event(4,0,0,0,[UIntPtr]::Zero)
}
Start-Sleep -Milliseconds 1000
$panelDeadline = [DateTime]::UtcNow.AddSeconds(10)
while (@(Get-Controls).Count -eq 0 -and [DateTime]::UtcNow -lt $panelDeadline) { Start-Sleep -Milliseconds 250 }
if (@(Get-Controls).Count -eq 0) { throw 'ChatGPT extension side panel did not open.' }
$chatMode = Find-Control ([System.Windows.Automation.ControlType]::Button) '^Chat$'
if ($chatMode) { Invoke-Control $chatMode; Start-Sleep -Milliseconds 500 }
if (-not $ExportCurrent) {
$closePreview = Find-Control ([System.Windows.Automation.ControlType]::Button) '^(ปิด|Close|Close preview|Close image preview|ปิดตัวอย่างภาพ|ปิดตัวแสดงผล)$'
if ($closePreview) { Invoke-Control $closePreview; Start-Sleep -Milliseconds 500 }
@@ -108,16 +126,15 @@ try {
Start-Sleep -Milliseconds 700
}
if (-not $editor) { throw 'Chat composer unavailable. Check login or manual browser verification.' }
Focus-Chrome
$editor = Find-Control ([System.Windows.Automation.ControlType]::Edit) '^(ถาม ChatGPT|Ask ChatGPT|Message ChatGPT)$'
$editor.SetFocus()
$editorBounds = $editor.Current.BoundingRectangle
[ChatGptWindow]::SetCursorPos([int]($editorBounds.Left + $editorBounds.Width / 2),[int]($editorBounds.Top + $editorBounds.Height / 2)) | Out-Null
[ChatGptWindow]::mouse_event(2,0,0,0,[UIntPtr]::Zero)
[ChatGptWindow]::mouse_event(4,0,0,0,[UIntPtr]::Zero)
Start-Sleep -Milliseconds 300
Focus-Chrome
[ChromeKeyboard]::TypeText([string]$request.prompt)
$editorValue = $null
if ($editor.TryGetCurrentPattern([System.Windows.Automation.ValuePattern]::Pattern,[ref]$editorValue)) {
$editorValue.SetValue([string]$request.prompt)
} else {
Focus-Chrome
$editor.SetFocus()
[ChromeKeyboard]::TypeText([string]$request.prompt)
}
$send = $null
$sendDeadline = [DateTime]::UtcNow.AddSeconds(15)
while ([DateTime]::UtcNow -lt $sendDeadline) {
@@ -137,28 +154,65 @@ try {
}
if (-not $imageButton) { throw 'Image generation timed out; inspect the ChatGPT tab before retrying.' }
# Read only the new generated image, never an old clipboard image or a screenshot.
Focus-Chrome
$copy = $null
if ($env:CHATGPT_IMAGE_EXPORT -eq 'clipboard') { $copy = Find-Control ([System.Windows.Automation.ControlType]::Button) '^(คัดลอกรูปภาพ|Copy image)$' }
if ($copy) {
[System.Windows.Forms.Clipboard]::Clear()
Invoke-Control $copy
$copyDeadline = [DateTime]::UtcNow.AddSeconds(8)
while ([DateTime]::UtcNow -lt $copyDeadline) {
if ([System.Windows.Forms.Clipboard]::ContainsImage()) {
$bitmap = [System.Windows.Forms.Clipboard]::GetImage()
try { $bitmap.Save($OutputFile,[System.Drawing.Imaging.ImageFormat]::Png) } finally { $bitmap.Dispose() }
Write-Output 'Generated image saved.'
exit 0
# Current ChatGPT builds expose Copy image directly while Download may only
# appear in some preview variants. Prefer the direct control and retain the
# download path as a fallback when clipboard access is unavailable.
$copyControlDeadline = [DateTime]::UtcNow.AddSeconds(15)
while ([DateTime]::UtcNow -lt $copyControlDeadline) {
$copy = Find-Control ([System.Windows.Automation.ControlType]::Button) '^(คัดลอกรูปภาพ|Copy image)$'
if ($copy) {
[System.Windows.Forms.Clipboard]::Clear()
Invoke-Control $copy
$copyDeadline = [DateTime]::UtcNow.AddSeconds(8)
while ([DateTime]::UtcNow -lt $copyDeadline) {
if ([System.Windows.Forms.Clipboard]::ContainsImage()) {
$bitmap = [System.Windows.Forms.Clipboard]::GetImage()
try { $bitmap.Save($OutputFile,[System.Drawing.Imaging.ImageFormat]::Png) } finally { $bitmap.Dispose() }
Write-Output 'Generated image saved.'
exit 0
}
Start-Sleep -Milliseconds 500
}
break
}
Start-Sleep -Milliseconds 500
}
# Some extension builds hide Copy image but expose it in the image context
# menu. This still copies the original bitmap and avoids download policy.
$generatedImage = Find-Control ([System.Windows.Automation.ControlType]::Image) '^(รูปภาพที่สร้าง|Generated image|Image generated)\s*\d+$'
if ($generatedImage -and -not $generatedImage.Current.IsOffscreen) {
Focus-Chrome
$bounds = $generatedImage.Current.BoundingRectangle
[ChatGptWindow]::SetCursorPos([int]($bounds.Left + $bounds.Width / 2),[int]($bounds.Top + $bounds.Height / 2)) | Out-Null
[ChatGptWindow]::mouse_event(8,0,0,0,[UIntPtr]::Zero)
[ChatGptWindow]::mouse_event(16,0,0,0,[UIntPtr]::Zero)
Start-Sleep -Milliseconds 500
$copyMenu = Find-GlobalControl ([System.Windows.Automation.ControlType]::MenuItem) '^(คัดลอกรูปภาพ|Copy image)$'
if ($copyMenu) {
[System.Windows.Forms.Clipboard]::Clear()
Invoke-Control $copyMenu
$copyDeadline = [DateTime]::UtcNow.AddSeconds(15)
while ([DateTime]::UtcNow -lt $copyDeadline) {
if ([System.Windows.Forms.Clipboard]::ContainsImage()) {
$bitmap = [System.Windows.Forms.Clipboard]::GetImage()
try { $bitmap.Save($OutputFile,[System.Drawing.Imaging.ImageFormat]::Png) } finally { $bitmap.Dispose() }
Write-Output 'Generated image saved from context menu.'
exit 0
}
Start-Sleep -Milliseconds 500
}
Start-Sleep -Milliseconds 500
}
}
# Clipboard permissions can fail even after generation succeeds. Export the same image instead.
Focus-Chrome
$download = Find-Control ([System.Windows.Automation.ControlType]::Button) '^(ดาวน์โหลด|Download|Download image)$'
if (-not $download) {
$imageButton = Find-Control ([System.Windows.Automation.ControlType]::Button) '^(รูปภาพที่สร้าง|Generated image|Image generated)\s*1$'
$imageButton = $null
$imageDeadline = [DateTime]::UtcNow.AddSeconds(10)
while ([DateTime]::UtcNow -lt $imageDeadline) {
$imageButton = Find-Control ([System.Windows.Automation.ControlType]::Button) '^(รูปภาพที่สร้าง|Generated image|Image generated)\s*1$'
if ($imageButton) { break }
Start-Sleep -Milliseconds 500
}
if (-not $imageButton) { throw 'The generated image is no longer visible.' }
Invoke-Control $imageButton
$exportDeadline = [DateTime]::UtcNow.AddSeconds(15)
+9 -1
View File
@@ -11,11 +11,19 @@ public static class ChromeKeyboard
[DllImport("user32.dll")] private static extern bool AttachThreadInput(uint source, uint target, bool attach);
[DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr h);
[DllImport("user32.dll")] private static extern bool BringWindowToTop(IntPtr h);
[DllImport("user32.dll")] private static extern void keybd_event(byte key, byte scan, uint flags, UIntPtr extra);
public static void ActivateWindow(IntPtr window)
{
if (GetForegroundWindow() == window) return;
if (SetForegroundWindow(window)) return;
// A synthetic Alt press is the documented user-input signal Windows
// uses to allow a background process to transfer foreground focus.
keybd_event(0x12, 0, 0, UIntPtr.Zero);
keybd_event(0x12, 0, 2, UIntPtr.Zero);
// SetForegroundWindow can report success before Windows has actually
// changed the foreground window. Only return after verifying the HWND.
SetForegroundWindow(window);
if (GetForegroundWindow() == window) return;
uint ignored;
uint current = GetCurrentThreadId();
uint foreground = GetWindowThreadProcessId(GetForegroundWindow(), out ignored);