mirror of
https://github.com/storytold/gridcraft.git
synced 2026-12-05 23:00:06 +00:00
Observe the visible Safari print prompt through macOS Vision
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import AppKit
|
||||
import CoreGraphics
|
||||
import Vision
|
||||
|
||||
let request = VNRecognizeTextRequest()
|
||||
request.recognitionLevel = .accurate
|
||||
request.recognitionLanguages = ["en-US"]
|
||||
try VNImageRequestHandler(url: URL(fileURLWithPath: CommandLine.arguments[1])).perform([request])
|
||||
let observations = request.results ?? []
|
||||
|
||||
guard let prompt = observations.first(where: { observation in
|
||||
observation.topCandidates(1).first?.string.lowercased().hasPrefix("this webpage is trying to print.") == true
|
||||
}) else {
|
||||
print("no-webpage-confirmation")
|
||||
exit(0)
|
||||
}
|
||||
|
||||
let buttons = try observations.compactMap { observation -> CGRect? in
|
||||
guard let candidate = observation.topCandidates(1).first,
|
||||
let range = candidate.string.range(of: #"\bPrint$"#, options: .regularExpression),
|
||||
let box = try candidate.boundingBox(for: range)?.boundingBox else { return nil }
|
||||
return box
|
||||
}.filter { box in
|
||||
box.midX > prompt.boundingBox.midX && box.midY < prompt.boundingBox.minY
|
||||
&& prompt.boundingBox.minY - box.midY < 0.16
|
||||
}
|
||||
guard buttons.count == 1, let button = buttons.first else {
|
||||
print("found-webpage-confirmation-without-one-print-button")
|
||||
exit(0)
|
||||
}
|
||||
guard NSWorkspace.shared.frontmostApplication?.bundleIdentifier == "com.apple.Safari" else {
|
||||
print("webpage-confirmation-safari-not-frontmost")
|
||||
exit(0)
|
||||
}
|
||||
guard CGPreflightPostEventAccess() else {
|
||||
print("webpage-confirmation-event-access-denied")
|
||||
exit(0)
|
||||
}
|
||||
|
||||
let screen = CGDisplayBounds(CGMainDisplayID())
|
||||
let click = CGPoint(x: screen.minX + button.midX * screen.width, y: screen.minY + (1 - button.midY) * screen.height)
|
||||
guard let down = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: click, mouseButton: .left),
|
||||
let up = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: click, mouseButton: .left) else {
|
||||
print("webpage-confirmation-event-creation-failed")
|
||||
exit(0)
|
||||
}
|
||||
down.post(tap: .cghidEventTap)
|
||||
up.post(tap: .cghidEventTap)
|
||||
print("confirmed-webpage")
|
||||
@@ -142,13 +142,23 @@ def native_sheet(action):
|
||||
def wait_sheet(name, visible):
|
||||
deadline = time.monotonic() + 35
|
||||
confirmed = False
|
||||
last_confirmation = None
|
||||
while time.monotonic() < deadline:
|
||||
state = native_sheet("inspect")
|
||||
if (state == "print-sheet") == visible:
|
||||
print(f"{name}: {state}", flush=True)
|
||||
return
|
||||
if visible and not confirmed and name in COLORS:
|
||||
confirmed = native_sheet("confirm-webpage") == "confirmed-webpage"
|
||||
confirmation = native_sheet("confirm-webpage")
|
||||
if confirmation != "confirmed-webpage" and os.environ.get("SAFARI_CONFIRM"):
|
||||
path = RESULTS / f"{name}-webpage-confirmation.png"
|
||||
if snapshot(path):
|
||||
result = subprocess.run([os.environ["SAFARI_CONFIRM"], path], capture_output=True, text=True, timeout=10, check=True)
|
||||
confirmation = result.stdout.strip()
|
||||
if confirmation != last_confirmation:
|
||||
print(f"{name}: {confirmation}", flush=True)
|
||||
last_confirmation = confirmation
|
||||
confirmed = confirmation == "confirmed-webpage"
|
||||
if confirmed:
|
||||
print(f"{name}: accepted Safari's per-print webpage confirmation", flush=True)
|
||||
time.sleep(0.35)
|
||||
|
||||
@@ -62,6 +62,8 @@ jobs:
|
||||
python3 -m venv "$RUNNER_TEMP/safari-probe-venv"
|
||||
python="$RUNNER_TEMP/safari-probe-venv/bin/python"
|
||||
"$python" -m pip install Pillow==12.0.0 reportlab==4.4.4 selenium==4.38.0
|
||||
export SAFARI_CONFIRM="$RUNNER_TEMP/safari-confirm"
|
||||
/usr/bin/swiftc .github/print-probe/safari-confirm.swift -o "$SAFARI_CONFIRM"
|
||||
bash .github/print-probe/mac-safari-capture.sh "$python"
|
||||
- name: Preserve output or the native-access diagnostic
|
||||
if: always()
|
||||
|
||||
Reference in New Issue
Block a user