-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchrome_script.py
More file actions
999 lines (885 loc) · 38.8 KB
/
chrome_script.py
File metadata and controls
999 lines (885 loc) · 38.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
import subprocess
import base64
import os
import logging
import json
logger = logging.getLogger(__name__)
def run_applescript(script):
"""Run AppleScript and return the result"""
wrapped = (
'try\n'
+ script
+ '\n'
+ 'on error errMsg number errNum\n'
+ 'return "__MICPIPE_APPLESCRIPT_ERROR__:" & errNum & ":" & errMsg\n'
+ 'end try'
)
process = subprocess.Popen(
["osascript", "-e", wrapped],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
out, err = process.communicate()
out = (out or "").strip()
debug = os.environ.get("MICPIPE_DEBUG_APPLESCRIPT") in ("1", "true", "TRUE", "yes", "YES")
if out.startswith("__MICPIPE_APPLESCRIPT_ERROR__:"):
if debug:
logger.debug(out)
return out
return out
class ChromeController:
"""Base class for controlling various AI chat interfaces in Chrome."""
def __init__(self, service_name, url_pattern, title_pattern, default_url):
self.service_name = service_name
self.url_pattern = url_pattern
self.title_pattern = title_pattern
self.default_url = default_url
def create_dedicated_window(self, bounds=(50, 50, 500, 400)):
"""Create a dedicated Chrome window and return (window_id, tab_index) or None."""
open_url = self.default_url
script = f'''
tell application "Google Chrome"
if (count of windows) = 0 then
make new window
end if
set newWin to make new window with properties {{bounds:{{{bounds[0]}, {bounds[1]}, {bounds[2]}, {bounds[3]}}}}}
set URL of active tab of newWin to "{open_url}"
if (count of windows) > 1 then
set index of newWin to (count of windows)
end if
return "WIN_ID:" & (id of newWin) & ",TAB:1"
end tell
'''
res = run_applescript(script)
if not res or res.startswith("__MICPIPE_APPLESCRIPT_ERROR__"):
return None
if res.startswith("WIN_ID:") and ",TAB:" in res:
try:
win_part, tab_part = res.split(",TAB:")
win_id = int(win_part.replace("WIN_ID:", ""))
tab_idx = int(tab_part)
return (win_id, tab_idx)
except Exception:
return None
return None
def is_window_alive(self, window_id, tab_index) -> bool:
"""Check if a specific window/tab still exists and matches the service."""
try:
win_id = int(window_id)
tab_idx = int(tab_index)
except (ValueError, TypeError):
return False
if win_id <= 0 or tab_idx <= 0:
return False
script = f'''
tell application "Google Chrome"
if (count of windows) = 0 then return "NO_WINDOW"
set targetWin to missing value
set targetWinId to {win_id} as integer
repeat with win in windows
set currentWinId to (id of win) as integer
if currentWinId = targetWinId then
set targetWin to win
exit repeat
end if
end repeat
if targetWin is missing value then return "NOT_FOUND"
try
set t to tab {tab_idx} of targetWin
on error
return "TAB_NOT_FOUND"
end try
set tUrl to ""
set tTitle to ""
try
set tUrl to (URL of t) as text
end try
try
set tTitle to (title of t) as text
end try
if (tUrl contains "{self.url_pattern}") or (tTitle contains "{self.title_pattern}") then
return "OK"
end if
return "MISMATCH"
end tell
'''
res = run_applescript(script)
return res == "OK"
def reveal_window(self, window_id, bounds=(60, 60, 1100, 800)) -> bool:
"""Resize and bring a Chrome window to the front."""
try:
win_id = int(window_id)
except (ValueError, TypeError):
return False
if win_id <= 0:
return False
script = f'''
tell application "Google Chrome"
if (count of windows) = 0 then return "NO_WINDOW"
set targetWin to missing value
set targetWinId to {win_id} as integer
repeat with win in windows
set currentWinId to (id of win) as integer
if currentWinId = targetWinId then
set targetWin to win
exit repeat
end if
end repeat
if targetWin is missing value then return "NOT_FOUND"
set bounds of targetWin to {{{bounds[0]}, {bounds[1]}, {bounds[2]}, {bounds[3]}}}
set index of targetWin to 1
activate
return "OK"
end tell
'''
res = run_applescript(script)
return res == "OK"
def set_window_bounds(self, window_id, bounds) -> bool:
"""Resize/move a Chrome window without changing focus."""
try:
win_id = int(window_id)
except (ValueError, TypeError):
return False
if win_id <= 0:
return False
script = f'''
tell application "Google Chrome"
if (count of windows) = 0 then return "NO_WINDOW"
set targetWin to missing value
set targetWinId to {win_id} as integer
repeat with win in windows
set currentWinId to (id of win) as integer
if currentWinId = targetWinId then
set targetWin to win
exit repeat
end if
end repeat
if targetWin is missing value then return "NOT_FOUND"
set bounds of targetWin to {{{bounds[0]}, {bounds[1]}, {bounds[2]}, {bounds[3]}}}
return "OK"
end tell
'''
res = run_applescript(script)
return res == "OK"
def demote_window(self, window_id) -> bool:
"""Push a Chrome window to the back of the window stack so it is no longer the 'last active' window.
This prevents the dedicated (hidden) window from hijacking external link opens.
Only acts when Chrome has 2+ windows; single-window case is a no-op.
"""
try:
win_id = int(window_id)
except (ValueError, TypeError):
return False
if win_id <= 0:
return False
script = f'''
tell application "Google Chrome"
if (count of windows) < 2 then return "SKIP"
set targetWin to missing value
set targetWinId to {win_id} as integer
repeat with win in windows
set currentWinId to (id of win) as integer
if currentWinId = targetWinId then
set targetWin to win
exit repeat
end if
end repeat
if targetWin is missing value then return "NOT_FOUND"
set index of targetWin to (count of windows)
return "OK"
end tell
'''
res = run_applescript(script)
return res in ("OK", "SKIP")
def reload_tab(self, window_id, tab_index) -> bool:
"""Reload a specific tab in a specific Chrome window."""
try:
win_id = int(window_id)
tab_idx = int(tab_index)
except (ValueError, TypeError):
return False
if win_id <= 0 or tab_idx <= 0:
return False
script = f'''
tell application "Google Chrome"
if (count of windows) = 0 then return "NO_WINDOW"
set targetWin to missing value
set targetWinId to {win_id} as integer
repeat with win in windows
set currentWinId to (id of win) as integer
if currentWinId = targetWinId then
set targetWin to win
exit repeat
end if
end repeat
if targetWin is missing value then return "NOT_FOUND"
try
set targetTab to tab {tab_idx} of targetWin
on error
return "TAB_NOT_FOUND"
end try
reload targetTab
return "RELOADED"
end tell
'''
res = run_applescript(script)
return res == "RELOADED"
def close_window(self, window_id) -> bool:
"""Close a specific Chrome window by ID."""
try:
win_id = int(window_id)
except (ValueError, TypeError):
return False
if win_id <= 0:
return False
script = f'''
tell application "Google Chrome"
if (count of windows) = 0 then return "NO_WINDOW"
set targetWin to missing value
set targetWinId to {win_id} as integer
repeat with win in windows
set currentWinId to (id of win) as integer
if currentWinId = targetWinId then
set targetWin to win
exit repeat
end if
end repeat
if targetWin is missing value then return "NOT_FOUND"
close targetWin
return "CLOSED"
end tell
'''
res = run_applescript(script)
return res == "CLOSED"
def is_recording_active(self, preferred_location=None) -> bool:
"""Default implementation for services without recording-state detection."""
return False
def get_tab_location(self):
"""Return (window_id, tab_index) for the first matching tab, or None."""
script = f'''
tell application "Google Chrome"
if (count of windows) = 0 then return "NOT_FOUND"
repeat with win in windows
set tabIndex to 1
repeat with t in tabs of win
try
if (URL of t contains "{self.url_pattern}") or (title of t contains "{self.title_pattern}") then
return "WIN_ID:" & (id of win) & ",TAB:" & tabIndex
end if
end try
set tabIndex to tabIndex + 1
end repeat
end repeat
return "NOT_FOUND"
end tell
'''
res = run_applescript(script)
if not res or res.startswith("__MICPIPE_APPLESCRIPT_ERROR__"):
return None
if res.startswith("WIN_ID:") and ",TAB:" in res:
try:
win_part, tab_part = res.split(",TAB:")
win_id = int(win_part.replace("WIN_ID:", ""))
tab_idx = int(tab_part)
return (win_id, tab_idx)
except Exception:
return None
return None
def get_front_tab_location(self):
"""Return (window_id, tab_index) if the front tab matches, else None."""
script = f'''
tell application "Google Chrome"
if (count of windows) = 0 then return "NOT_FOUND"
try
set frontWin to front window
set winId to id of frontWin
set tabIndex to active tab index of frontWin
set t to active tab of frontWin
if (URL of t contains "{self.url_pattern}") or (title of t contains "{self.title_pattern}") then
return "WIN_ID:" & winId & ",TAB:" & tabIndex
end if
end try
return "NOT_MATCHED"
end tell
'''
res = run_applescript(script)
if not res or res.startswith("__MICPIPE_APPLESCRIPT_ERROR__"):
return None
if res.startswith("WIN_ID:") and ",TAB:" in res:
try:
win_part, tab_part = res.split(",TAB:")
win_id = int(win_part.replace("WIN_ID:", ""))
tab_idx = int(tab_part)
return (win_id, tab_idx)
except Exception:
return None
return None
def _execute_js(self, js_code, preferred_location=None, open_url=None):
b64_js = base64.b64encode(js_code.encode('utf-8')).decode('utf-8')
# Check if preferred_location is window_id or URL based on type
preferred_win_id = 0
preferred_tab_index = 0
if preferred_location and len(preferred_location) == 2:
try:
# Try to parse first element as int (window ID)
preferred_win_id = int(preferred_location[0])
preferred_tab_index = int(preferred_location[1])
except (ValueError, TypeError):
# Not a number, might be URL from new code
preferred_win_id = 0
preferred_tab_index = 0
script = f'''
tell application "Google Chrome"
if (count of windows) = 0 then return "NO_WINDOW"
if {preferred_win_id} <= 0 or {preferred_tab_index} <= 0 then return "NO_LOCATION"
set foundWin to missing value
set targetWinId to {preferred_win_id} as integer
repeat with win in windows
set currentWinId to (id of win) as integer
if currentWinId = targetWinId then
set foundWin to win
exit repeat
end if
end repeat
if foundWin is missing value then return "NOT_FOUND"
try
set pt to tab {preferred_tab_index} of foundWin
on error
return "NOT_FOUND"
end try
-- Verify the preferred tab is still the right service tab.
set ptUrl to ""
set ptTitle to ""
try
set ptUrl to (URL of pt) as text
end try
try
set ptTitle to (title of pt) as text
end try
if not ((ptUrl contains "{self.url_pattern}") or (ptTitle contains "{self.title_pattern}")) then
return "NOT_FOUND"
end if
set res to execute pt javascript "eval(decodeURIComponent(escape(window.atob('{b64_js}'))))"
return "SUCCESS:USED_WIN_ID=" & {preferred_win_id} & ",TAB=" & {preferred_tab_index} & ":" & res
end tell
'''
result = run_applescript(script)
logger.debug(f"[_execute_js] preferred_win_id={preferred_win_id}, result={result[:200]}")
return result
def is_front_tab_match(self) -> bool:
return self.get_front_tab_location() is not None
class ChatGPTChrome(ChromeController):
def __init__(self):
super().__init__("ChatGPT", "chatgpt.com", "ChatGPT", "https://chatgpt.com")
def ensure_chatgpt_tab_exists(self):
"""Create a dedicated ChatGPT window. Returns status string."""
location = self.create_dedicated_window()
return "CREATED" if location else "ERROR"
def is_front_tab_chatgpt(self):
"""Check if the front tab in Chrome is a ChatGPT tab. Returns 'YES' or 'NO'."""
return "YES" if self.get_front_tab_location() is not None else "NO"
def get_chatgpt_tab_location(self):
"""Alias for get_tab_location for clarity."""
return self.get_tab_location()
def get_front_chatgpt_tab_location(self):
"""Alias for get_front_tab_location for clarity."""
return self.get_front_tab_location()
def is_page_ready(self, preferred_location=None):
js = '''
(function() {
if (document.readyState !== 'complete') return "PAGE_NOT_READY";
var buttons = Array.from(document.querySelectorAll('button'));
var btn = buttons.find(b =>
(b.ariaLabel && b.ariaLabel.toLowerCase().includes('dictate')) ||
b.querySelector('svg path[d*="M12 1a3 3 0 0 0-3 3v8a3 3 0 0 0 6 0V4a3 3 0 0 0-3-3z"]')
);
return btn ? "READY" : "BTN_NOT_FOUND";
})()
'''
return self._execute_js(js, preferred_location)
def start_dictation(self, preferred_location=None):
js = '''
(function() {
var buttons = Array.from(document.querySelectorAll('button'));
var btn = buttons.find(b =>
(b.ariaLabel && b.ariaLabel.toLowerCase().includes('dictate')) ||
b.querySelector('svg path[d*="M12 1a3 3 0 0 0-3 3v8a3 3 0 0 0 6 0V4a3 3 0 0 0-3-3z"]')
);
if (btn) { btn.click(); return "START_DONE"; }
return "START_BTN_NOT_FOUND";
})()
'''
return self._execute_js(js, preferred_location)
def is_recording_active(self, preferred_location=None):
js = '''
(function() {
var buttons = Array.from(document.querySelectorAll('button'));
var btn = buttons.find(b =>
(b.ariaLabel && b.ariaLabel.includes('Submit dictation')) ||
b.querySelector('svg path[d*="M20 6L9 17l-5-5"]')
);
return btn ? "ACTIVE" : "INACTIVE";
})()
'''
return self._execute_js(js, preferred_location)
def stop_dictation(self, preferred_location=None):
"""Stop dictation by clicking Submit dictation button to finish and keep transcribed text."""
js = '''
(function() {
// Return window ID for debugging
var winInfo = "UNKNOWN";
try {
winInfo = window.location.href;
} catch(e) {}
var buttons = Array.from(document.querySelectorAll('button'));
// Submit dictation button - finishes recording and keeps transcribed text.
// If this selector fails due to UI/locale changes, the caller should notify the user.
var btn = buttons.find(b =>
(b.ariaLabel && b.ariaLabel.includes('Submit dictation')) ||
b.querySelector('svg path[d*="M20 6L9 17l-5-5"]')
);
if (btn) {
btn.click();
return "SUBMIT_CLICKED:URL=" + winInfo;
}
return "SUBMIT_BTN_NOT_FOUND:URL=" + winInfo;
})()
'''
result = self._execute_js(js, preferred_location)
logger.debug(f"[stop_dictation] preferred_location={preferred_location}, result={result}")
return result
def cancel_dictation(self, preferred_location=None):
js = '''
(function() {
var buttons = Array.from(document.querySelectorAll('button'));
var btn = buttons.find(b => b.ariaLabel && b.ariaLabel.toLowerCase().includes('stop dictation'));
if (btn) { btn.click(); return "CANCEL_DONE"; }
return "CANCEL_BTN_NOT_FOUND";
})()
'''
return self._execute_js(js, preferred_location)
def get_text_and_clear(self, activate_first=True, preferred_location=None):
js_code = '''
(function() {
function diag(payload) {
try { return JSON.stringify(payload); } catch (e) { return String(payload); }
}
function isVisible(el) {
try {
if (!el) return false;
var r = el.getBoundingClientRect();
return !!(r && r.width > 0 && r.height > 0);
} catch (e) {
return false;
}
}
function findComposerBox() {
// 1) Known stable IDs / test IDs (varies by rollout)
var el = document.querySelector('#prompt-textarea');
if (el) return { el: el, via: '#prompt-textarea' };
el = document.querySelector('[data-testid="prompt-textarea"]');
if (el) return { el: el, via: '[data-testid=\"prompt-textarea\"]' };
// 2) Prefer the textbox inside the form that owns the send button (less ambiguity)
var sendBtn = document.querySelector('button[data-testid="send-button"]');
if (sendBtn && sendBtn.closest) {
var form = sendBtn.closest('form');
if (form) {
el =
form.querySelector('#prompt-textarea') ||
form.querySelector('[data-testid="prompt-textarea"]') ||
form.querySelector('textarea') ||
form.querySelector('div[contenteditable="true"][role="textbox"]') ||
form.querySelector('div[contenteditable="true"]');
if (el) return { el: el, via: 'send-button.closest(form)' };
}
}
// 3) Last resort: pick a visible textarea/contenteditable textbox
var candidates = []
.concat(Array.from(document.querySelectorAll('textarea')))
.concat(Array.from(document.querySelectorAll('div[contenteditable="true"][role="textbox"]')))
.concat(Array.from(document.querySelectorAll('div[contenteditable="true"]')));
for (var i = 0; i < candidates.length; i++) {
if (isVisible(candidates[i])) return { el: candidates[i], via: 'visible-candidate' };
}
return null;
}
var found = findComposerBox();
if (!found || !found.el) {
return "NOT_FOUND|DBG=" + diag({
href: (function(){ try { return location.href; } catch(e) { return null; } })(),
title: (function(){ try { return document.title; } catch(e) { return null; } })(),
promptTextareas: document.querySelectorAll('#prompt-textarea').length,
testidTextareas: document.querySelectorAll('[data-testid="prompt-textarea"]').length,
sendButtons: document.querySelectorAll('button[data-testid="send-button"]').length,
});
}
var box = found.el;
// Ensure the composer is focused; in some UI states transcription is only materialized after focus.
try { box.focus(); } catch(e) {}
var text = "";
try {
if (typeof box.value === 'string') text = box.value;
if (!text) text = box.innerText || box.textContent || "";
} catch(e) {}
if (!text || !text.trim()) {
var valLen = 0;
var innerLen = 0;
try { valLen = (typeof box.value === 'string') ? box.value.length : 0; } catch(e) {}
try { innerLen = (box.innerText || box.textContent || "").length; } catch(e) {}
return "EMPTY|DBG=" + diag({
href: (function(){ try { return location.href; } catch(e) { return null; } })(),
title: (function(){ try { return document.title; } catch(e) { return null; } })(),
via: found.via,
tag: (function(){ try { return box.tagName; } catch(e) { return null; } })(),
id: (function(){ try { return box.id; } catch(e) { return null; } })(),
dataTestid: (function(){ try { return box.getAttribute('data-testid'); } catch(e) { return null; } })(),
isVisible: isVisible(box),
valLen: valLen,
innerLen: innerLen,
});
}
// Clear composer
try {
if (typeof box.value === 'string') box.value = "";
} catch(e) {}
try {
box.innerText = "";
box.textContent = "";
box.innerHTML = "";
} catch(e) {}
try {
box.dispatchEvent(new Event('input', { bubbles: true }));
box.dispatchEvent(new Event('change', { bubbles: true }));
} catch(e) {}
return text.trim();
})()
'''
if not activate_first:
return self._execute_js(js_code, preferred_location)
if not preferred_location:
return "NO_LOCATION"
b64_js = base64.b64encode(js_code.encode('utf-8')).decode('utf-8')
win_id, tab_idx = preferred_location
script = f'''
tell application "Google Chrome"
set originalWin to front window
set originalTabIndex to active tab index of originalWin
set targetWin to missing value
set targetTab to missing value
set targetTabIndex to 0
-- Find window by ID
if {win_id} > 0 and {tab_idx} > 0 then
try
set targetWinId to {win_id} as integer
repeat with win in windows
set currentWinId to (id of win) as integer
if currentWinId = targetWinId then
set targetWin to win
set targetTab to tab {tab_idx} of targetWin
set targetTabIndex to {tab_idx}
exit repeat
end if
end repeat
end try
end if
-- Validate the preferred tab is still the right service tab.
if targetTab is not missing value then
set ptUrl to ""
set ptTitle to ""
try
set ptUrl to (URL of targetTab) as text
end try
try
set ptTitle to (title of targetTab) as text
end try
if not ((ptUrl contains "{self.url_pattern}") or (ptTitle contains "{self.title_pattern}")) then
return "NOT_FOUND"
end if
end if
if targetTab is missing value then return "NOT_FOUND"
set active tab index of targetWin to targetTabIndex
set index of targetWin to 1
delay 0.15
set res to execute targetTab javascript "eval(decodeURIComponent(escape(window.atob('{b64_js}'))))"
try
set index of originalWin to 1
set active tab index of originalWin to originalTabIndex
end try
try
if (count of windows) > 1 then
set index of targetWin to (count of windows)
end if
end try
return "SUCCESS:" & res
end tell
'''
return run_applescript(script)
def pre_fill_prompt(self, prompt, preferred_location=None):
"""Pre-fill prompt text in the input box using execCommand for better reactivity"""
js_code = f'''
(function() {{
var box = document.querySelector('#prompt-textarea');
if (!box) {{
box = document.querySelector('[data-testid="prompt-textarea"]');
}}
if (!box) return "NOT_FOUND";
try {{
box.focus();
// Select all and delete (clear)
document.execCommand('selectAll', false, null);
document.execCommand('delete', false, null);
// Insert text via execCommand (this triggers React state updates more reliably)
var success = document.execCommand('insertText', false, {json.dumps(prompt)});
if (!success) {{
// Fallback to setting property
if (box.tagName === 'TEXTAREA' || typeof box.value === 'string') {{
box.value = {json.dumps(prompt)};
}} else {{
box.innerText = {json.dumps(prompt)};
}}
box.dispatchEvent(new Event('input', {{ bubbles: true }}));
box.dispatchEvent(new Event('change', {{ bubbles: true }}));
}}
return "SUCCESS";
}} catch(e) {{
return "ERROR:" + e.message;
}}
}})()
'''
return self._execute_js(js_code, preferred_location)
def submit_message(self, preferred_location=None):
"""Click the send button to submit the message"""
js = '''
(function() {
var sendBtn = document.querySelector('button[data-testid="send-button"]') ||
document.querySelector('button[aria-label="Send prompt"]') ||
document.querySelector('button[data-testid="composer-submit-button"]') ||
document.querySelector('#composer-submit-button');
// If still not found, search by SVG path (extreme fallback)
if (!sendBtn) {
var svgs = document.querySelectorAll('svg');
for (var i = 0; i < svgs.length; i++) {
if (svgs[i].innerHTML.indexOf('M15.192') !== -1) { // ChatGPT send icon signature
sendBtn = svgs[i].closest('button');
if (sendBtn) break;
}
}
}
if (sendBtn) {
if (sendBtn.disabled) return "SEND_BTN_DISABLED";
sendBtn.click();
return "SENT";
}
return "SEND_BTN_NOT_FOUND";
})()
'''
return self._execute_js(js, preferred_location)
def is_response_complete(self, preferred_location=None):
"""Check if AI response is complete (Simpler, more robust version)"""
js = '''
(function() {
// 1. If stop button exists, we are definitely NOT done
var stopBtn = document.querySelector('button[data-testid="stop-button"]') ||
document.querySelector('button[aria-label="Stop streaming"]') ||
document.querySelector('button[aria-label="Stop generating"]');
if (stopBtn) return "GENERATING";
// 2. Assistant messages check
var assistants = document.querySelectorAll('[data-message-author-role="assistant"]');
if (assistants.length === 0) return "NO_RESPONSE";
// 3. check for streaming class as secondary indicator
var isStreaming = !!document.querySelector('.streaming') || !!document.querySelector('.result-streaming');
if (isStreaming) return "GENERATING";
// 4. If stop button is gone and we have messages, we consider it potential completion
return "COMPLETE";
})()
'''
return self._execute_js(js, preferred_location)
def click_copy_button(self, preferred_location=None):
"""Extract text content from the last AI response directly (no clipboard API needed)"""
js = """
(function() {
var assistants = document.querySelectorAll('[data-message-author-role="assistant"]');
if (assistants.length === 0) return "NO_RESPONSE";
var lastAssistant = assistants[assistants.length - 1];
// Find the markdown content container
var mdContainer = lastAssistant.querySelector('.markdown') ||
lastAssistant.querySelector('[class*="markdown"]') ||
lastAssistant.querySelector('.prose') ||
lastAssistant;
// Get text content, preserving some structure
var text = "";
// Try to get inner text which preserves line breaks better
if (mdContainer) {
text = mdContainer.innerText || mdContainer.textContent || "";
}
// Clean up the text
text = text.trim();
if (text) {
return "TEXT:" + text;
}
return "EMPTY_RESPONSE";
})()
"""
return self._execute_js(js, preferred_location)
class GeminiChrome(ChromeController):
def __init__(self):
super().__init__("Gemini", "gemini.google.com", "Gemini", "https://gemini.google.com/app")
def ensure_gemini_tab_exists(self):
"""Create a dedicated Gemini window. Returns status string."""
location = self.create_dedicated_window()
return "CREATED" if location else "ERROR"
def is_front_tab_gemini(self):
"""Check if the front tab in Chrome is a Gemini tab. Returns 'YES' or 'NO'."""
return "YES" if self.get_front_tab_location() is not None else "NO"
def get_gemini_tab_location(self):
"""Alias for get_tab_location for clarity."""
return self.get_tab_location()
def get_front_gemini_tab_location(self):
"""Alias for get_front_tab_location for clarity."""
return self.get_front_tab_location()
def is_page_ready(self, preferred_location=None):
js = '''
(function() {
if (document.readyState !== 'complete') return "PAGE_NOT_READY";
var btn = document.querySelector('.speech_dictation_mic_button');
return btn ? "READY" : "BTN_NOT_FOUND";
})()
'''
return self._execute_js(js, preferred_location)
def start_dictation(self, preferred_location=None):
js = '''
(function() {
var btn = document.querySelector('.speech_dictation_mic_button');
if (btn) { btn.click(); return "START_DONE"; }
return "START_BTN_NOT_FOUND";
})()
'''
return self._execute_js(js, preferred_location)
def is_recording_active(self, preferred_location=None):
js = '''
(function() {
var micOn = document.querySelector('.speech_dictation_mic_button mat-icon.mic-on');
return micOn ? "ACTIVE" : "INACTIVE";
})()
'''
return self._execute_js(js, preferred_location)
def stop_dictation(self, preferred_location=None):
"""Stop dictation - in Gemini, clicking the mic button again stops and submits."""
js = '''
(function() {
// Check if mic is actively listening (has mic-on icon)
var micOn = document.querySelector('.speech_dictation_mic_button mat-icon.mic-on');
if (micOn) {
// Click the mic button to stop and submit
var btn = document.querySelector('.speech_dictation_mic_button');
if (btn) { btn.click(); return "STOP_CLICKED"; }
}
// Fallback: try to find and click the send button if transcription is ready
var sendBtn = document.querySelector('button.send-button');
if (sendBtn) { sendBtn.click(); return "SEND_CLICKED"; }
return "STOP_BTN_NOT_FOUND";
})()
'''
return self._execute_js(js, preferred_location)
def cancel_dictation(self, preferred_location=None):
"""Gemini does not support cancel - this is a no-op that returns a status."""
return "CANCEL_NOT_SUPPORTED"
def get_text_and_clear(self, activate_first=True, preferred_location=None):
js_code = '''
(function() {
try {
// Gemini uses .ql-editor with role=textbox
// It can have different aria-labels depending on language
var editor = document.querySelector('.ql-editor[role="textbox"]');
// Fallback 1: Any ql-editor (Quill editor)
if (!editor) {
editor = document.querySelector('.ql-editor');
}
// Fallback 2: contenteditable div with role=textbox
if (!editor) {
editor = document.querySelector('div[contenteditable="true"][role="textbox"]');
}
if (!editor) {
return "NOT_FOUND";
}
// Get text - innerText works better for contenteditable
var text = (editor.innerText || editor.textContent || "").trim();
if (!text) {
return "EMPTY";
}
// Clear the editor by removing all child nodes
while (editor.firstChild) {
editor.removeChild(editor.firstChild);
}
// Trigger events
try {
editor.dispatchEvent(new Event('input', { bubbles: true }));
editor.dispatchEvent(new Event('change', { bubbles: true }));
} catch(e) {}
return text;
} catch(err) {
return "ERROR:" + err.message;
}
})()
'''
if not activate_first:
return self._execute_js(js_code, preferred_location)
if not preferred_location:
return "NO_LOCATION"
b64_js = base64.b64encode(js_code.encode('utf-8')).decode('utf-8')
win_id, tab_idx = preferred_location
script = f'''
tell application "Google Chrome"
set originalWin to front window
set originalTabIndex to active tab index of originalWin
set targetWin to missing value
set targetTab to missing value
set targetTabIndex to 0
-- Find window by ID
if {win_id} > 0 and {tab_idx} > 0 then
try
set targetWinId to {win_id} as integer
repeat with win in windows
set currentWinId to (id of win) as integer
if currentWinId = targetWinId then
set targetWin to win
set targetTab to tab {tab_idx} of targetWin
set targetTabIndex to {tab_idx}
exit repeat
end if
end repeat
end try
end if
-- Validate the preferred tab is still the right service tab.
if targetTab is not missing value then
set ptUrl to ""
set ptTitle to ""
try
set ptUrl to (URL of targetTab) as text
end try
try
set ptTitle to (title of targetTab) as text
end try
if not ((ptUrl contains "{self.url_pattern}") or (ptTitle contains "{self.title_pattern}")) then
return "NOT_FOUND"
end if
end if
if targetTab is missing value then return "NOT_FOUND"
set active tab index of targetWin to targetTabIndex
set index of targetWin to 1
delay 0.15
set res to execute targetTab javascript "eval(decodeURIComponent(escape(window.atob('{b64_js}'))))"
try
set index of originalWin to 1
set active tab index of originalWin to originalTabIndex
end try
try
if (count of windows) > 1 then
set index of targetWin to (count of windows)
end if
end try
return "SUCCESS:" & res
end tell
'''
return run_applescript(script)